Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Textbox Int Conversion

Posted on 2006-11-30
6
Medium Priority
?
447 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

661 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