Cast from type 'DBNull' to type 'String' is not valid.

Ok, that is the error... "Cast from type 'DBNull' to type 'String' is not valid. "

I am filling a DataSet from the db and some fields are empty.
Reading from it and placing the info into a label.
Code below....

Sub getProductInfo()
Dim DSprod2 As New DataSet()
Dim MyCommand As OleDbDataAdapter
sql = "Select * from tblProducts Where fldNum = " & intfldNum
MyCommand = New OleDbDataAdapter(sql, conn)
MyCommand.Fill(DSprod2, "tblProducts")
lblProd.Text = DSprod2.Tables("tblProducts"). _
Rows(0).Item("fldProduct")
End Sub

Now, how do I get around a null field in the db?
I have looked at the quick starts, the net, and here in forums...
Is there an easy way to do this such as a type cast?
Thanks,
Zath
0
Zath
4/28/2004 7:33:22 AM
asp.net.getting-started 91979 articles. 4 followers. Follow

8 Replies
837 Views

Similar Articles

[PageSpeed] 11

Hi,


Add "ToString()" to the line where you set lblProd.Text.
This property expects a string.

lblProd.Text = DSprod2.Tables("tblProducts").Rows(0).Item("fldProduct").ToString()

Hope this helps,
Jeroen
0
JeroenKlooster
4/28/2004 12:06:44 PM

Try using the ToString method on the Item. For example:
    Rows(0).Item("fldProduct").ToString ()

Steven Bey

Recursion: see Recursion
0
stevenbey
4/28/2004 12:08:25 PM
You could do an IF statement right after you Fill your DataSet to check to see if the field is null.  Such as:

If DSprod2.Tables("tblProducts").Rows(0).Item("fldProduct") = System.DBNull
lblProd.Text = "Not Found"
Else
lblProd.Text = DSprod2.Tables("tblProducts").Rows(0).Item("fldProduct")

Hope I helped!
In this world there are 10 types of people, those that know binary and those who don't.
0
optik
4/28/2004 12:10:01 PM
Where possible, we set our DB default value to an empty string and then disallow Nulls.

I also have an old habit of concatenating strings returned from a database with string.empty. this causes a null to turn into an empty string.
Mike Banavige
~~~~~~~~~~~~

Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
0
mbanavige
4/28/2004 12:14:46 PM

Thank You.......
I have tried to use the .ToString in every place, in every instance....
When I even tried but not tested the Rows(0).Item("fldProduct").ToString (),
the .ToString never came up in the help pop up box; therefore, I need to upgrade.....

Thanks again...
Zath
0
Zath
4/28/2004 12:32:32 PM
Optik,

I have thought about that, but my dataset has more than one textbox to fill.....
So, that would be too much code......
I just didn't know how to read a null to a string into a textbox from a db.....
But we both just found a better way to code.....
Ohh the wonders of creating bugs then killing them....
Zath
0
Zath
4/28/2004 1:07:55 PM
I assume the .ToString worked then?
In this world there are 10 types of people, those that know binary and those who don't.
0
optik
4/28/2004 1:10:56 PM
Yes it did........ the .ToString() worked...

<hits self over head> DOH......
Could not believe I missed that.....
Zath
0
Zath
4/28/2004 2:07:13 PM
Reply: