PC Review


Reply
Thread Tools Rate Thread

Cast from type 'DBNull' to type 'String' is not valid. when creating dataset

 
 
troutbum
Guest
Posts: n/a
 
      9th Mar 2004
I am creating a dataset by using a stored procedure. When I bind the
dataset to the datagrid I get the following Error "Cast from type
'DBNull' to type 'String' is not valid." I used a sqlreader to verify
that the field was returning "", as per my stored procedure
IsNull(field, '') as Field returned. All the other fields returned
are integers and have a values associated with them as well. I tried
to run the debugger to see if the dataset is empty, but it doesn't
appear to be. I also checked the datagrid columns and stored
procedure fields to also make sure they match. Thanks for any
direction.
Troutbum
 
Reply With Quote
 
 
 
 
William Ryan eMVP
Guest
Posts: n/a
 
      9th Mar 2004
Troutbum:

I'm kind of confused about the sequence of events. When you say "creating a
dataset by using a stored procedure" I'm assuming you are filling it and
it's already been created. However, where does the datareader come in?
What line of code is throwing the exceptiion something like
myGrid.DataSource = theDataSet?

Can you post the code snippet, it'd probably be easier to track down then.
"troutbum" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I am creating a dataset by using a stored procedure. When I bind the
> dataset to the datagrid I get the following Error "Cast from type
> 'DBNull' to type 'String' is not valid." I used a sqlreader to verify
> that the field was returning "", as per my stored procedure
> IsNull(field, '') as Field returned. All the other fields returned
> are integers and have a values associated with them as well. I tried
> to run the debugger to see if the dataset is empty, but it doesn't
> appear to be. I also checked the datagrid columns and stored
> procedure fields to also make sure they match. Thanks for any
> direction.
> Troutbum



 
Reply With Quote
 
Kevin Yu [MSFT]
Guest
Posts: n/a
 
      10th Mar 2004
Hi Troutbum,

Thank you for posting in the community!

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you received an error message which
says "Cast from type 'DBNull' to type 'String' is not valid." when binding
the DataSet to a DataGrid. If there is any misunderstanding, please feel
free to let me know.

Based on my experience, this issue might be caused by many reasons. Please
try to set breakpoints in your code and step into the program to see if the
exception was thrown exactly when binding to the data source. Could you
please also paste some of your code, the database table definition and the
stored procedure here? It might be much easier for us to find the problem
with code, and we can deliver our assistance more quickly.

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

 
Reply With Quote
 
Guinness Mann
Guest
Posts: n/a
 
      11th Mar 2004
On 9 Mar 2004 05:32:02 -0800, (E-Mail Removed) (troutbum) wrote:

> I used a sqlreader to verify
> that the field was returning "", as per my stored procedure
> IsNull(field, '') as Field returned.


Here's how I deal with that pesky "DBNull is not a string" problem:

public static string executeStrScalar(string sSQL)
{
string tmp = "";
object obj = new object(); <--- HERE

try
{
HERE ---> obj = SqlHelper.ExecuteScalar
(strCon, CommandType.Text, sSQL);
HERE ---> if (obj != null)
tmp = Convert.ToString(obj);
}
catch(Exception e)
{
string s = String.Format
( "AccessDistractors:getDistractorTextForId: "
+ "Unexpected database error {0}.\n"
+ "SQL = {1}."
, e.Message
, sSQL
);
throw new ApplicationException(s);
}
return tmp;
}

By setting the return from the DAAB equal to a plain old object, I get
the chance to check it for null and *then* convert it to a string.

-- Rick

 
Reply With Quote
 
Kevin Yu [MSFT]
Guest
Posts: n/a
 
      13th Mar 2004
Hi Troutbum,

I'd like to know if this issue has been resolved yet. Is there anything
that I can help. I'm still monitoring on it. If you have any questions,
please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

 
Reply With Quote
 
gpig
Guest
Posts: n/a
 
      13th Mar 2004
sql datareader: do you read the data manually using the
"while sqldatareader.read" ? Then you should use "If NOT
sqldatareader.isDBNull(columnindex) THEN your variable =
sqldatareader.getstring(columnindex)


Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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
Conversion from type 'DBNull' to type 'String' is not valid Cirene Microsoft ASP .NET 2 19th Jun 2008 06:18 AM
Cast from type 'DBNull' to type 'String' is not valid. tshad Microsoft ASP .NET 6 15th Dec 2004 05:10 PM
Cast from type 'DBNull' to type 'String' is not valid. =?Utf-8?B?RGF2ZSBCeXJvbg==?= Microsoft Dot NET 3 3rd Sep 2004 07:15 AM
Re: Cast from type 'DBNull' to type 'Boolean' is not valid Mike Newton Microsoft ASP .NET 0 27th Jul 2004 09:41 PM
Cast from type 'DBNull' to type 'String' is not valid. Elmo Watson Microsoft ASP .NET 3 25th Dec 2003 03:30 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:35 AM.