"cast from type 'dbnull' to type 'string' is not valid"
-
I keep getting the following error in my code: "cast from type 'dbnull' to type 'string' is not valid" My code is as follows: Do While DR.Read tbxRefNo.Text = Trim(DR("Ref")) tbxTitle.Text = Trim(DR("Title")) If Not IsDBNull(DR("Signed")) Then tbxDateSigned.Text = DR("Signed") Else tbxDateSigned.Text = "" End If tbxStatus.Text = DR("StatusName") tbxSignedBy.Text = DR("SignedBy") Loop I am reading the values from a database using the loop but I was getting a problem when I read a record that had a NULL value in the "Signed" field. I changed the code to put in the I Not IsDBNull... but I am getting the above error message. Anyone any ideas how to do this better? Thanks, macca
-
I keep getting the following error in my code: "cast from type 'dbnull' to type 'string' is not valid" My code is as follows: Do While DR.Read tbxRefNo.Text = Trim(DR("Ref")) tbxTitle.Text = Trim(DR("Title")) If Not IsDBNull(DR("Signed")) Then tbxDateSigned.Text = DR("Signed") Else tbxDateSigned.Text = "" End If tbxStatus.Text = DR("StatusName") tbxSignedBy.Text = DR("SignedBy") Loop I am reading the values from a database using the loop but I was getting a problem when I read a record that had a NULL value in the "Signed" field. I changed the code to put in the I Not IsDBNull... but I am getting the above error message. Anyone any ideas how to do this better? Thanks, macca
Are you certain this is the only field that has a null in it ? Because it looks like it should work to me. Assuming you're using SQL Server, the best approach would be to use the COALESCE keyword to avoid returning DBNUll at all. Even better would be to give these strings a default value of '' instead of null, assuming that they have the same meaning ( as they do in the UI ) Christian Graus - Microsoft MVP - C++ -- modified at 17:40 Thursday 15th September, 2005
-
I keep getting the following error in my code: "cast from type 'dbnull' to type 'string' is not valid" My code is as follows: Do While DR.Read tbxRefNo.Text = Trim(DR("Ref")) tbxTitle.Text = Trim(DR("Title")) If Not IsDBNull(DR("Signed")) Then tbxDateSigned.Text = DR("Signed") Else tbxDateSigned.Text = "" End If tbxStatus.Text = DR("StatusName") tbxSignedBy.Text = DR("SignedBy") Loop I am reading the values from a database using the loop but I was getting a problem when I read a record that had a NULL value in the "Signed" field. I changed the code to put in the I Not IsDBNull... but I am getting the above error message. Anyone any ideas how to do this better? Thanks, macca
The easiest thing to do is do DR("StatusName").ToString(). This will automatically convert any nulls to String.Empty Torin Blair - MCSD.NET
'In the immortal words of Socrates - "I drank what?".' -
I keep getting the following error in my code: "cast from type 'dbnull' to type 'string' is not valid" My code is as follows: Do While DR.Read tbxRefNo.Text = Trim(DR("Ref")) tbxTitle.Text = Trim(DR("Title")) If Not IsDBNull(DR("Signed")) Then tbxDateSigned.Text = DR("Signed") Else tbxDateSigned.Text = "" End If tbxStatus.Text = DR("StatusName") tbxSignedBy.Text = DR("SignedBy") Loop I am reading the values from a database using the loop but I was getting a problem when I read a record that had a NULL value in the "Signed" field. I changed the code to put in the I Not IsDBNull... but I am getting the above error message. Anyone any ideas how to do this better? Thanks, macca
-
hi use this to check null values If Not DR.IsDBNull(DR.GetOrdinal("Signed")) Then tbxDateSigned.Text = DR("Signed") Else tbxDateSigned.Text = "" End If Mihir..