PDA

View Full Version : DBNull


castro05
08-06-2004, 07:48 AM
I have develop an aspnet page using vb code. I retrieving data from a sql server (2000). The problem is that I have two field on one of the table that are has null values and I getting the following error code

Cast from type 'DBNull' to type 'String' is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Cast from type 'DBNull' to type 'String' is not valid.

Source Error:


Line 54: '// So, to get the correct value, I simply subtract 1.
Line 55: EventDescription.Text = myDataReader.Item("EventDescription")
Line 56: EventStartTime.Text = myDataReader.item("EventStartTime")
Line 57: EventPhone.Text = myDataReader.Item("EventPhone")
Line 58:

JerSchneid
08-06-2004, 09:05 AM
That error occurs when you're trying to assign a null value to a string.

In your case it's possible your giving "myDataReader" a bad column name (If you pass it a column name that doesn't exist, it will return null). I also noticed on line 56 you use a lowercase item, instead of a capital Item as on the other two lines. This could be causing a problem.

Scott
08-06-2004, 09:47 AM
You can avoid the DbNull error by explicitly casting the result of your data reader to type string as in:
Line 55: EventDescription.Text = myDataReader.Item("EventDescription").<u>ToString</u>