473,657 Members | 2,654 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.Ole Db.OleDbCommand ("select name,lok from pc",
oConnection)
dtreader = comd.ExecuteRea der
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 7027

You can either check for

IsDBNULL before setting it.

if ( !myDataReader.I sDBNull ( 0 ) )
{
string x = myDataReader.Ge tString(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******** ******@TK2MSFTN GP05.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.Ole Db.OleDbCommand ("select name,lok from pc",
oConnection)
dtreader = comd.ExecuteRea der
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.ne t> wrote in message
news:e4******** ******@TK2MSFTN GP04.phx.gbl...

You can either check for

IsDBNULL before setting it.

if ( !myDataReader.I sDBNull ( 0 ) )
{
string x = myDataReader.Ge tString(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******** ******@TK2MSFTN GP05.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.Ole Db.OleDbCommand ("select name,lok from pc",
oConnection)
dtreader = comd.ExecuteRea der
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
27873
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 = (( curRow == System.DBNull.Value ) ? SqlInt32.Null : (int)curRow ); The errors are (in turn):
7
6261
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 (WhatCol.Type <> 5 AND WhatCol.Type <> 6) Then Line 176: If rownumber = 3 Then Line 177: thisfield = WhatCol.Value
6
5430
by: tshad | last post by:
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")
10
2412
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
2420
by: ibiza | last post by:
Hi all, I'm using a business logic layer as described in this source code : http://www.asp.net/QuickStart/util/srcview.aspx?path=~/aspnet/samples/data/GridViewObject.src&file=GridViewObject_cs\App_Code\AuthorsComponent.cs&lang=C%23+Source It works very well until I encountered a small problem recently. When it is time to bind the data from the db to my class representing an "author" (let's say, for simplicity), what about if my row...
4
6225
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 'DBNull' to type 'Date' is not valid. <asp:Calendar ID="Calendar2" runat="server" SelectedDate='<%# Bind("DateReceived") %>' VisibleDate='<%# Eval("DateReceived") %>'></asp:Calendar>
4
6280
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") When the First_Name of a user is empty, I get an error telling me type DBNull cannot be converted to String. Is there a way of solving this problem?
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='<%# System.Drawing.Color.FromName(Eval("AttachmentEntityBackColor")) %>' Font-Bold="True" ForeColor='<%# System.Drawing.Color.FromName(Eval("AttachmentEntityForeColor")) %>'
3
3240
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 get: Conversion from type 'DBNull' to type 'String' is not valid. I know the value of the dgFamilyMember.CurrentRow.Cells(3).Value= null but the first part of the iif returns false! What is wrong here?
0
8394
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8306
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7327
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6164
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.