473,466 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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.

Mar 26 '07 #1
7 1854
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]
- mv*@spam.guard.caspershouse.com

"chance" <ch****@crwmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
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.

Mar 26 '07 #2
PS

"chance" <ch****@crwmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
Getting and error on this first line of code:
//inspector phone
theField = reader.GetString(20);
What column number is theField in the reader?
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.

Mar 26 '07 #3
Hi,

"chance" <ch****@crwmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
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();
Mar 26 '07 #4
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.


On Mar 26, 11:53 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
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]
- m...@spam.guard.caspershouse.com

"chance" <cha...@crwmail.comwrote in message

news:11**********************@l77g2000hsb.googlegr oups.com...
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 -

Mar 26 '07 #5
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]
- mv*@spam.guard.caspershouse.com

"chance" <ch****@crwmail.comwrote in message
news:11**********************@p15g2000hsd.googlegr oups.com...
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.


On Mar 26, 11:53 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
> 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]
- m...@spam.guard.caspershouse.com

"chance" <cha...@crwmail.comwrote in message

news:11**********************@l77g2000hsb.googleg roups.com...
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 -


Mar 26 '07 #6
Yes. if I use ToString on all the non-String fields everything works
fine. Problem solved.

thanks.

On Mar 26, 1:20 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
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]
- m...@spam.guard.caspershouse.com

"chance" <cha...@crwmail.comwrote in message

news:11**********************@p15g2000hsd.googlegr oups.com...
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.
On Mar 26, 11:53 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
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]
- m...@spam.guard.caspershouse.com
"chance" <cha...@crwmail.comwrote in message
>news:11**********************@l77g2000hsb.googleg roups.com...
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 -

Mar 26 '07 #7
chance <ch****@crwmail.comwrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 26 '07 #8

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

Similar topics

1
by: | last post by:
When I execute the following (with an OleDBDataAdapter), wanting to add a row to a visual foxpro table: myrow= datasetTarget.Tables(0).NewRow 'fill all columns here like.. row(i)= myvalue '...
8
by: Shino | last post by:
Hi, Can anyone help with this error: "ORA-00904: invalid column name"? Thanks! SQL> create view PPFa as 2 SELECT L.UserID AS LecID, U.Name, U.Email, I.IntakeID, S.UserID AS StudID 3 FROM...
1
by: mariantrygg | last post by:
Set qdf = dbs.QueryDefs("qryUpdate_OMNIS_F_ST_Transactions") 'append qdf.Parameters(0) = DMax("STTR_SEQ", "CopyOMNIS_F_ST_Transaction") qdf.Execute dbSeeChanges This gives me an error 3146...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
2
by: adams114 | last post by:
I am having a strange problem with invalid type casts. I am trying to update a MS SQL Database with a stored procedure. When I setup the parameters collection for the command object I get a invalid...
3
by: John Howard | last post by:
Making the following call to a local MSAccess database works fine: Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) Dim intRows As Integer Dim strSQL As String Dim ds As New...
15
by: David | last post by:
Hi, I have built a web application that will be a very high profile application. We had tested it, demonstrated it and shown that it all works. On a dress rehearsal run through, it failed...
5
by: cgoetz | last post by:
Hi all, I'm using VS 2003 with the 1.1 framework and am experimenting with the DataGrid and am applying several different techniques to it. Here is what works: Dim WithEvents datagridtextBox...
4
by: Alan | last post by:
I keep getting an invalid cast error on this line of code: Dim gridCol As MyGridColumn = CType(dgt.GridColumnStyles(hi.Column), MyGridColumn) I am trying to develop a sub that will change the...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.