Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DBNull Problem

Status
Not open for further replies.

IndyGill

Technical User
Jan 15, 2001
191
GB
Hi

I am having a problem with nulls in a database field. I have an object which produces a dataset, to which I then select a specific datarow. I want to check if a field in that datarow is null. However I keep getting this error "Cast from type 'DBNull' to type 'String' is not valid."

This is my code

Private Sub checkForNull(ByVal strArID As String)
rw = a.GetRow(strArID) 'this gets a specific row
If IsDBNull(rw.Area_ArID) Then
lable1.text = "DB is Null"
Else
label1.text = "DB has a value")
End If
End Sub

Thanks in advance
 
What are the types of [tt]a[/tt] and [tt]rw[/tt]? (I don't know of a database-related class with a [tt]GetRow[/tt] function, offhand.)
 
rw is row from a datatable
a loads a dataset of

the row (rw) has several fields I know that some of them have a null value. So I select the rw.fieldname to select the value. But when I try write it out I get the "Cast from type 'DBNull' to type 'String' is not valid."
 
You can use a strong typed datarow. For each column, Foo, there exists a property, IsFooNull.

In your example:

If Not rw.IsArea_ArIDNull then
'do this
else
'do that
end if
 
The way that I always checked if a value was null was:

[tt]If rw.Area_ArID Is DBNull.Value Then[/tt]

It seems to me that if it was doing a type conversion before, it might still have to for that. It's worth trying, though.

In case that doesn't help, do you know the actual, full, data types for [tt]a[/tt] and [tt]rw[/tt]? I understand that [tt]rw[/tt] is a row of data, but I want to look at the docs for the class.
 
You can try
Code:
if(Convert.IsDbNull(somevalue))
 
I use the following function for converting null values:
Code:
Private Function DeNull(ByVal ThisValue As System.Object) As System.Object
     Return IIf(ThisValue Is DBNull.Value, "", ThisValue)
End Function

and an example of how i use it is:
Code:
Subject.Text = DeNull(Dr("Subject"))
where Dr is the name of my SQLDataReader

Hope that helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top