Pages

Wednesday, April 30, 2008

Using Subversion with Visual Web Developer

*Note: this method doesn't just work for Visual Web Developer, it should work for any software where source files are stored on your local drives. This includes C#, C++, Java, VB, Delphi, C++ Builder, Flash, Poser, etc.

This tutorial will get you started using Tortoise SVN to easily back up and version your websites. If you don't know why this is a great idea, then you've never screwed up your projects so bad that you wasted 3 days trying to get it back to where you started.

Step 1

Find a spot to back up your stuff. I use an external USB drive. There are also ways to do this over the intranet and Internet. You can store your archive (subversion calls this a repository) on your main hard drive, or another computer in your network as well.

Step 2

Download and install Tortoise SVN for windows. Once installed, right-click nearly any file on your system and note the new options that are available (Don't select them yet, just *note* them).

Step 3

Create a folder on your archive drive to put a project in. In my case, on the external drive it's named like this: I:\subversion\Website1.

Then navigate to the parent folder and right-click the project archive folder (in my case this was I:\subversion) and right-click the new archive folder (Website1). Choose TortoiseSVN - Create Repository Here. Select Native Filesystem (FSFS). This takes a few 10ths of a second, and creates files and folders in the archive folder.

Step 4

Now browse back to your websites folder (the place where you have the files to be archived) and right-click the project you want to check in. Select TortoiseSVN - Import. It is important to note that to Tortoise, import means "import to the archive" and export means "Pull it out of the archive". In the import dialog select the archive folder you just created (such as file:///I:/Subversion/Website1. Click OK. The contents of your project will be imported into the repository. Click OK.

Now whenever you change a file in your project, you can upload the changes to the repository, and if you mess it up, you can roll it back in an instant! I'll do more tutorials about using Tortoise SVN later.


Apologies for the delay...

Yes, you must use tortoise to check out the project again. I suggest checking it out to a new folder. There are some Windows sharing settings that will cause errors here if you are saving to a shared drive. I am still looking into this.

Saturday, April 26, 2008

Error: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

When accessing a SQL Database from an ASP.NET page, I sometimes get this error:

Error: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

... This seems to be because the SQL Server isn't spun up all the time like Oracle is. So initially after a long pause it seems to take a lot longer to set up again.

There are 2 possibilities here, one is that you are timing out before a connection is made, and the other is that your command is taking too long to execute.

Connect Timeout Set the Connect Timeout to a much higher value, like 120.

ComandTimeout Set the CommandTimeout to a much higher value.

Tuesday, April 15, 2008

MSSQL Update Trigger Example

This tutorial shows how you would create a trigger in Microsoft SQL Server 2005/2008 that will date/timestamp a column named last_updated everytime any data in the row is updated.

This example assumes a primary key that includes 3 fields.

CREATE TRIGGER MyTableUpdate
ON dbo.MyTable
FOR update
AS
UPDATE
MyTable
SET last_updated = GetDate()
From MyTable Inner Join Inserted On
MyTable.KeyField1 = Inserted.KeyField1
and MyTable.KeyField2 = Inserted.KeyField2
and MyTable.KeyField3 = Inserted.KeyField3

Thursday, March 20, 2008

Missing Event Handlers

First, has anyone other than me noticed that it takes like 2+ minutes for Visual Web Developer to fully open and come alive? What the hell is it doing? We'll save that for another time.

So you create an Ajax Web Form. You want to put an even handler on Init of the form. How? There is no event icon in the object inspector for the document.

So you click on the Script manager and presto! you get the little lightning bolt icon.

Let's try it with a master page. We create a master and a content page and guess what? There is NO WAY to add an onInit handler to the content page. There's no script manager. If you open the .cs code file and/or double-click the page in the designer, you get a page_load function.

You can try to add one by hand, but where would you put that?....

Ok lets move on.

Slap a button on the screen, and then select the button. Wohoo! the little lightning bolt is there!

Now create a table and put in a row and a cell and put the button in there. Oops, you can no longer click the button and create event handlers, you keep getting the table. In fact the only way I have found to add a handler is to drag the button out of all the panels, tables, etc., and edit it's event handlers, then drag it back into it's table cell and hope it all works.

This is goofy.

The solution is to create all components you need to access the events of outside all tables tables or panels, create the event handlers and then drag (or cut/paste) the objects back into the table or panel.

Tuesday, March 11, 2008

IE7 Displays Blank Page: Firefox OK.

When I try to browse to my website, which contins an ASP page, I get a blank page for IE7, yet all is well for Firefox.

View Source in Firefox gives the normal page, while IE just shows this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; 
charset=windows-1252"></HEAD>
<BODY></BODY></HTML>

The page had some Flash embedded, but I get this result even after removing it. Im heading for "Clearly, IE is BROKEN". What MORON would have a browser substitute a blank page without any kind of error or warning message?


I found the solution after pulling an all-nighter on it.

This had to do with a database error: here's what happened. For some reason the database error was NOT getting reported to the browser, it just got a blank page.

I store the data from every hit in a table for diagnosing problems. That data element was a varchar(250), which up until now was more than adequate. The string from a firefox hit comes in at 163 and looks like this:

03/10/2008 16:24:14 Mozilla Netscape 5.0 (Windows; en-US) Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12 en-US Win32

However IE7 sends this for the exact same page:

03/10/2008 16:24:14 Mozilla Microsoft Internet Explorer 4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.590; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022) Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.590; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022) undefined Win32

That's a whopping 380 characters, which caused a string length error on my SQL insert command.

I still don't understand why in hell it returned a completely blank page to IE without any errors listed but whatever, I'm re-doing the whole site in ASP.NET anyways.

The ANSWER: I had to stretch the database column where I'm storing the hit data strings to 1000 characters. That ought to hold me until IE8 SP2 at least.

Thursday, March 6, 2008

ASP.NET: Uploading Files

This is so easy. Drop a FileUpload object onto your AJAX web page. Drop in a button.

In the button event handler, do this:

//get the path to the UPLOADS directory in your webbserver
        string path=Server.MapPath(@"~\Uploads"); 

//create the full path+file name
        string SaveAsName=path+@"\"+FileUpload1.FileName;

        try
        {

//This is the bulk of it right here.
            FileUpload1.SaveAs(SaveAsName);
        }
        catch(Exception err)
        {
            Label1.Text = err.Message;
        }

That's it. The FileUpload object hands you all the data and functionality you need. Of course I could have checked to see that the file being uploaded is of a certain size or type, but this quick tutorial is just about the basics.

Sunday, February 17, 2008

Permissions...

This article is about setting up private, member-only areas of your website that are either for admins or clients to access. The .net framework gives several easy ways to set this up with a minimal amount of coding.

Assuming you already have a website, and wat to add a restricted zone, do this:

First, enable permissions for your website

WebSite->ASP.NET Configuration
Browse to the security tab.

There are essentially 3 things to configure: Users, Roles, and Access. By default this data is stored in a MSSQL database in your website's folder structure.

I like to enable roles first, creating roles like client, admin, mod, or whatever. This process is easy enough. For this tutorial, create a user, and give it the admin role. Now before we can add access, we'll close the config screen and return to our project.

Create a folder



Add any html or aspx web page to the new folder. Add a link to the new page to your main website.

If you run the website now, you get free access to the new page (we haven't limited access yet).

Return to the ASP.NET Configuration and on the security tab, click Manage Access Rules.

Click on the new admin directory and then add allow permission for admin role. Then add deny permission for all.

The way this works is that this list is accessed from top to bottom, looking for a permission to apply. The first match that is found is applied. Therefore if we gave the deny - all permission first, then nobody would have any access to the folder.

The Login Page

By default, the login page is named login.aspx. This page will load any time that the client doesn't have sufficient privileges to access the page asked for. It must be in the unprotected part of your website.

So create a new page named login.aspx. Add in a login and password recovery gizmo from the login tab of the toolbox. Click on the gizmos and use the Auto Format function from the pop-out box to select the perfect style.

That's it.

Now anytime the user tries to access a page that he's not logged in for, he first gets the login screen. If he is already logged in, he gets the screen he asked for without interruption.

It's cool. I know.

Saturday, February 9, 2008

ASP.NET How to open a URL in a pop-up window.

Note: if you just want the browser to open a page in another browser page, look up the target parameter of the a href tag.

If you just want to pop a quick message box, look up the javascript alert function.

This article demonstrates how to pop up a new browser window on the click of a hyperlink and load whatever page you want in a controlled browser window.

In the head portion of your page , put the following javascript function.


<script language="javascript">function Pop(URL){
  a=window.open(URL,'MyWindow','status=0,toolbar=0,width=300,height=250');
  a.focus();
  return false;} 
</script>

AJAX NOTE:this can be in the AJAX master page, or in the "Content1" head section of the content page, as I show here.)

Then create a link in the body of your article, like this:


<a onclick="return Pop('htmlpage.htm')" href="http://www.blogger.com/">Test the popup feature</a>

Here's the details of how this all works.

URL: Within the function: We pass in the URL to open from our call to the function. The URL can be any html, shtml, or aspx page.

window.open: Call this javascript function and give it a URL, a window name (the html name of the document) and the opening parameters. You can use the name parameter to control the number of popups. For instance, if we created 10 links all naming the same name (like "MyWindow"), then they will all load in the same pop-up window. If we create 10 links all naming different windows, then each hyperlink will sen the page to it's own named window.

Using the parameters in my example, the pop-up window will be a fixed size, and have no status bar or toolbars. Here is a more complete list of parameters:

status The status bar at the bottom of the window.
toolbar The standard browser toolbar, with buttons such as Back and Forward.
location The Location entry field where you enter the URL.
menubar The menu bar of the window
directories The standard browser directory buttons, such as What's New and What's Cool
resizable Allow/Disallow the user to resize the window.
scrollbars Enable the scrollbars if the document is bigger than the window
height Specifies the height of the window in pixels. (example: height='350')
width Specifies the width of the window in pixels.

a: Once Javascript creates the window, it assigns it to the Javascript object (in the example it's named "a").

a.focus(): If the user clicks the link on the main page again after the window was initially created, the main page may cover the pop up. This line says that whenever we pop or reload this window's URL, we'll set focus on it.

return false: It's a function. We're returning false. Bear with me, I'll explain why in a moment.

Now we'll look at the hyperlink. It's a nearly standard html construct with a few unfamiliar twists.

href="": This says to browse to noplace. We could have left this out, but then the link would not look like a link in the page. It would look like regular text. Normally passing in "" as the href would force a reload of the page we're already on, I'll show how to stop the page from reloading later.

onclick=: This is where we tell the link nto call our function. Now we could have just said onclick = "Pop('hrmlpage.htm')" but as it turns out the onclick event expects a return value. If that value is true, then the link exectues its default behavior and browses to the link in the href parameter, but if it recevices a false, it skips that part. We could have done this:


<a onclick="Pop('htmlpage.htm'); return false;" href="">Test the popup feature</a>

...and the reload of the main page would be suppressed. But in our case, since our function already returns a false (handy, huh?), we can use that value to tell onclick to stop the link from reloading. We need to tell the onclick call to accept the value of the function with the return keyword as shown in the original version of the html link.

Note that there is no reason the Pop function must be married to a hyperlink. It is possible (though annoying) to call it on page load, mouseover events, or any other browser event.

Monday, January 21, 2008

Why is EVERYTHING so Damned Hard?

Windows 2003 SBS ASP.NET 2008 Express SQL Server 2005 .net framework 3.5 (not beta)

So, I wanted to add a login page to my website and offer more content to logged-in users. I saw the login components and followed the tutorial to the letter. It works on my desktop perfectly. I can create logins and even the forgot my password crap works. I publish it to my website, and try to log in.

An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections.

Oh, shit! So I search the help (when am I going to learn?) and it tells me that all I have to do is open an Application that DOES NOT EXIST on my server, and grant THE INTERNET access to my DATABASE. I don't think so.

Please, if you know what is wrong, go to http://209software.com/Login.aspx and use the username "a" and the password "a". It's not a valid login, but it crashes every time trying to verify.

My web server, email server, and database server are all the same machine.

Here's what I want to know, and MSDN has squat on it.

  1. if I grant database access to the database, can people BROWSE my database over the web?
  2. What am I doing wrong? and how do I make it right?
  3. WHERE on the internet can you go if the Microsoft help is crap?

Ok, I got it. I downloaded something, but it wasn't SQL server 2005 express. Now, a mere 8 hours of downloading and installing later, it seems to work.

Monday, December 10, 2007

Wednesday, November 21, 2007

Creating a new Class

Create a new winforms project, then right-click the project and do this:

Create a new Class object

Add this code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassDemo
{
    class newClass
    {
        public string sharedString = "This can be read and written by anyone.";
        private string myString = "Only visible in newClass.";


    }
}

And then right-click and add a 'ctor' (constructor) snippet as shown.

Tuesday, November 20, 2007

Day 5: Arrays

The website of the day is here.

And here is the example code...


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Arrays
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            /*
             * Arrays Demo
             */
            richTextBox1.AppendText("FIXED ARRAYS"+Environment.NewLine);
            //a fixed array of 4 strings (0-3), initialized on create.
            String[] myfamily = new String[]
               {"Bryan", "Ruth", "Rebekah", "Rachel"};
            
            //a fixed array initialized later
            String[] myPets = new String[3];
            myPets[0] = "Sasha";
            myPets[1] = "Ross";
            myPets[2] = "Nightstar";
            
            //reading fixed arrays
            foreach (string s in myfamily) 
            {
                richTextBox1.AppendText(s + Environment.NewLine);
            }
            foreach (string s in myPets)
            {
                richTextBox1.AppendText(s + Environment.NewLine);
            }

            Random R = new Random();
            

            int P = (int)R.Next(3);
            int F = (int)R.Next(4);

            string playtime = 
              string.Format("{0} is playing with {1}", myPets[P], myfamily[F]);
            richTextBox1.AppendText(playtime + Environment.NewLine);

            //variable Arrays
            richTextBox1.AppendText(Environment.NewLine + 
              "VARIABLE ARRAYS" + 
              Environment.NewLine);
            //untyped array
            ArrayList RandomStuff = new ArrayList();
            //you can put anything into this array            
            RandomStuff.Add(new Font("Arial",12));
            RandomStuff.Add(7.1);
            RandomStuff.Add("The Castle Anthrax");

            //now... how can we work with this collection of trash?

            foreach (object O in RandomStuff)
            {
                richTextBox1.AppendText("I see a(n) " + 
                  O.GetType()+Environment.NewLine);
                if (O is System.Drawing.Font)
                    richTextBox1.AppendText("   ->" + 
                      (Font).FontFamily+Environment.NewLine);
                if (O is Double)
                    richTextBox1.AppendText("   ->" + O.ToString() +  
                      Environment.NewLine);
                if (O is String)
                    richTextBox1.AppendText("   ->" + O.ToString() + 
                      Environment.NewLine);                    
            }

            //typed vararrays
            richTextBox1.AppendText(Environment.NewLine + 
             "TYPED VARIABLE ARRAYS" + 
             Environment.NewLine);
            List<String> myStrings = new List<String>();
            List<Double> myNumbers = new List<Double>();
            List<Button> myButtons = new List<Button>();


            myStrings.Add("Bread");
            myStrings.Add("Mayo"); 
            myStrings.Add("Turkey");
            myStrings.Add("Cheese.Provolone");
            myStrings.Add("Bread");

            foreach (string ingredient in myStrings)
            {
                richTextBox1.AppendText(ingredient + " ");
            }
            richTextBox1.AppendText(Environment.NewLine);

            /*
             * Since these arrays are stongly typed, you can't put a 
             * string into the button array nor a number into the 
             * string array.  you *can* however put the descendant
             * of a any object into a typed array.
             */
        }
    }
}



Todays Assignment: make a list of your favorite things.

Monday, November 19, 2007

Day 4: launch an outside App

This is a little less formal than previous posts, but here goes. First, create a form with a button, a RichText, and a PictureBox.

Here is the source code I wrote to accomplish this. I am a big believer in examples. They tell the story in a far more eloquent way than my witty expose'.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Process POVRay = new Process();
        

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StringBuilder povtext = new StringBuilder();
            string camera = "camera{location <0,1,-1>*10 look_at<0,0,0>}";
            string light1 = "light_source{<0,1,0>*1000 color rgb 1}";
            string water = "plane{y,0 texture{pigment{color rgb 0.5}"+
                " finish{reflection .2 }" +
                " normal{ bumps .4 turbulence .7 }}}";
            string sphere = "sphere{<0,1,0>,1 pigment{color rgb<1,0,0>}}";

            povtext.AppendLine(camera);
            povtext.AppendLine(light1);
            povtext.AppendLine(water);
            povtext.AppendLine(sphere);

            richTextBox1.Text = povtext.ToString();

            //save to file

            using (StreamWriter sw = new StreamWriter("C:\\temp\\demo.pov"))
            {
                // Add some text to the file.                
                sw.WriteLine(povtext);
                sw.Flush();
                sw.Close();
            }


            //launch POV-RAY and create a new bitmap;
            Process POVRay = new Process();
            POVRay.StartInfo.FileName = 
                "C:\\Program Files\\POV3.6\\bin\\pvengine.exe";
            POVRay.StartInfo.Arguments = 
                "+IC:\\temp\\demo.pov +OC:\\temp\\demo.bmp +w320 +h200 -P -D /EXIT";
            POVRay.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;           

            POVRay.Start();
            POVRay.WaitForExit();
            this.pictureBox1.Load("file:C:\\temp\\demo.bmp");
           
        }
    }
}


You will need POVRay to run this.

Your assignment: figure out how to launch another application like Word, Firefox, or Notepad.

Enjoy.

Thursday, November 15, 2007

Day 3: WinForms App

Less prose! More examples!

Start C# File - New Project.

Windows Forms Application.

This is what you should see.

Look in the toolbox for a class object called a button and another called a textbox. Do something like this:

Rightclick the textbox. Select properties.

Look now at the properties box.

Ok now a few things to note. Look at the speed buttons just below the word "textbox1". The first 2 icons pick whether you are sorting alphabetically or by groups. The next 2 icons determine whether you are viewing properties or methods.

Now find the multiline property of textbox1 and change it to true.

Note how the handles on the textbox have changed. Grab the lower right handle and stretch it like this.

ok, now double click button1 in the editor.


        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "Written by CODE";
        }

Add the text in bold. Then hit the F5 key to start with debugging. After clicking the button, you'll see this.

More examples...

        private void button1_Click(object sender, EventArgs e)
        {
            //this is a comment, the compiler ignores it.
            /*
             * this kind of comment can span more
             * than one line
             */

            //putting text in the textbox
            textBox1.Text = "Written by CODE";

            //adding to the text that is already there.
            textBox1.Text += " because we cared.";
            
            //newline example 
            /*(Environment.NewLine ensures that 
             * the correct chars are used based 
             * on the operating environment)*/
            textBox1.Text += Environment.NewLine+"(new line)"; 

            //appendtext
            textBox1.AppendText(Environment.NewLine);
            textBox1.AppendText("appended text");

            //casting non-strings
            textBox1.AppendText(Environment.NewLine);
            double j = 22.0 / 7.0;
            textBox1.AppendText(j.ToString("F04"));

            textBox1.AppendText(Environment.NewLine);
            int i = 1024;
            textBox1.AppendText(i.ToString());

            //results of methods
            textBox1.AppendText(Environment.NewLine);
            textBox1.AppendText(Environment.CommandLine);

            //math results
            textBox1.AppendText(Environment.NewLine);
            textBox1.AppendText(Math.Cos(0.1).ToString("F05"));

            textBox1.AppendText(Environment.NewLine);
            textBox1.AppendText((110.0/73.0).ToString("F05"));

            //dates
            textBox1.AppendText(Environment.NewLine);
            DateTime D = new DateTime(2007, 12, 31, 17, 00, 00);
            textBox1.AppendText(D.ToShortDateString());
            textBox1.AppendText(" "+D.ToShortTimeString());

            DateTime NOW = DateTime.Now;
            TimeSpan diff = D - NOW;
            textBox1.AppendText(Environment.NewLine);
            textBox1.AppendText(diff.Days.ToString()+" days " );
            textBox1.AppendText(diff.Hours.ToString() + " hours");
            textBox1.AppendText(diff.Minutes.ToString() + " minutes");

            //changing object properties
            this.BackColor = Color.AliceBlue;
            this.ForeColor = Color.Blue;

            /*
             * note how we can refer to the current form as "this"
             * 
             */
        }

Tuesday, November 13, 2007

Day 2

housekeeping

Here are the books and websites I use most when the answers are not coming to me.

C# for Programmers (2nd Edition) (Deitel Developer Series)

Pop open Microsoft Visual C# 2008 Express Edition - I suggest you pin it to your start menu.

Notice the area in the center, called the Start Page.

Now look closer at the "Getting Started" section.

There are actually some good resources there for you to use.

ready, set, go.

Today we create our first application.

Sadly it'll be a console app.

Click File - New Project - Console Application. You'll get something that looks like this:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
  class Program
  {
      static void Main(string[] args)
      {
      }
  }
}

The using's are all the .net namespaces or libraries that are included in the project by default.

The ConsoleApplication1 namespace is your own private realm where your code exists. All .net code exists in a namespace.

Variables

As I mentioned, all things in C# are class objects. The difference between a class object and a variable are that class objects can contain methods and they can be extended. An int in Delphi can't have any of its own functions, but in C#, even the simplest data object can and does.

Do this: Add this to the code above.

      static void Main(string[] args)
      {
          int x;
          int y = 2;
          x = 7;
          int z = x + y;          
      }
  }

int x; creates an integer class object but does not assign it a default value. Any attempt to read or manipulate it at this point would cause an exception.

The int y = 2; line shows a shorthand way of creating and initializing the value at the same time.

The x = 7; shows how to add or change data in an existing data object.

The int z = x + y; line shows a variable z being created containing the results of a calculation. Note that subsequent changes to x and y will not cause z to recalculate, it just works the math at the time and stores it in z.

Now do this. Type z. and a popup context helper appears. This shows the methods that can be accessed from an int.

if you mouse-hover over the items you get an explanation like this.

Ok, time to move on to...

EMail Demo

Now clear out all the varibables and type in the word MailMessage as shown.

Note the little rectangle under the "e". Hover your mouse over that and you'll see this:

What the IDE is offering to do is add the namespace System.Net.Mail to your using section or, alternately, add it to the declaration itself. You don't have to add a namespace to your project to access it, you can explicitly pick objects from any namespace by using their fully qualified name. Adding them to the using clause just simplifies the typing.

In this case, choose the first one, add System.Net.Mail to the using section.

Now you'll see the using System.Net.Mail; has been added to the top section of the program and all the objects and methods in that namespace are now native to your program.

The MailMessage class is an object that can be used to send or receive email messages. You can create an empty one and load it later like this...


MailMessage myMailMessage = new MailMessage();
myMailMessage.To = new MailAddress("bryanv@eloan.com", "Bryan Valencia");
myMailMessage.From = new MailAddress("gumby@eloan.com", "Gumby and Pokey");
myMailMessage.Subject = "I am feeling down again.";
myMailMessage.Body = "I am all congested with disclosures.";

Or you can create it and initialize it with variables. Note that we had to create 2 MailAddress objects to set the To and From addresses in myMailMessage.

The following example shows how to create a MailMessage object with all its values pre-initialized.


      static void Main(string[] args)
      {
          MailMessage myMailMessage = new MailMessage(
              "gumby@eloan.com",
              "bryanv@eloan.com",
              "I am feeling down again.",
              "I am all congested with disclosures."
          );
      }


Ok, now let's send this bad-boy. In order to do that (and please feel free to substitute your own address for the To address), you need an object called the SMTPClient. It's in the same namespace as the MailMessage.


      static void Main(string[] args)
      {
          MailMessage myMailMessage = new MailMessage(
              "gumby@eloan.com",
              "bryanv@eloan.com",
              "I am feeling down again.",
              "I am all congested with disclosures."
          );

          SmtpClient mySMPTClient = new SmtpClient("eloan.com");
          mySMPTClient.Send(myMailMessage);

          Console.WriteLine("Message Sent");
      }

Now, hit Control-F5, or from the menu, Debug - Start without debugging. You should get this:

Then in about 10 seconds, whoever you decided to bless with your email should see it in their inbox.

Let me diagram what we just did. When we created the MailMessage object, here's what was happening:

what we typedwhat happened
MailMessageWe told C# that we are about to create an object of the class MailMessage in the System.Net.Mail namespace.
myMailMessagewe told C# that we are going to call it myMailMessage.
= new MailMessage(...)We told C# to go ahead and create it now - that means to allocate memory and initialize it with either default values or the ones we are passing in. The object is now created and resides somewhere in memory, and can be accessed by the name myMailMessage.

Constructors

Every class object has a predefined method called a constructor. It tells C# how to create instances of itself. Constructors can be simple, like the constructor for an int, or more complicated like the one for MailMessage. They can even be overloaded.

Try this:

Note how the context helper pops up a helper for the constructor. There are 4 overloaded constructors for the MailMessage class. Use the down arrow key to see the formats you can choose from. All methods in all objects will pop these helpers while you are editing.

A few little items

Single quotes and double quotes are not interchangeable. Strings use the double quote marks "like this". Chars use the single quote (or tickmark or apostrophe), like this: 'A'.

A class is the organization of an object, an object is an instance of that class. Or, think of classes as cookie cutters (or blueprints) and objects as cookies (or Lear Jets).

Overloading means that there can be many methods with the same name. More on this later.

Monday, November 12, 2007

Getting Started with Visual Studio Part 1

This is intended to be a quick start tutorial so make sure to read them all in order. Use the GettingStarted Label on the web page to find all the articles in this series. This is the result of what happened to us last week. We had been promised a class in C#.NET to accommodate an upgrade project, but instead, most of us got laid off. Since I had been to the pre-training training, I attempt here to share the wealth.

This tutorial will use the Visual Studio 2008 Express (free) editions only, and will cover ASP.NET and C#.NET development ONLY. There will be no VB or J# here. Also, I am not a Microsoft hack, so when something sucks, I'll say so. Ok, on with it.

First topic: Getting the software.

Today the software may be found here.

If this page is not live anymore, then try googling for VS Express 2008 Download. Be sure to download C# and Visual Web Developer.

Accept all the defaults and reboot between and after the installs. Now you will see the IDE's under Start - All Programs.

Let's get started.

Click on the Visual Studio 2008 link to bring up the IDE. It takes the news channel several seconds to load so be patient. Let's talk about some conceptual topics before we start coding.

C# is like C++

Yes, it's kind of like C++ and kind of like Java. Everything is a class object. Even strings and integers are classes. Even though C# makes it easy to instantiate them, they are very much the same.

Managed memory

Because of the way the .NET framework manages memory, you need not worry about memory and resource leaks. When the memory manager notices that nobody is holding a pointer to some object, it places it on a list of items that can be wiped. This means that you as a developer have no control over when objects are destroyed, and no guarantee that they'll be destroyed in any particular order, even if the item is a parent of an object that is not being destroyed. I know, it's not the coder's way. Get over it.

CLR and Languages

The Microsoft Common Language Runtime makes it so you can create objects in one language and use them freely in another. For instance a Zip code lookup object in C# can be used in J#, VB.NET, or other C# projects. The CLR is your friend. Gone are the days of having to rewrite all your Delphi Projects into VB. You're going to like this, I promise.

Tomorrow, our first project. Don't forget to bookmark this. Your assignment for tonight is to download and install C#, ASP.NET, and optionally the GIMP.

Thursday, November 1, 2007

OracleGetClob Woes

Must... type... this... before... head... explodes!

Ok, I have constructed a query and I'm fetching data from the database, including an Oracle Clob column.

The regular columns work great and look like this:

description = reader.GetValue(5).ToString();

bot for some inane reason,

description = reader.GetValue(6).ToString();

...when run against a clob field will not work if your life depends on it.

I have been to a total of 25 web pages now looking of one solitary example of how to read an oracleclob, and there isn't one! Just one f'ing example is all I need, to see what needs to be done to put the data from my clob object into my string object.

There are many sites that contain volumes about the OracleGetClob() method, and some about the Fetchsize property, but not one solid example that starts with a clob and ends up with the text in a string!

How does it all work together? Anyone?

This is so goddamned frustrating. Where is my Delphi CD?

Wednesday, October 31, 2007

Online Backup Solutions

product cost review
Carbonite free for 15 days, $49.95 a year (per PC), unlimited storage Installs in a flash and works in the background. The install is very easy and the popups afterward tell you exactly what to expect.
iBackup free trial (how long?)$99.50 a year 5GB
intronis 30 day trial, $9.95 a month ($119.40 a year) for 1GB
xdrive 5GB Free, 50GB for $9.95 a month ($119.40 a year) I had a lot of hope for this one, but bottom line: they required my phone number and zip code, and then presented me with a completely illegible verification image, the registration pages took 3-5 minutes to load, and they would not allow me to use my preferred screen name (some_yahoo).

I hate not being able to use my regular screen name, I sign up for dozens of services online. My attitude is that if I have to make a unique screen name for it, it's not worth it.

Then there is the notion that if the "pick a screen name" screen takes 5 minutes to come up, I can't imagine trying to backup my webserver on this service. Oh well - I'll never know now.

Monday, October 15, 2007

How to horitontally center a div in CSS

This is so easy, I spit coffee through my nose.
<div style="margin-left:auto; margin-right:auto; width:150px; display:block;"> This div is centered </div>
And here's what it looks like:
This div is centered

Share This!

Contact Us

Name

Email *

Message *