Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Insert null from TextBox to integer Field

Reply
Thread Tools

Insert null from TextBox to integer Field

 
 
Fraggle
Guest
Posts: n/a
 
      05-07-2004
I have an asp.net page, I have a text box where people can enter an
integer (age). I wish to store this in a SQLServer Database. If the
person enters no age then I would store a NULL.

However if the textbox is left blank, I get an error
"[InvalidCastException: Cast from string "" to type 'Integer' is not
valid.]"

How can I work around this, and get the null into the db?

Cheers

Fragg
 
Reply With Quote
 
 
 
 
=?Utf-8?B?RCBTaGVsZG9u?=
Guest
Posts: n/a
 
      05-07-2004
You have to use Convert.DBNul

For example (using a stored procedure)

int intValueFromTextBox = Convert.ToInt32(txtTextBox.Text)
if(intValueFromTextBox ==null

cmdInsertRecord.SelectCommand.Parameters["@nAge"].Value = Convert.DBNull

els

cmdInsertRecord.SelectCommand.Parameters["@nAge"].Value = intValueFromTextBox
}
 
Reply With Quote
 
 
 
 
avnrao
Guest
Posts: n/a
 
      05-07-2004
how are you accessing db? with stored procedures? when setting the parameter
value check if the text box value is "" and set the value to null.
Av.

"Fraggle" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
>I have an asp.net page, I have a text box where people can enter an
> integer (age). I wish to store this in a SQLServer Database. If the
> person enters no age then I would store a NULL.
>
> However if the textbox is left blank, I get an error
> "[InvalidCastException: Cast from string "" to type 'Integer' is not
> valid.]"
>
> How can I work around this, and get the null into the db?
>
> Cheers
>
> Fragg



 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      05-07-2004
If you're using a SQL Statment, you can simply omit the column from the
INSERT SQL statement.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Fraggle" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> I have an asp.net page, I have a text box where people can enter an
> integer (age). I wish to store this in a SQLServer Database. If the
> person enters no age then I would store a NULL.
>
> However if the textbox is left blank, I get an error
> "[InvalidCastException: Cast from string "" to type 'Integer' is not
> valid.]"
>
> How can I work around this, and get the null into the db?
>
> Cheers
>
> Fragg



 
Reply With Quote
 
Patrick Delifer
Guest
Posts: n/a
 
      05-07-2004
What DB are you using? if you're using SQL, What type did you give to your
Age Column (int, string, etc?)make sure it matches the cast type you are
getting back from the DB.
Go to the Age COlumn in you table and select Allow Nulls.

let me know

Patrick

"avnrao" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> how are you accessing db? with stored procedures? when setting the

parameter
> value check if the text box value is "" and set the value to null.
> Av.
>
> "Fraggle" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) om...
> >I have an asp.net page, I have a text box where people can enter an
> > integer (age). I wish to store this in a SQLServer Database. If the
> > person enters no age then I would store a NULL.
> >
> > However if the textbox is left blank, I get an error
> > "[InvalidCastException: Cast from string "" to type 'Integer' is not
> > valid.]"
> >
> > How can I work around this, and get the null into the db?
> >
> > Cheers
> >
> > Fragg

>
>



 
Reply With Quote
 
ak_ambi ak_ambi is offline
Junior Member
Join Date: Oct 2008
Posts: 1
 
      10-13-2008
In WindowsForms I was able to do that this way:

txtNummer.DataBindings["Text"].NullValue = "";
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
create textbox at run time and insert data from textbox to database tanmaypatankar ASP .Net 0 12-18-2007 03:47 PM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
how can i save a null value from web control to an integer field in the data base casper_992@hotmail.com ASP .Net 2 09-26-2005 07:50 PM
Insert NULL if Form Field Blank Keith ASP General 1 06-09-2004 12:16 PM
About Insert Null to DateTime Field FatboyCanteen ASP .Net 6 11-11-2003 06:40 AM



Advertisments