Solved

Textbox Int Conversion

Posted on 2006-11-30
6
440 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 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
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.

 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

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…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

623 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