473,320 Members | 2,024 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.

System.InvalidCastException

Howdy!

Code
-----
string dtsSearchReq ="";
while (myReader.Read()) {
for(int j=0;j<myReader.FieldCount;j++) {
if (myReader.IsDBNull(i)) dtsSearchReq += myReader.GetString(i);

}
}

Background
----------
In the above code, I am using an OdbcDataReader to read the fields of
retrieved records. I am using a system DSN to connect to an MS-XL
spreadsheet.

There are 17 fields, 16 of which correspond to "string" datatype and 1
corresponds to "double". However, I would like to recieve all fields as
"string". One of the string fields in the database has a NULL value.

Problem
-------
When I use the IsDBNull check, myReader.GetString(i) fails at a field
with "double" datatype. System.InvalidCastException is raised. Note
that the "double" field contained a value of 1 and not a NULL.

When I do away with IsDBNull check, as expected, myReader.GetString(i)
fails when it encounters the NULL value. However, the "double" field
when encountered, is successfully converted to a corresponding text
representation (namely 1.00).

Summary: myReader.GetString(i) succesfully converts a "double" to
"string" in the absense of IsDBNull, but fails to do so in its
presence!

can anyone please throw some light?

Nov 17 '05 #1
5 3165
How about:

for(int j=0;j<myReader.FieldCount;j++)
{
object o = myReader.GetValue(j) ;
if ( o != DBNull.Value )
dtsSearchReq += o.ToString();
}

Nov 17 '05 #2
How about:

for(int j=0;j<myReader.FieldCount;j++)
{
object o = myReader.GetValue(j) ;
if ( o != DBNull.Value )
dtsSearchReq += o.ToString();
}

Nov 17 '05 #3
Hello Mohammad,

Your code works great! Thank you very much.

Any hint why mine was failing?

Nov 17 '05 #4
Kini <ki********@gmail.com> wrote:
Hello Mohammad,

Your code works great! Thank you very much.

Any hint why mine was failing?


Yes - you were calling GetString on a column which wasn't a string. You
should almost always just call the appropriate method based on the type
of the column.

By the way, it helps to post your *actual* code rather than typing it
in again - your initial posted code would have failed because it was
trying to call GetString on all null values rather than all non-null
values.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
Hello Jon,

Thank you for the answer and the suggestion.

Cordially, Kini

Nov 17 '05 #6

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

Similar topics

1
by: bob scola | last post by:
I have a csharp, VS 2003 solution for a winform application The application uses an object called a "matter" and the class is defined in matter.cs. I can load matter objects into a combobox ...
4
by: DOTNET | last post by:
Hi, Anybody help me regarding this error: I am assigning the values to the session variables when the button is clicked and passing these session variables to the next page and when I am...
0
by: QA | last post by:
I am using a Business Scorecard Accelarator in a Sharepoint Portal 2003 using SQL Server 2005 I am getting the following error: Error,5/7/2005 10:50:14 AM,580,AUE1\Administrator,"Specified cast is...
2
by: Michael | last post by:
Hello all and thanks in advance, I'm running a vb.net program and part of it copies files from one server to another. It generally works fine but once in a while I get this error: ...
0
by: JGKjr | last post by:
I have a Web Application written in C#. I am trying to call a web service method and recieve the error: System.InvalidCastException: Cannot assign object of type System.Boolean to an object of...
1
by: Marc | last post by:
Hi! I'm working with a C# client that calls a php web service. I've created a wrapper to call the service using .NET wsdl tool (adding a web reference). The call to the server works fine, it...
2
by: John Smith | last post by:
I'm writing webervice client using .Net 2.0. I have this class: public class MyWebService : SoapHttpClientProtocol { public XmlDocument validate(string url, XmlDocument xmlDocument) {...
8
by: Gamma | last post by:
I'm trying to inherit subclass from System.Diagnostics.Process, but whenever I cast a "Process" object to it's subclass, I encounter an exception "System.InvalidCastException" ("Specified cast is...
8
by: =?Utf-8?B?YXVsZGg=?= | last post by:
i trying to collect windows services. i'm getting inconsistencies. the collection works every time locally. but remote collection yields different result depending on my lab environment. 1) in...
4
by: rsdev | last post by:
Hi, I have an InvalidCastException which is completely puzzling me. I have checked all the members in the stored procedure against my data provider and seems to be ok. Also in the stack trace it...
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
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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: 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.