Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Textbox Int Conversion

Posted on 2006-11-30
6
Medium Priority
?
456 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 1200 total points
ID: 18050149
try using 'DBNull.Value' when txtNumStaff.Text is empty.
0
 
LVL 5

Assisted Solution

by:prajapati84
prajapati84 earned 800 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
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.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

971 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