Processing Input
4 Feb 2008 @ 02:10PM

Updated: 22 Jan 2010 @ 02:10PM
Now that we have the form data in variables, let's do something fun with it. As always I'll post the code and then explain what it does.


More code


Ok, the first thing you may notice is that on line 7 I've declared the contentString variable as equal to null. Then on line 37 I have contentString += to our form data. The += sign concatenates the following string onto the end of the previous string. Basically I'm adding on to what's already there. This is important in our implementation.

On line 12 I have the word try. This is a try block. You may notice that the section afterward has a { (brace), then some indented code, then a } and finally catch {}. What this basically does is attempt to execute the code inside the try{ } portion and if the code has an error (throws an exception) it runs what's inside the catch{ } portion. In this case there is nothing in the catch{} section. Why would I do this? Try removing the try block from around the name part. Then pull up the page. You'll probably get an error message. I'll explain why in a second.

Within our try blocks there is an if statement. This is the basis of programming descisions. I check the length of the name variable and if it's > 0 I run the code inside the if block. And this is why I have to have a try block. If you pull up the web page without submitting a form, the name variable is not set since it was never submitted. Since it's not set, you can't find out what the length is and the code throws an error. By wrapping it in a try block we eliminate this problem.

In the case that the name field exists and has a length greater than 0, I then stick some content into the contentString string. Eventually this is output onto the page. I do the exact same process with the birthdate and occupation fields.

This wraps up our tutorial. This last page introduces several concepts so if you're confused, go back over it. Try playing around with the code using what you've learned so far. Add additional fields and see if you can spit out different text strings. What was introduced here is the basis of a huge chunk of C# web application programming. The sooner you understand what's going on, the better. We'll be going further into logic in the next tutorial.
Comments (0)