By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,882 Members | 2,082 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,882 IT Pros & Developers. It's quick & easy.

Insert null from TextBox to integer Field

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
}
Nov 18 '05 #2

P: n/a
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" <Fr************@yahoo.com> wrote in message
news:ce**************************@posting.google.c 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

Nov 18 '05 #3

P: n/a
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" <Fr************@yahoo.com> wrote in message
news:ce**************************@posting.google.c 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

Nov 18 '05 #4

P: n/a
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" <av*@newsgroups.com> wrote in message
news:O1**************@TK2MSFTNGP10.phx.gbl...
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" <Fr************@yahoo.com> wrote in message
news:ce**************************@posting.google.c 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


Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.