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

T

tshad

The error I am getting is:

*******************************************************************
Exception Details: System.InvalidCastException: Cast from type 'DBNull' to
type 'String' is not valid.

Source Error:

Line 144: firstName.text = ClientReader("firstName")
Line 145: lastName.text = ClientReader("lastName")
Line 146: middleName.text = ClientReader("middleName") <------
Line 147: fullName.text = ClientReader("fullName")
Line 148: address1.text = ClientReader("address1")
*******************************************************************

The code I am getting the error in is:

if ClientReader.Read then
applicantID.text = ClientReader("applicantID")
firstName.text = ClientReader("firstName")
lastName.text = ClientReader("lastName")
middleName.text = ClientReader("middleName")
fullName.text = ClientReader("fullName")
allowedWorkUS.text = ClientReader("allowedWorkUS")
end if

How would I best do this? Do I have to check for null for each of my
variables everytime I read a record?

Thanks,

Tom.
 
D

Daniel Fisher\(lennybacon\)

ClientReader.GetValue(0).ToString()
is not so nice cause you set the num of th column and not the name, but
works.
 
M

Mythran

tshad said:
The error I am getting is:

*******************************************************************
Exception Details: System.InvalidCastException: Cast from type 'DBNull' to
type 'String' is not valid.

Source Error:

Line 144: firstName.text = ClientReader("firstName")
Line 145: lastName.text = ClientReader("lastName")
Line 146: middleName.text = ClientReader("middleName") <------
Line 147: fullName.text = ClientReader("fullName")
Line 148: address1.text = ClientReader("address1")
*******************************************************************

The code I am getting the error in is:

if ClientReader.Read then
applicantID.text = ClientReader("applicantID")
firstName.text = ClientReader("firstName")
lastName.text = ClientReader("lastName")
middleName.text = ClientReader("middleName")
fullName.text = ClientReader("fullName")
allowedWorkUS.text = ClientReader("allowedWorkUS")
end if

How would I best do this? Do I have to check for null for each of my
variables everytime I read a record?

Thanks,

Tom.

You can create a class that inherits whatever object type ClientReader is
and do the dbnull checks inside there or check for nulls each time you
access an item that can be null...

What we did is created a method in our "common" module that looks similar to
the following:

Public Function GetFieldValue(ByVal Row As DataRow, ByVal FieldName As
String) As String
If Row.IsNull(FieldName)
Return String.Empty
Else
Return CStr(Row(FieldName))
End If
End Function

We then call this function whenever we need to get a database value (note,
we use typed data sets but the above takes an untyped data row, we figured
we didn't want to write a new GetFieldValue for each dataset we have so we
created this generic one...).

To call it, we would use middleName.Text =
Common.GetFieldValue(ClientReader, "middleName"). That is, if ClientReader
was a data row (looks to me like it's a DataReader).

Ours does a lot more than the above, but for your purposes, you can build on
that.

Mythran
 
K

Karl Seguin

firstName.Text = GetStringFromReader("firstName", clientReader,
String.Empty)

public static function(byval columnName as string, byval dr as IDataRecord,
byval defaultValue as string) as string
if dr(columnName) is DBNull.Value then
return defaultValue
end if
return Convert.ToString(dr(columnName))
end function


..net 2.0 will support nullable types...yay!

Karl
 
I

Isaias Formacio-Serna

I would say yes, check if they are not DBNull before asigning them, but only
in those records you know that could have a null (Allow nulls).

-IFS
 
T

tshad

Mythran said:
You can create a class that inherits whatever object type ClientReader is
and do the dbnull checks inside there or check for nulls each time you
access an item that can be null...

What we did is created a method in our "common" module that looks similar
to the following:

Public Function GetFieldValue(ByVal Row As DataRow, ByVal FieldName As
String) As String
If Row.IsNull(FieldName)
Return String.Empty
Else
Return CStr(Row(FieldName))
End If
End Function

We then call this function whenever we need to get a database value (note,
we use typed data sets but the above takes an untyped data row, we figured
we didn't want to write a new GetFieldValue for each dataset we have so we
created this generic one...).

To call it, we would use middleName.Text =
Common.GetFieldValue(ClientReader, "middleName"). That is, if
ClientReader was a data row (looks to me like it's a DataReader).

Yes, it is a DataReader.

But I tried to use your function, with and without the public and I get an
error:

***********************************************************
Compiler Error Message: BC30182: Type expected.

Source Error:

Line 36: End Sub
Line 37:
Line 38: Function GetFieldValue(ByVal Row As DataRow, ByVal FieldName As
Line 39: String) As String
Line 40: If Row.IsNull(FieldName)
***********************************************************

Am I missing something? At the moment the function is in my page as just a
function. Is "DataRow" causing a problem?

Thanks,

Tom
Function GetFieldValue(ByVal Row As DataRow, ByVal FieldName As
String) As String
If Row.IsNull(FieldName)
Return String.Empty
Else
Return CStr(Row(FieldName))
End If
End Function
 
T

tshad

tshad said:
Yes, it is a DataReader.

But I tried to use your function, with and without the public and I get an
error:

Nevermind this. I found the problem - when I copied the file, I didn't
notice that part of the function statement went to the next line.

I am, however, getting an error:

**************************************************************
Compiler Error Message: BC30311: Value of type
'System.Data.SqlClient.SqlDataReader' cannot be converted to
'System.Data.DataRow'.

Source Error:

Line 152: firstName.text = ClientReader("firstName")
Line 153: lastName.text = ClientReader("lastName")
Line 154: middleName.text = GetFieldValue(ClientReader,"middleName")
Line 155: fullName.text = ClientReader("fullName")
Line 156: address1.text = ClientReader("address1")
***************************************************************

Tom
 

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

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top