Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Casting query

Status
Not open for further replies.

MattWoberts

Programmer
Oct 12, 2001
156
GB
Hi,

Please can anyone help - I am going mad!

I have a dataReader, and am itterating through it and building a string for each item. The datareader contains 2 columns, one is a number and the other is a text column.

My code was fine, but all of a sudden refuses to work - complaining about invalid casts. The code looks like:

sString = r.GetString(0) + " - " + r.GetString(1);

Is it really invalid to cast a Int32 into a string? Surely not? This code all seemed to work fine for me yesteray.

Also, I get errors on other areas in the code that work if I trace through them, but give an invalid CAST if I just hit F5 and allow the program to continue. Am I going mad?

Any help greatly appreciated!!!!
 
Remarks:
"No conversions are performed, therefore the data retrieved must already be a 32-bit signed integer."

You should write:
r.GetInt32(0) + " - " + r.GetString(1);
if the first column is an integer.

-obislavu-



 
obislavu is correct - you should use the method to get the value from the dataReader that matches the datatype as defined in the database.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Matt,

No, you are not "going mad", although it can seem that way when something that was working one day stops working the next. There is something changing on you, but it isn't your sanity.

Adding to the confusion, the cast problem very well could be on the string column. It isn't necessarily on the int column. (Most drivers know how to convert int to string.) I better start explaining what's going on before you start thinking *I'm* insane.

The problem probably appeared over night because you started looking at some different data on the second day. The problem is probably nulls in your data. If your database column isn't flagged to not allow nulls, you can have nulls in the columns in the rows returned by your reader. The nulls will appear as references to System.Data.DbNull (a singleton used to mark null tuples).

To get around the problem, either (if applicable) mark the columns as not allowing null in the database, or (more generally) check for null before you try retrieving your values using IsDBNull(columnNumber) before tring to retrieve the value. If IsDBNull returns true, you can substitute your own string (like <null>) or do something else appropriate to your application.

One other problem you mentioned in your post is different exceptions visible via the debugger versus what you see when you press F5. Check carefully exactly what you are seeing. You may be seeing caught exceptions when you single step the app. When you let it rip with F5, you are only going to see the uncaught exceptions that bubble to the surface.
 
Thanks everyone for the replies - you have restored my faith in .NET and I dont think I am going mad anymore. It was a combination of my code and DBNulls causing the problems I was having, all of which I think is sorted now :)

I do though have one more related question which has really stumped me:

One of my datareaders returns 2 &quot;int&quot; columns from the table, col1, and col2. They both have data (no nulls), and when I check their type by looking at reader.GetDataTypeName(x) they say they are &quot;int&quot;. But.... if I do reader.GetInt32(x) then one of them works, but the other - which is holding the number 1, fails with an invalid cast!?!!?

I can however do a GetString on this one, so am currently working around with:

Convert.ToInt32(rdr.GetValue(0))

Can anyone shed some light?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top