Cast from 'DBNull' to "string' is not valid

G

Guest

Hello
I'm new to this, and I'm not sure how to avoid the following error
I use the following code to pull data from a SQL database, depending on the choice selected in a listbox

Private Sub lBoxCP_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lBoxCP.SelectedIndexChange

dim xyz as DataRowView = lBoxCP.SelectedIte
txtCPMain.Text = xyz("STR_CUTTING_PERMIT"
txtOArea.Text = xyz("STR_OPERATING_AREA"

and so on..
this works great as long as there is data in the field, but if it's empty, I get an error "Cast from 'DBNull' to 'string' is not valid

How do I have VB.NET just leave it blank

Thanks
Ambe
 
H

Herfried K. Wagner [MVP]

* "=?Utf-8?B?YW1iZXI=?= said:
dim xyz as DataRowView = lBoxCP.SelectedItem
txtCPMain.Text = xyz("STR_CUTTING_PERMIT")

\\\
If Not xyz(...) Is DBNull.Value Then
...
End If
///
 
K

Ken Tucker [MVP]

Hi,

Try this.

txtCPMain.Text = xyz("STR_CUTTING_PERMIT").ToString
txtOArea.Text = xyz("STR_OPERATING_AREA").ToString

Ken
------------------
amber said:
Hello,
I'm new to this, and I'm not sure how to avoid the following error.
I use the following code to pull data from a SQL database, depending on
the choice selected in a listbox.
Private Sub lBoxCP_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles lBoxCP.SelectedIndexChanged
dim xyz as DataRowView = lBoxCP.SelectedItem
txtCPMain.Text = xyz("STR_CUTTING_PERMIT")
txtOArea.Text = xyz("STR_OPERATING_AREA")

and so on...
this works great as long as there is data in the field, but if it's empty,
I get an error "Cast from 'DBNull' to 'string' is not valid"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top