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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DBNull error

Status
Not open for further replies.

briancostea

Programmer
Apr 21, 2005
82
US
i just started getting the following error this week ... it was working fine for about 3 weeks until yesterday. any help would be greatly appreciated.

error:
System.InvalidCastException: Conversion from type 'DBNull' to type 'String' is not valid.


code:
txtCarAg.Attributes.Add("value", IIF(ObjDataReader("car") is dbNull.Value, "", replace(ObjDataReader("car"), "*", "'")))

thanks - brian
 
If the field is null, the cast will fail. So try something like this:

If Not (ObjDataReader("car") Is DbNull.Value) Then
'do something
Else
'do something else
End If

-DNG
 
i believe that is what i am doing with the iif() statment. it should return "" if the field is null.
 
When inserting your records it's a good idea to check for null values. your not getting this error using the data reader are you? The datareader is one way only(Forward only)

When I use a datareader i always do the following

txt.text = reader("FieldName") & ""

Are you trying to update the DB when you receive this error?

Let the datareader populate your controls, then check for null values when updating.

 
Use the following:

txtCarAg.Attributes.Add("value", IIF(ObjDataReader("car") is dbNull.Value, "", replace(ObjDataReader("car").ToString(), "*", "'")))

I am sure it will work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top