473,385 Members | 1,343 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,385 software developers and data experts.

Conversion from type 'DBNull' to type 'String' is not valid

Hi again,

I want to read all the records of a table with 2 fields.
The problem is that some records have null value in the second field.
This code below works when all records have both fields fiilled, but gives
the error:
"Conversion from type 'DBNull' to type 'String' is not valid" when not.

.....
comd = New System.Data.OleDb.OleDbCommand("select name,lok from pc",
oConnection)
dtreader = comd.ExecuteReader
for i=0 to 41
dtreader.Read()
for j=0 to 1
f=dtreader.item(j)
Response.Write(f)
next
Response.Write("<br>")
next
dtreader.Close()

Thanks for hints.
Chris
May 10 '06 #1
2 7013

You can either check for

IsDBNULL before setting it.

if ( !myDataReader.IsDBNull ( 0 ) )
{
string x = myDataReader.GetString(0);
}
Or you can find the SafeDataReader .. a class to help with this.
(google it, but its in vb.net)

"Chris" <cc*@sdsd.dc> wrote in message
news:uW**************@TK2MSFTNGP05.phx.gbl...
Hi again,

I want to read all the records of a table with 2 fields.
The problem is that some records have null value in the second field.
This code below works when all records have both fields fiilled, but gives
the error:
"Conversion from type 'DBNull' to type 'String' is not valid" when not.

....
comd = New System.Data.OleDb.OleDbCommand("select name,lok from pc",
oConnection)
dtreader = comd.ExecuteReader
for i=0 to 41
dtreader.Read()
for j=0 to 1
f=dtreader.item(j)
Response.Write(f)
next
Response.Write("<br>")
next
dtreader.Close()

Thanks for hints.
Chris

May 10 '06 #2
Thanks

"sloan" <sl***@ipass.net> wrote in message
news:e4**************@TK2MSFTNGP04.phx.gbl...

You can either check for

IsDBNULL before setting it.

if ( !myDataReader.IsDBNull ( 0 ) )
{
string x = myDataReader.GetString(0);
}
Or you can find the SafeDataReader .. a class to help with this.
(google it, but its in vb.net)

"Chris" <cc*@sdsd.dc> wrote in message
news:uW**************@TK2MSFTNGP05.phx.gbl...
Hi again,

I want to read all the records of a table with 2 fields.
The problem is that some records have null value in the second field.
This code below works when all records have both fields fiilled, but gives the error:
"Conversion from type 'DBNull' to type 'String' is not valid" when not.

....
comd = New System.Data.OleDb.OleDbCommand("select name,lok from pc",
oConnection)
dtreader = comd.ExecuteReader
for i=0 to 41
dtreader.Read()
for j=0 to 1
f=dtreader.item(j)
Response.Write(f)
next
Response.Write("<br>")
next
dtreader.Close()

Thanks for hints.
Chris


May 11 '06 #3

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

Similar topics

22
by: Christoph Boget | last post by:
I am getting an error (a few among many) for the following lines of code: retval.BrokerName = (( curRow == System.DBNull.Value ) ? SqlString.Null : (string)curRow ); retval.BrokerGroupId = ((...
7
by: | last post by:
Source Error: Line 173: sData(rownumber - 1, lcnt) = WhatCol.Value Line 174: End IF Line 175: If (sData(rownumber, lcnt) = sData(rownumber - 1, lcnt)) AND...
6
by: tshad | last post by:
The error I am getting is: ******************************************************************* Exception Details: System.InvalidCastException: Cast from type 'DBNull' to type 'String' is not...
10
by: Bob | last post by:
I'm sure there's a good reason, I just haven't been able to think of it - why didn't MS allow "DBNull" values to simply be a null (Nothing)? Bob
2
by: ibiza | last post by:
Hi all, I'm using a business logic layer as described in this source code : ...
4
by: SAL | last post by:
I'm sorry if this has been answered before but I didn't see it in a quick scan of the list. The following code is causing an error when the field is null: Error text: Conversion from type...
4
by: Michel Vanderbeke | last post by:
Hello, When reading data from a SqlClient.SqlDataReader in VB.NET 2005, I want to display fiels that sometimes are empty. sUser = CStr(rdUser("Name") & Space(1) & CStr(rdUser("First_Name") ...
2
by: Cirene | last post by:
I'm setting a databound label's backcolor and forecolor like this... <asp:Label ID="lblSampleColorScheme" runat="server" BackColor='<%#...
3
by: Sep410 | last post by:
Hi all, Please help me in this code: txtLastNameFM.Text = IIf(dgFamilyMember.CurrentRow.Cells(3).Equals(System.DBNull.Value), "", dgFamilyMember.CurrentRow.Cells(3).Value) this is an error I...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.