Invalid cast when trying to reference a database column...

  • Thread starter Thread starter chance
  • Start date Start date
C

chance

Getting and error on this first line of code:


//inspector phone
theField = reader.GetString(20);
builder.MoveToBookmark("inspector_phone");
builder.Write(theField);

the first line is crashing my program. it complains about an invalid
cast. i don't understand why though. I tried putting a IsDBNull around
it (thinking the data might be null) but that does not seem to help.
the field is declared as a string and reader is a connection to my
Oracle database.

Any help appreciated.
 
Instead of calling GetString, call GetObject, and look at the value
returned in the debugger (you will obviously have to set up a variable of
type object to hold the result).

You should see what the type is that is returned, and it should help you
with figuring out what the problem is.

Hope this helps.
 
chance said:
Getting and error on this first line of code:


//inspector phone
theField = reader.GetString(20);

What column number is theField in the reader?
 
Hi,

chance said:
Getting and error on this first line of code:


//inspector phone
theField = reader.GetString(20);
builder.MoveToBookmark("inspector_phone");
builder.Write(theField);

What type is the field?

Personally I prefer to use the indexing capability of the SqlDataReader (I
assume you are using it).
The above line I would had written it like:

string field = reader["ColumnName"].ToString();
 
Well I change data providers to Oracle.NET. It seems to be a little
more informative. What is happening (at least on the date fields) is I
am getting invalid system cast, cannot convert datetime to string. Not
sure how I should write this code.

Here is what I have:

theField =
(String)custRow["NOTICE_OF_VIOLATION_DATE"];
builder.MoveToBookmark("date_served");
builder.Write(theField);

Any help appreciated.




Instead of calling GetString, call GetObject, and look at the value
returned in the debugger (you will obviously have to set up a variable of
type object to hold the result).

You should see what the type is that is returned, and it should help you
with figuring out what the problem is.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




Getting and error on this first line of code:
//inspector phone
theField = reader.GetString(20);
builder.MoveToBookmark("inspector_phone");
builder.Write(theField);
the first line is crashing my program. it complains about an invalid
cast. i don't understand why though. I tried putting a IsDBNull around
it (thinking the data might be null) but that does not seem to help.
the field is declared as a string and reader is a connection to my
Oracle database.
Any help appreciated.- Hide quoted text -

- Show quoted text -
 
Ok, but if you did what I mentioned originally, you will see exactly the
type that the provider is offering, and then you will know WHY it's an
invalid cast.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

chance said:
Well I change data providers to Oracle.NET. It seems to be a little
more informative. What is happening (at least on the date fields) is I
am getting invalid system cast, cannot convert datetime to string. Not
sure how I should write this code.

Here is what I have:

theField =
(String)custRow["NOTICE_OF_VIOLATION_DATE"];
builder.MoveToBookmark("date_served");
builder.Write(theField);

Any help appreciated.




Instead of calling GetString, call GetObject, and look at the value
returned in the debugger (you will obviously have to set up a variable of
type object to hold the result).

You should see what the type is that is returned, and it should help
you
with figuring out what the problem is.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




Getting and error on this first line of code:
//inspector phone
theField = reader.GetString(20);
builder.MoveToBookmark("inspector_phone");
builder.Write(theField);
the first line is crashing my program. it complains about an invalid
cast. i don't understand why though. I tried putting a IsDBNull around
it (thinking the data might be null) but that does not seem to help.
the field is declared as a string and reader is a connection to my
Oracle database.
Any help appreciated.- Hide quoted text -

- Show quoted text -
 
Yes. if I use ToString on all the non-String fields everything works
fine. Problem solved.

thanks.

Ok, but if you did what I mentioned originally, you will see exactly the
type that the provider is offering, and then you will know WHY it's an
invalid cast.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




Well I change data providers to Oracle.NET. It seems to be a little
more informative. What is happening (at least on the date fields) is I
am getting invalid system cast, cannot convert datetime to string. Not
sure how I should write this code.
Here is what I have:
theField =
(String)custRow["NOTICE_OF_VIOLATION_DATE"];
builder.MoveToBookmark("date_served");
builder.Write(theField);
Any help appreciated.
Instead of calling GetString, call GetObject, and look at the value
returned in the debugger (you will obviously have to set up a variable of
type object to hold the result).
You should see what the type is that is returned, and it should help
you
with figuring out what the problem is.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Getting and error on this first line of code:
//inspector phone
theField = reader.GetString(20);
builder.MoveToBookmark("inspector_phone");
builder.Write(theField);
the first line is crashing my program. it complains about an invalid
cast. i don't understand why though. I tried putting a IsDBNull around
it (thinking the data might be null) but that does not seem to help.
the field is declared as a string and reader is a connection to my
Oracle database.
Any help appreciated.- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
chance said:
Well I change data providers to Oracle.NET. It seems to be a little
more informative. What is happening (at least on the date fields) is I
am getting invalid system cast, cannot convert datetime to string. Not
sure how I should write this code.

Here is what I have:

theField =
(String)custRow["NOTICE_OF_VIOLATION_DATE"];
builder.MoveToBookmark("date_served");
builder.Write(theField);

Well, "NOTICE_OF_VIOLATION_DATE" sounds like it's more likely to be a
DateTime than a string - why are you casting it to a string?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top