What's wrong with this statement??

  • Thread starter Thread starter Prodip Saha
  • Start date Start date
P

Prodip Saha

I am trying to assign a value to a variable (of string type) from database
column and it's giving me Specified cast is not valid. Can you tell me
what's wrong with the statement?
string _strVar1;

//DataRow["DataColumnName"] is Int32 type coming from Database

_strVar1=((Int32)DataRow["DataColumnName"]).ToString();

I can assign the value in Immediate window w/o any problem but I am not been
able to execute this line of code. It gives me Specified cast is not valid.

Thanks,

Prodip
 
What would be the point of turning something into a string, just to turn it
into an integer afterwards? Why not just cast the object to an integer to
begin with?

The reason this is failing, is that you cannot cast a string directly to an
integer. You can parse the string into an integer using Int32.Parse, but
you cannot cast directly.

Again, there is no point in doing the extra work of turning an object (that
is really an integer), into a string, just to then parse it back into an
integer. Easier and more efficient to just cast it to an integer right off
the bat.
 
Marina, Thanks for the response. I was actually trying to convert Int32 to
string type and not the other way.

Anyway, the problem was different. The asp.net debugging file was located in
the debug folder while the application is looking for dll located in the bin
folder. This caused the application to debug even the comment lines because
it was not pointed to correct copy of the dll.



Marina said:
What would be the point of turning something into a string, just to turn it
into an integer afterwards? Why not just cast the object to an integer to
begin with?

The reason this is failing, is that you cannot cast a string directly to an
integer. You can parse the string into an integer using Int32.Parse, but
you cannot cast directly.

Again, there is no point in doing the extra work of turning an object (that
is really an integer), into a string, just to then parse it back into an
integer. Easier and more efficient to just cast it to an integer right off
the bat.

Prodip Saha said:
I am trying to assign a value to a variable (of string type) from database
column and it's giving me Specified cast is not valid. Can you tell me
what's wrong with the statement?
string _strVar1;

//DataRow["DataColumnName"] is Int32 type coming from Database

_strVar1=((Int32)DataRow["DataColumnName"]).ToString();

I can assign the value in Immediate window w/o any problem but I am not been
able to execute this line of code. It gives me Specified cast is not valid.

Thanks,

Prodip
 
Back
Top