Textbox Int Conversion
Posted on 2006-11-30
I am getting a major headache from what seems to me as a relatively simple problem.
P.S. I am using .Net Version 2.0 and coding in C#
On my .aspx page, I have a text-box code like this:
Number of Staff:<br /><asp:TextBox ID="txtNumStaff" runat="server" Height="13px" Width="89px"></asp:TextBox>
A user can either leave the text box empty (null) or enter in a valid positive integer.
If the user enters in anything but a positive integer, a friendly error message should come up next to the text-box that says "Please enter an integer value or leave the field blank"
On my code-behind file, I have this:
protected int data_insert ()
int numStaff = Convert.ToInt32(txtNumStaff.Text);
// A few lines of database connection code along with a reference to a stored procedure that is going to insert numStaff into the database
In the database, numStaff is defined as an int, but null values are allowed.
Right now, the problem is that I receive a type exception error if I leave the numStaff field empty.
Any suggestions anyone?