473,385 Members | 1,732 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.

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 3167
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...

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.