Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Cast from type 'DBNull' to type 'String' is not valid.

Reply
Thread Tools

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

 
 
tshad
Guest
Posts: n/a
 
      12-15-2004
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.


 
Reply With Quote
 
 
 
 
Daniel Fisher\(lennybacon\)
Guest
Posts: n/a
 
      12-15-2004
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" <> wrote in message
news:%...
> 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.
>



 
Reply With Quote
 
 
 
 
Mythran
Guest
Posts: n/a
 
      12-15-2004
"tshad" <> wrote in message
news:%...
> 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



 
Reply With Quote
 
Karl Seguin
Guest
Posts: n/a
 
      12-15-2004
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:...
> 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" <> wrote in message
> news:%...
> > 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.
> >

>
>



 
Reply With Quote
 
Isaias Formacio-Serna
Guest
Posts: n/a
 
      12-15-2004
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" <> wrote in message
news:%...
> 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.
>



 
Reply With Quote
 
tshad
Guest
Posts: n/a
 
      12-15-2004
"Mythran" <> wrote in message
news:...
> "tshad" <> wrote in message
> news:%...
>> 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).


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

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



 
Reply With Quote
 
tshad
Guest
Posts: n/a
 
      12-15-2004
"tshad" <> wrote in message
news:...
> "Mythran" <> wrote in message
> news:...
>> "tshad" <> wrote in message
>> news:%...
>>> 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).

>
> 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

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

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Cast from type 'DBNull' to type 'Boolean' is not valid Mike Newton ASP .Net 0 07-27-2004 08:41 PM
malloc - to cast or not to cast, that is the question... EvilRix C Programming 8 02-14-2004 12:08 PM
to cast or not to cast malloc ? MSG C Programming 38 02-10-2004 03:13 PM
Cast from type 'DBNull' to type 'String' is not valid. Elmo Watson ASP .Net 3 12-25-2003 03:30 AM
Cast from type 'DBNull' to type 'String' is not valid error Rob ASP .Net Datagrid Control 0 07-28-2003 09:11 PM



Advertisments