Error: specified cast is not valid. Why not?

A

Alan Silver

Hello,

I have an ASP.NET page where I am grabbing an SqlDataReader and using it
to populate some controls on the form. Amongst the fields pulled out of
the table are two integer fields, which I am trying to cast as strings
so they can be displayed in a Literal control.

I am using the following code (heavily edited for clarity) ...

if (dtrDilemmas.Read()) {
litDilCurrentYesNo.Text = (String)dtrDilemmas["CurrentYes"] + "
votes yes and " + (String)dtrDilemmas["CurrentNo"] + " votes no";
}

where dtrDilemmas is the SqlDataReader that contains just one record. In
that record are two fields, CurrentYes and CurrentNo, which are both
integers. I have other (varchar) fields which I can use just fine. When
it hits the line shown above, I get the error "specified cast is not
valid".

Anyone any idea why not and how I fix it? TIA
 
E

Elton Wang

Try

litDilCurrentYesNo.Text = ((int)dtrDilemmas
["CurrentYes"]).Tostring() + "votes yes and " + ((int)
dtrDilemmas["CurrentNo"]).Tostring() + " votes no";

HTH,

Elton Wang
(e-mail address removed)
 
K

Kevin Spencer

You can't cast the object as a string because it isn't a string. However,
you can use the DataReader's methods to get the string out of it, such as
GetString(). Example:

litDilCurrentYesNo.Text =
dtrDilemmas.GetString(dtrDilemmas.GetOrdinal("CurrentYes")) ...

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 
A

Alan Silver

Thanks to both of you. It's obvious now, but it needed pointing out ;-)
Try

litDilCurrentYesNo.Text = ((int)dtrDilemmas
["CurrentYes"]).Tostring() + "votes yes and " + ((int)
dtrDilemmas["CurrentNo"]).Tostring() + " votes no";

HTH,

Elton Wang
(e-mail address removed)

-----Original Message-----
Hello,

I have an ASP.NET page where I am grabbing an SqlDataReader and using it
to populate some controls on the form. Amongst the fields pulled out of
the table are two integer fields, which I am trying to cast as strings
so they can be displayed in a Literal control.

I am using the following code (heavily edited for clarity) ...

if (dtrDilemmas.Read()) {
litDilCurrentYesNo.Text = (String)dtrDilemmas ["CurrentYes"] + "
votes yes and " + (String)dtrDilemmas["CurrentNo"] + " votes no";
}

where dtrDilemmas is the SqlDataReader that contains just one record. In
that record are two fields, CurrentYes and CurrentNo, which are both
integers. I have other (varchar) fields which I can use just fine. When
it hits the line shown above, I get the error "specified cast is not
valid".

Anyone any idea why not and how I fix it? TIA
 
G

Guest

Really, I thought you were able to downcast from Object to String since
string inherits from Object. Can you shed some light on this subject for the
benefit of all?

Kevin Spencer said:
You can't cast the object as a string because it isn't a string. However,
you can use the DataReader's methods to get the string out of it, such as
GetString(). Example:

litDilCurrentYesNo.Text =
dtrDilemmas.GetString(dtrDilemmas.GetOrdinal("CurrentYes")) ...

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.

Alan Silver said:
Hello,

I have an ASP.NET page where I am grabbing an SqlDataReader and using it
to populate some controls on the form. Amongst the fields pulled out of
the table are two integer fields, which I am trying to cast as strings
so they can be displayed in a Literal control.

I am using the following code (heavily edited for clarity) ...

if (dtrDilemmas.Read()) {
litDilCurrentYesNo.Text = (String)dtrDilemmas["CurrentYes"] + "
votes yes and " + (String)dtrDilemmas["CurrentNo"] + " votes no";
}

where dtrDilemmas is the SqlDataReader that contains just one record. In
that record are two fields, CurrentYes and CurrentNo, which are both
integers. I have other (varchar) fields which I can use just fine. When
it hits the line shown above, I get the error "specified cast is not
valid".

Anyone any idea why not and how I fix it? TIA
 
K

Kevin Spencer

You can cast anything to a string that IS a string. For example, if you use
object (generally not a good idea), you can store ANY kind of object in it.
So, let's say that the object contains a number. Could you cast it as a
string? No. You would have to Convert it to a string. This can be done using
the .ToString() method, or Convert.ToString().

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.

Tampa .NET Koder said:
Really, I thought you were able to downcast from Object to String since
string inherits from Object. Can you shed some light on this subject for
the
benefit of all?

Kevin Spencer said:
You can't cast the object as a string because it isn't a string. However,
you can use the DataReader's methods to get the string out of it, such as
GetString(). Example:

litDilCurrentYesNo.Text =
dtrDilemmas.GetString(dtrDilemmas.GetOrdinal("CurrentYes")) ...

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.

Alan Silver said:
Hello,

I have an ASP.NET page where I am grabbing an SqlDataReader and using
it
to populate some controls on the form. Amongst the fields pulled out of
the table are two integer fields, which I am trying to cast as strings
so they can be displayed in a Literal control.

I am using the following code (heavily edited for clarity) ...

if (dtrDilemmas.Read()) {
litDilCurrentYesNo.Text = (String)dtrDilemmas["CurrentYes"] + "
votes yes and " + (String)dtrDilemmas["CurrentNo"] + " votes no";
}

where dtrDilemmas is the SqlDataReader that contains just one record.
In
that record are two fields, CurrentYes and CurrentNo, which are both
integers. I have other (varchar) fields which I can use just fine. When
it hits the line shown above, I get the error "specified cast is not
valid".

Anyone any idea why not and how I fix it? TIA
 

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

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top