Solved

Textbox Int Conversion

Posted on 2006-11-30
6
434 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now