Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning Null

Status
Not open for further replies.

quinnipiac0807

Programmer
Oct 25, 2004
38
US
I have this line of code that works with my class. I want this to return null if selecteditem.value == 0. Problem being that when null is converted to int it returns 0. I need to put a null value into my database. Any suggestions.

address.StateID = Convert.ToInt32((ddlState.SelectedItem.Value.ToString().Length==0)?null:ddlState.SelectedItem.Value.ToString().Trim());
 
You can't store a database null in an integer variable.

What you can do is use a separate partner boolean value to indicate null:
Code:
if (ddlState.SelectedItem.Value.ToString().Length==0)
  StateIsNull = True;
or use a indicator value within the integer itself:
Code:
if (ddlState.SelectedItem.Value.ToString().Length==0)
  address.StateID = Int.MinValue;
And when you write to the database, you would test for MinValue, storing a DBNull instead.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top