Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I'm getting a bizarre error in my code, and I was wondering if somebody knew what I could look for to fix it

This is the line that's erroring. record is a DataTable, Rows(0) is checked to be valid, and fcurr is declared as a String
VB
If IsDBNull(record.Rows(0).Item("fcurr")) Then fcurr = Nothing Else fcurr = record.Rows(0).Item("fcurr")


System.InvalidCastException was unhandled
  Message="Specified cast is not valid."

  Source="TestApplication"

  StackTrace:
       at TestApplication.SalesOrders.OrderHeader.Load(SqlConnection dbServer, SqlTransaction dbTrans, Int32 OrderNumber) in C:\MarqDev\TestApplication\SalesOrders\OrderHeader.vb:line 530
       at TestApplication.DevTools.OrderFormTest.Button1_Click(Object sender, EventArgs e) in C:\MarqDev\TestApplication\TestApplication.DevTools\OrderFormTest.vb:line 11

  InnerException: 


I don't understand, because there's no casting being done, fcurr is populated with the string "EUR", and if I press F5 to continue to the code, it continues on the same line without generating the exception.

I'm seeing this occur a couple of times on similar lines. Happens in debug mode, and also in a Release compile

Does anybody have any idea what I can look at? As I said, if I continue the code from the breakpoint, it carries on without a problem

Edit:
That also occurs on Integer and Double fields.

Edit 2:
The DataTable is constructed from a SqlDataAdapter querying a table from an SQL Server

Edit 3:
Ahhh, actually, whilst the debugger is breaking on this line, this isn't actually the line causing the problem. I think I know what it is now, and it's related to the data type of the previous field
Posted
Updated 25-Apr-12 4:33am
v4

1 solution

If you're using an untyped datatable or are accessing values over the items collection, the values will be returned as object, so the problem is that you're trying to assign an object value to a string variable, hence the invalidcastexception. Fix it by adding a ToString() call:

VB
If IsDBNull(record.Rows(0).Item("fcurr")) Then fcurr = Nothing Else fcurr = record.Rows(0).Item("fcurr").ToString()
 
Share this answer
 
Comments
MarqW 25-Apr-12 10:03am    
I made the change you suggested. I'm afraid the exception raises on the "If IsDBNull(record.Rows(0).Item("fcurr")) Then" bit and the ToString makes no difference.

I can't ToString() the first bit, because that would make the IsDBNull() fail everytime. Also, it's happening on fields that are integers etc. as well :(
jim lahey 25-Apr-12 10:32am    
OK, I understand. Mybe try If record.Rows(0).Item("fcurr") is Nothing instead - my VB isn't up to much these days :)
MarqW 25-Apr-12 10:40am    
DBNull.Value is not the same as nothing, which is what this line is trying to check. As I've just said in my edit, it's apparently nothing to do with this line, and is the line above (looks like the debugger was stopping on the wrong line for me). So I think I can get this sorted now.

Thanks anyway

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900