Monday, March 12, 2012

How to handle DBNull DataType for Ms access ??

Hi,

I have problem in using this function to check if the field is empty or null :

Let say , I have connect to a Database and set up Dataset :

Dim Ds as dataset

Dim StrAddr as string

If IsDBNull( DS.tables("tblName").rows(0).item("Address")) then

LbMsg.text =" No address entered."

else

StrAddr = DS.tables("tblName").rows(0).item("Address"))

end if

I have error msg : Can not cast DBNull to string.

All I wanted is to check the field address to see if it is empty or no data entered.

Please help.

How to check ? How to handle if the field has nothing in it at all. How to use IsDBNull() ?

Thanks in advance.

Try this:

StrAddr = DS.tables("tblName").rows(0).item("Address").ToString

If StrAddr ="" then StrAddr = "No Address Entered"

Tim

Labels: , , , , , , , , , , , , ,

How to handle DBNull

As i write some functions to return string or dataset from database, there is a chance of return DBNULL.

Even i checked the dr(0) with Nothing, It still get Error
dim dr as sqldatareader = xxx.executeQuery
if dr.Read() then
if dr(0) <> Nothing Then
dim abc as string = dr(0)
end if
end if

How can i solve this?
Thx a lotsThere is ISDBNull function in VB and also isDBNull method in SqldataReader, which you can use to check if field is null. If field is null (DBNull), you should set the member/variable you set based on this field, so that its value clearly indicates it has not been set, for example with string it could be String.Empty.
thx for your quick respone

Labels: , , , , , , , , , ,