Connecting Tech Pros Worldwide Help | Site Map

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

tshad
Guest
 
Posts: n/a
#1: Nov 19 '05
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.


Daniel Fisher\(lennybacon\)
Guest
 
Posts: n/a
#2: Nov 19 '05

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


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


--
Daniel Fisher(lennybacon)
MCP ASP.NET C#
Blog: http://www.lennybacon.com/


"tshad" <tscheiderich@ftsolutions.com> wrote in message
news:%232b0YNs4EHA.1524@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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.
>[/color]


Mythran
Guest
 
Posts: n/a
#3: Nov 19 '05

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


"tshad" <tscheiderich@ftsolutions.com> wrote in message
news:%232b0YNs4EHA.1524@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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.
>[/color]

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



Karl Seguin
Guest
 
Posts: n/a
#4: Nov 19 '05

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


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

--
MY ASP.Net tutorials
http://www.openmymind.net/


"Daniel Fisher(lennybacon)" <info@(removethis)lennybacon.com> wrote in
message news:uFmwjRs4EHA.3820@TK2MSFTNGP11.phx.gbl...[color=blue]
> ClientReader.GetValue(0).ToString()
> is not so nice cause you set the num of th column and not the name, but
> works.
>
>
> --
> Daniel Fisher(lennybacon)
> MCP ASP.NET C#
> Blog: http://www.lennybacon.com/
>
>
> "tshad" <tscheiderich@ftsolutions.com> wrote in message
> news:%232b0YNs4EHA.1524@TK2MSFTNGP09.phx.gbl...[color=green]
> > The error I am getting is:
> >
> > ************************************************** *****************
> > Exception Details: System.InvalidCastException: Cast from type 'DBNull'[/color][/color]
to[color=blue][color=green]
> > 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.
> >[/color]
>
>[/color]


Isaias Formacio-Serna
Guest
 
Posts: n/a
#5: Nov 19 '05

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


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

"tshad" <tscheiderich@ftsolutions.com> wrote in message
news:%232b0YNs4EHA.1524@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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.
>[/color]


tshad
Guest
 
Posts: n/a
#6: Nov 19 '05

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


"Mythran" <kip_potter@hotmail.comREMOVETRAIL> wrote in message
news:Oux4oWs4EHA.2192@TK2MSFTNGP14.phx.gbl...[color=blue]
> "tshad" <tscheiderich@ftsolutions.com> wrote in message
> news:%232b0YNs4EHA.1524@TK2MSFTNGP09.phx.gbl...[color=green]
>> 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.
>>[/color]
>
> 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).[/color]

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
[color=blue]
>
> Ours does a lot more than the above, but for your purposes, you can build
> on that.
>
> Mythran
>
>
>[/color]


tshad
Guest
 
Posts: n/a
#7: Nov 19 '05

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


"tshad" <tscheiderich@ftsolutions.com> wrote in message
news:eXzgGhs4EHA.3092@TK2MSFTNGP10.phx.gbl...[color=blue]
> "Mythran" <kip_potter@hotmail.comREMOVETRAIL> wrote in message
> news:Oux4oWs4EHA.2192@TK2MSFTNGP14.phx.gbl...[color=green]
>> "tshad" <tscheiderich@ftsolutions.com> wrote in message
>> news:%232b0YNs4EHA.1524@TK2MSFTNGP09.phx.gbl...[color=darkred]
>>> 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.
>>>[/color]
>>
>> 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).[/color]
>
> Yes, it is a DataReader.
>
> But I tried to use your function, with and without the public and I get an
> error:[/color]

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
[color=blue]
>
> ************************************************** *********
> 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
>[color=green]
>>
>> Ours does a lot more than the above, but for your purposes, you can build
>> on that.
>>
>> Mythran
>>
>>
>>[/color]
>
>[/color]


Closed Thread