Database: table field casting problem

  • Thread starter Thread starter Jim Stools
  • Start date Start date
J

Jim Stools

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.
 
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 said:
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.
 
Jim said:
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
 
Jim said:
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
 
Many Thanks!

Ken said:
Jim said:
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
 
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 said:
Jim said:
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
 

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