Solved

Textbox Int Conversion

Posted on 2006-11-30
6
437 Views
Last Modified: 2012-05-05
Hello Experts:

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

        sqlCmd.Parameters.AddWithValue("@numStaff", numStaff);      
}

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?

MUCH APPRECIATED!

FG!

0
Comment
Question by:freezegravity
6 Comments
 
LVL 15

Expert Comment

by:Solar_Flare
ID: 18049708
if you are going to include the numStaff parameter in the query then it needs to have a numeric value

you can either set the parameter to 0 if the textbox is null,

or

make sure the field in the DB allows nulls and do not include numStaff in the query so it will default to null as no data was provided
0
 
LVL 10

Accepted Solution

by:
aki4u earned 300 total points
ID: 18050149
try using 'DBNull.Value' when txtNumStaff.Text is empty.
0
 
LVL 5

Assisted Solution

by:prajapati84
prajapati84 earned 200 total points
ID: 18051393
Yes u can insert null value when the textbox is empty:

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

     if txtNumStaff = ""
          sqlCmd.Parameters.AddWithValue("@numStaff", DBNull.Value);  
     else
          sqlCmd.Parameters.AddWithValue("@numStaff", numStaff);  
     end if        
}

Regards,
Mukesh
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 10

Expert Comment

by:aki4u
ID: 18051462
Mukesh, did you read my comment?
0
 
LVL 5

Expert Comment

by:prajapati84
ID: 18051958
aki4u, yes i read but sometimes explanations are more important than just a hint. How to apply - that u should mention in ur code.

Regards,
Mukesh
0
 

Author Comment

by:freezegravity
ID: 18098467
Thank you all for answering so quickly:

aki4u, prajapati84 is right in that explanations are more helpful. However, you answered first and so you did get the accepted answer.

prajapati84, thanks for illustrating how to use DBNull.Value. Its just your code is in VB while mine was in C#, but it works the same and I can translate between the two, so no big deal.

And SolarFlare, you had a good suggestion but entering in 0 would not work because the number of Staff could be 0. I also wanted to differentiate between whether numStaff was entered or not. The second suggestion had the problem of if you don't pass in a parameter to the db, your stored procedure would need some tweaking to insert/update or it would throw an error saying that a parameter was missing.

Thanks again for all who participated,

MUCH APPRECIATED!!!!

FG!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C#, asp.net MVC, sql server, LINQ 3 37
encoding an href string 2 24
Jquery datatables integration with ASP.NET MVC, bootstrap 3 22
Add or delete table rows 10 54
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question