DBNull

Discussion in 'Visual Studio' started by castro05, Aug 6, 2004.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. 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:
     
  2. 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.
     
  3. 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>
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page