PC Review


Reply
Thread Tools Rate Thread

Database: table field casting problem

 
 
Jim Stools
Guest
Posts: n/a
 
      12th Jun 2006
Int16 iValue = (Int16) dr["Portfolio_Number"];

The field name is "Portfolio" and it is a Int16 - however I get an error:
"Specified cast is not valid".

Any ideas -- Thanks in advance.


 
Reply With Quote
 
 
 
 
Jim Stools
Guest
Posts: n/a
 
      12th Jun 2006
Never mind: It's a problem with MySql



"Jim Stools" <(E-Mail Removed)> wrote in message
news:448d974e$0$1016$(E-Mail Removed)...
> Int16 iValue = (Int16) dr["Portfolio_Number"];
>
> The field name is "Portfolio" and it is a Int16 - however I get an error:
> "Specified cast is not valid".
>
> Any ideas -- Thanks in advance.
>



 
Reply With Quote
 
Saad Rehmani
Guest
Posts: n/a
 
      12th Jun 2006

Try

Console.Writeline(dr["Portfolio_Number"].GetType().FullName);

--
Saad Rehmani / Prodika / Dallas / TX / USA

> Int16 iValue = (Int16) dr["Portfolio_Number"];
>
> The field name is "Portfolio" and it is a Int16 - however I get an
> error: "Specified cast is not valid".
>
> Any ideas -- Thanks in advance.
>



 
Reply With Quote
 
Jim Stools
Guest
Posts: n/a
 
      12th Jun 2006
The field was actually an Int32 however the number is always positive and
will never be over 2,000. The variable storing the number is Int16 so I
ended up using

Int16 iValue = (Int16) (Int32) dr["Portfolio_Number"];

which seems to work.




"Saad Rehmani" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> Try
> Console.Writeline(dr["Portfolio_Number"].GetType().FullName);
>
> --
> Saad Rehmani / Prodika / Dallas / TX / USA
>
>> Int16 iValue = (Int16) dr["Portfolio_Number"];
>>
>> The field name is "Portfolio" and it is a Int16 - however I get an
>> error: "Specified cast is not valid".
>>
>> Any ideas -- Thanks in advance.
>>

>
>



 
Reply With Quote
 
Ken
Guest
Posts: n/a
 
      12th Jun 2006
Jim Stools wrote:
> The field was actually an Int32 however the number is always positive and
> will never be over 2,000. The variable storing the number is Int16 so I
> ended up using
>
> Int16 iValue = (Int16) (Int32) dr["Portfolio_Number"];
>
> which seems to work.
>

Jim,

Here's what we do to avoid that problem:

Int16 iValue =
System.Convert.ToInt16(dr["Portfolio_Number"].ToString());

No need to worry how the data is stored in SQL. But, why specify an
int16? Why not just use an Int?

Ken - KC7RAD
www.aninetworks.com

 
Reply With Quote
 
Ken
Guest
Posts: n/a
 
      12th Jun 2006

Jim Stools wrote:
> The field was actually an Int32 however the number is always positive and
> will never be over 2,000. The variable storing the number is Int16 so I
> ended up using
>
> Int16 iValue = (Int16) (Int32) dr["Portfolio_Number"];
>
> which seems to work.


Jim, I just looked at your code again. With your cast, you are trying
to cast the Object, not its value! (Don't worry, I have done that many
times myself! :-)

I am not sure what your castings will do, but I don't know if this will
return what you want.

Ken - KC7RAD
www.aninetworks.com

 
Reply With Quote
 
Jim Stools
Guest
Posts: n/a
 
      13th Jun 2006
Many Thanks!

"Ken" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> Jim Stools wrote:
>> The field was actually an Int32 however the number is always positive and
>> will never be over 2,000. The variable storing the number is Int16 so I
>> ended up using
>>
>> Int16 iValue = (Int16) (Int32) dr["Portfolio_Number"];
>>
>> which seems to work.

>
> Jim, I just looked at your code again. With your cast, you are trying
> to cast the Object, not its value! (Don't worry, I have done that many
> times myself! :-)
>
> I am not sure what your castings will do, but I don't know if this will
> return what you want.
>
> Ken - KC7RAD
> www.aninetworks.com
>



 
Reply With Quote
 
Jim Stools
Guest
Posts: n/a
 
      13th Jun 2006
Ken,

I forgot to mention the database is a MySql database which does not seem to
like .net 2.0, the ODBC connector I believe is buggy. However, before the
weeks up I may convert it all to SQL Server, which I'm researching now.

From the samples I've looked at (assuming field "Portfolio_Number" is an
Int16) --- Int16 iValue = (Int16) dr["Portfolio_Number"]; --- would be the
correct way for retrieving the value. Actually that's a question.

Thanks,





"Ken" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> Jim Stools wrote:
>> The field was actually an Int32 however the number is always positive and
>> will never be over 2,000. The variable storing the number is Int16 so I
>> ended up using
>>
>> Int16 iValue = (Int16) (Int32) dr["Portfolio_Number"];
>>
>> which seems to work.

>
> Jim, I just looked at your code again. With your cast, you are trying
> to cast the Object, not its value! (Don't worry, I have done that many
> times myself! :-)
>
> I am not sure what your castings will do, but I don't know if this will
> return what you want.
>
> Ken - KC7RAD
> www.aninetworks.com
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Table/Field Information Rose B Microsoft Access Queries 2 6th Jun 2009 11:59 AM
DATABASE field - Table from excel Big Bad Bev Microsoft Word Document Management 0 23rd Jun 2008 02:32 AM
Add field to jet database table in ADO Michael Malinsky Microsoft Excel Programming 3 13th May 2004 11:16 AM
new field in a table of a sql database Renate Microsoft Access Queries 1 24th Oct 2003 05:42 PM
How to add a new field to a table in a developed database? jims Microsoft Access 1 15th Jul 2003 01:25 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:50 AM.