Error: specified cast is not valid. Why not?

Discussion in 'ASP .Net' started by Alan Silver, Feb 15, 2005.

  1. Alan Silver

    Alan Silver Guest

    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, Feb 15, 2005
    #1
    1. Advertising

  2. Alan Silver

    Elton Wang Guest

    Try

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

    HTH,

    Elton Wang



    >-----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)
    >.
    >
     
    Elton Wang, Feb 15, 2005
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:...
    > 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)
     
    Kevin Spencer, Feb 15, 2005
    #3
  4. Alan Silver

    Alan Silver Guest

    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
    >
    >
    >
    >>-----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)
     
    Alan Silver, Feb 15, 2005
    #4
  5. 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("CurrentYes")) ...
    >
    > --
    > HTH,
    >
    > Kevin Spencer
    > Microsoft MVP
    > ..Net Developer
    > Neither a follower nor a lender be.
    >
    > "Alan Silver" <> wrote in message
    > news:...
    > > 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)

    >
    >
    >
     
    =?Utf-8?B?VGFtcGEgLk5FVCBLb2Rlcg==?=, Feb 15, 2005
    #5
  6. 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" <> wrote in
    message news:...
    > 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("CurrentYes")) ...
    >>
    >> --
    >> HTH,
    >>
    >> Kevin Spencer
    >> Microsoft MVP
    >> ..Net Developer
    >> Neither a follower nor a lender be.
    >>
    >> "Alan Silver" <> wrote in message
    >> news:...
    >> > 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)

    >>
    >>
    >>
     
    Kevin Spencer, Feb 15, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew
    Replies:
    0
    Views:
    679
    Andrew
    Aug 12, 2003
  2. Stefan Landgraf

    Re: 'Specified Cast Is Not Valid' Error!

    Stefan Landgraf, Aug 19, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    429
    Stefan Landgraf
    Aug 19, 2003
  3. Miguel Angel Dinelli
    Replies:
    1
    Views:
    464
    Chris Jackson
    Nov 13, 2003
  4. =?Utf-8?B?Q2hhcmxlcw==?=
    Replies:
    8
    Views:
    496
    =?Utf-8?B?Q2hhcmxlcw==?=
    May 4, 2004
  5. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,213
    Smokey Grindel
    Dec 2, 2006
Loading...

Share This Page