473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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.
Nov 19 '05 #1
6 5393
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" <ts**********@ftsolutions.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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.

Nov 19 '05 #2
"tshad" <ts**********@ftsolutions.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

Nov 19 '05 #3
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:uF**************@TK2MSFTNGP11.phx.gbl...
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" <ts**********@ftsolutions.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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.


Nov 19 '05 #4
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" <ts**********@ftsolutions.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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.

Nov 19 '05 #5
"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:Ou**************@TK2MSFTNGP14.phx.gbl...
"tshad" <ts**********@ftsolutions.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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

Nov 19 '05 #6
"tshad" <ts**********@ftsolutions.com> wrote in message
news:eX**************@TK2MSFTNGP10.phx.gbl...
"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:Ou**************@TK2MSFTNGP14.phx.gbl...
"tshad" <ts**********@ftsolutions.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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


Nov 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dave Byron | last post by:
I am having trouble with DBNull's from my SQL server. I am building/converting a asset web app from Access and my db has nulls on various fields . I have tried searching newsgroups and tried to get...
1
by: Ruslan Shlain | last post by:
A little problem I am having and its driving me nuts. I have a datatable and I am adding a column to it on the fly. Then based on some other fields i add or not add a value to that column for each...
5
by: .Net Sports | last post by:
I have a datagrid codebehind script that takes data from sql dbase and displays it in a footer row as a total. One column has amount_dollars (which works fine), while another has new sales (which...
3
by: PK9 | last post by:
I am looking for assistance in pinpointing the cause of the following exception. I am getting a "Specified Cast is not valid" exception on my page. I am trying to populate a datagrid. One of my...
2
by: Fabian | last post by:
Hi, I work with asp.net 2.0 and I have a intermittent error, only happens a few times a day. In the page I evaluate a Query String and then I get data form a database. The code snipped: ...
2
by: amber | last post by:
Hello I'm new to this, and I'm not sure how to avoid the following error I use the following code to pull data from a SQL database, depending on the choice selected in a listbox Private Sub...
10
by: roy.anderson | last post by:
Error is thus: "Cast from type 'DBNull' to type 'String' is not valid." Simple, right? Well, no (or at least not for me). Here's a function, followed by the calling code below: Function...
1
by: amjad | last post by:
System.InvalidCastException: Specified cast is not valid. hi i am received that error below is my code result = new CustID(Convert.ToString(reader), Line 57:...
2
by: DMG | last post by:
Why does this fail with a cast exception? The field is coming in from the data base as null due some bad design on my part but I thought this would allow me to test and set to false. ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.