473,506 Members | 17,176 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7021

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
27838
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
6250
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
5406
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
2397
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
2414
by: ibiza | last post by:
Hi all, I'm using a business logic layer as described in this source code : ...
4
6221
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
6274
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
352
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
3226
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
7218
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7103
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7307
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7021
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7478
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5035
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4701
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
409
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.