Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Error: specified cast is not valid. Why not?

Reply
Thread Tools

Error: specified cast is not valid. Why not?

 
 
Alan Silver
Guest
Posts: n/a
 
      02-15-2005
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

--
Alan Silver
(anything added below this line is nothing to do with me)
 
Reply With Quote
 
 
 
 
Elton Wang
Guest
Posts: n/a
 
      02-15-2005
Try

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

HTH,

Elton Wang
http://www.velocityreviews.com/forums/(E-Mail 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
>
>--
>Alan Silver
>(anything added below this line is nothing to do with me)
>.
>

 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      02-15-2005
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("Curr entYes")) ...

--
HTH,

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

"Alan Silver" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
> --
> Alan Silver
> (anything added below this line is nothing to do with me)



 
Reply With Quote
 
Alan Silver
Guest
Posts: n/a
 
      02-15-2005
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 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
>>
>>--
>>Alan Silver
>>(anything added below this line is nothing to do with me)
>>.
>>


--
Alan Silver
(anything added below this line is nothing to do with me)
 
Reply With Quote
 
=?Utf-8?B?VGFtcGEgLk5FVCBLb2Rlcg==?=
Guest
Posts: n/a
 
      02-15-2005
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" wrote:

> 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("Curr entYes")) ...
>
> --
> HTH,
>
> Kevin Spencer
> Microsoft MVP
> ..Net Developer
> Neither a follower nor a lender be.
>
> "Alan Silver" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> >
> > --
> > Alan Silver
> > (anything added below this line is nothing to do with me)

>
>
>

 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      02-15-2005
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" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> 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" wrote:
>
>> 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("Curr entYes")) ...
>>
>> --
>> HTH,
>>
>> Kevin Spencer
>> Microsoft MVP
>> ..Net Developer
>> Neither a follower nor a lender be.
>>
>> "Alan Silver" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > 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
>> >
>> > --
>> > Alan Silver
>> > (anything added below this line is nothing to do with me)

>>
>>
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools

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
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Understand why I am getting a "Specified cast is not valid" error =?Utf-8?B?Q2hhcmxlcw==?= ASP .Net 8 05-04-2004 04:06 PM
User Control - InvalidCastException: Specified cast is not valid Ajit ASP .Net 1 04-24-2004 09:28 PM
System.InvalidCastException: Specified cast is not valid. Arjen ASP .Net 1 08-06-2003 03:26 AM
System.InvalidCastException: Specified cast is not valid Floela ASP .Net 3 07-10-2003 10:36 AM



Advertisments