Invalid Cast Exception (ASP.NET using C#)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In a code-behind page in asp.net, written in c#, i am attempting to cast a
value from a dataset into an "int" type. The value in the dataset is itselt
an integer.

My code is:
DropDownList1.SelectedIndex = (int)ds.Tables[0].Rows[0]["vStudyQTypeId"];

The error i get is:
System.InvalidCastException: Specified cast is not valid.

Can anyone shed some light on what might be the problem? I have been trying
to work w/ mappings and schematypes, however, still have no success.

Thanks,
 
charliewest said:
In a code-behind page in asp.net, written in c#, i am attempting to cast a
value from a dataset into an "int" type. The value in the dataset is itselt
an integer.

My code is:
DropDownList1.SelectedIndex = (int)ds.Tables[0].Rows[0]["vStudyQTypeId"];

The error i get is:
System.InvalidCastException: Specified cast is not valid.

Can anyone shed some light on what might be the problem? I have been trying
to work w/ mappings and schematypes, however, still have no success.

The first thing to do is find out what the type *actually* is. Try
printing out

ds.Tables[0].Rows[0]["vStudyQTypeId"].GetType();
 
You might want to try:

DropDownList1.SelectedIndex = (int)(ds.Tables[0].Rows[0]["vStudyQTypeId"]);

I can't swear to it (I'm not in front of my dev box to test), but I
think your statement is saying to cast the ds to int then try to
reference everything else, which obviously won't work. This change
tells it to resolve everything then then cast the results to int. If
that doesn't work then I'd check the type of the value you are grabbing
and make sure it is an int. (you may also want to make sure the return
value can't be dbnull.value or you'll get an exception as well.

Have A Better One!

John M Deal, MCP
Necessity Software
 
John M Deal said:
You might want to try:

DropDownList1.SelectedIndex = (int)(ds.Tables[0].Rows[0]["vStudyQTypeId"]);

I can't swear to it (I'm not in front of my dev box to test), but I
think your statement is saying to cast the ds to int then try to
reference everything else, which obviously won't work.

Nope - that would fail at compile time if that were the issue.
This change
tells it to resolve everything then then cast the results to int. If
that doesn't work then I'd check the type of the value you are grabbing
and make sure it is an int. (you may also want to make sure the return
value can't be dbnull.value or you'll get an exception as well.

Yup, the value is the important thing.
 
Thanks for your help. Turns out that the type was "System.Int16" rather than
"int", which i had mistakenly took it for.

Jon Skeet said:
John M Deal said:
You might want to try:

DropDownList1.SelectedIndex = (int)(ds.Tables[0].Rows[0]["vStudyQTypeId"]);

I can't swear to it (I'm not in front of my dev box to test), but I
think your statement is saying to cast the ds to int then try to
reference everything else, which obviously won't work.

Nope - that would fail at compile time if that were the issue.
This change
tells it to resolve everything then then cast the results to int. If
that doesn't work then I'd check the type of the value you are grabbing
and make sure it is an int. (you may also want to make sure the return
value can't be dbnull.value or you'll get an exception as well.

Yup, the value is the important thing.
 
Back
Top