Catching errors in ASP.net & C# PDF Print E-mail
User Rating: / 0
PoorBest 
Saturday, 07 March 2009
HTML clipboard

Here is my first tutorial; I hope that it will be a good starting point for some of the beginners.

Catch errors with coding can make the programmers’ and the users’ life a little easier and a lot less graying hair. If you are from a VB6 background; well throw out the ideas of using On Error Goto or Resume Next—those things DO NOT EXIST in C#-land. So lets get to the areas of catching errors with ASP.net and C#.

So what types of errors there are?

Syntax –This is a basic typos or the improper closing of tags.

Logical –It could be a math error (i.e. division by zero); mismatches (i.e. adding a string to a number); bad data output (i.e. allowing invalid time/date on the output) and there are others as well.

System—These can be ASP.net or CLR parser error based on the something that it doesn’t like about the code it is trying to process. But it could be a memory low on the server or the server got unplugged.

So lets start with the first one on the list syntax errors.

Here is some basic coding:

                        <html>
<head>
<title>Error 1</title>
</head>
<body>
<center>ErrorsRUs</center>
<asp:lbel id=”Error” runat=”server” text=”Ooops…” />
</body>
</html>
Here goes a common error; we are typing quickly or forget to close attributes or add spaces to them. 
But there is a way to catch these errors that might cause you to throw you computer or other things thru a window. It is a basic thing called …TRAPPING. With this you can trap invalid data (i.e. allowing fractions or decimals in data fields that require whole real numbers). There is one aspect of this calling for manual trapping; this stops the execution of the process of the page to load before allowing the data processing to proceed to the next point of the application. 
Lets see what this can do for us as a programmer. 
<script language="c#" runat="Server">
void WholePositveNumber(object sender, EventArgs e)
{
if (txtNumber.Text != "")
{
if (!(Char.IsNumber(txtNumber.Text, 0)))
{
if (txtNumber.Text.Substring(0,1) != "-")
{
lblEntered.Text = "Please enter only positive numbers.";
}
else
{
lblEntered.Text = "Please enter number of the positive nature.";
}
}
else if (Convert.ToInt32(txtNumber.Text) == 0)
{
lblEntered.Text = "Please enter a number greater then 0.";
}
else if (Convert.ToInt32(txtNumber.Text) > 0)
{
lblEntered.Text = "Thank you for entering a positive real number.";
}
}
else
{
lblEntered.Text = "Please provide a number.";
}
}
</script>
<html>
<head>
<title> Trapping error a la Manual Style</style>
</head>
<body>
<form method="post" action="PostiveNumbers.aspx" runat="server" ID="Form1">
<asp:Label text="Enter:" runat="server" />
<asp:Textbox id="txtNumber" runat="server" />
<hr />
<asp:Button id="btnEntered" Text="Enter" 
onclick="PositiveNumber" runat="server" />
<br />
<asp:Label id="lblEntered" runat="server" />
</form>
</body>
</html>   

So you can see that the lines of code that make sure that the numbers are positive in nature. It can make your job and the users day a little easier if you put in the safety measures for data entry. But wait there is some more tags that can make your job easier.

These are call validation controls and there are 6 of them:

  • RequiredFieldValidator—these one makes sure that the data field has something inputted in to it.
  • CompareValidator—these one compares the current data field entry with some other control or constant (i.e. password and username)
  • RangeValidator—these one lets the data field entry to be in between 2 set points(i.e. x<=2 but not x>5).
  • RegularExpressionValidator—these one lets the data corresponds to a order or pattern(i.e. Please enter your email address: all those fields would have to have a @ in its data field).
  • CustomValidator—these one is one that you can make up the rules for(i.e. like if you want to drive users insane make them have the password with a minimum of 2 numbers and 4 letters in any order but the letters have to be one small, one cap, one small, one cap or some order of that nature).
  • ValidationSummary—these one gives the user a master error message list that states all the controls listed on page.

So lets add a required field one to the example above, let insert in after the asp:textbox:

<asp:RequiredFieldValidator  ControlToValidate=”txtNumber” runat=”server”   
ErrorMessage=”Please enter a positive value in the data field.”>
</asp:RequiredFieldValidator
Last Updated ( Saturday, 07 March 2009 )
 
< Prev   Next >
School Joomla Templates and Joomla Tutorials