GridView\CheckBox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gh

    GridView\CheckBox

    I am using VS 2008. I have a gridview with several columns that are
    checkboxes. The data for each column is either a 1 or 0. When I try to
    view the page in the browser I get an error "Specified cast is not
    valid". Below is the code for the column. PL is the field name in the
    datasource. It is a string field. I did change it to integer and still
    received the same error message. What could be causing the error?


    TIA



    <asp:TemplateFi eld>
    <EditItemTempla te>
    asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#
    Bind("PL") %>' />
    </EditItemTemplat e>
    <ItemTemplate >
    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#
    Bind("PL") %>'
    Enabled="false" />
    </ItemTemplate>
  • Mark Rae [MVP]

    #2
    Re: GridView\CheckB ox

    "gh" <gh@att.netwrot e in message
    news:eTvADQnuIH A.420@TK2MSFTNG P02.phx.gbl...
    What could be causing the error?
    The Checked tag of an <asp:CheckBox /webcontrol needs to be populated with
    something that can be evaluated as a Boolean.

    What happens if you modify your markup like so..?

    Checked='<%# DataBinder.Eval (Container.Data Item, "PL") %>'


    --
    Mark Rae
    ASP.NET MVP


    Comment

    • gh

      #3
      Re: GridView\CheckB ox

      Mark Rae [MVP] wrote:
      "gh" <gh@att.netwrot e in message
      news:eTvADQnuIH A.420@TK2MSFTNG P02.phx.gbl...
      >
      >What could be causing the error?
      >
      The Checked tag of an <asp:CheckBox /webcontrol needs to be populated
      with something that can be evaluated as a Boolean.
      >
      What happens if you modify your markup like so..?
      >
      Checked='<%# DataBinder.Eval (Container.Data Item, "PL") %>'
      >
      >
      Mark:

      I get the same error message (System.Invalid CastException: Specified
      cast is not valid.
      ) on Line 26.

      </EditItemTemplat e>
      Line 25: <ItemTemplate >
      Line 26: <asp:CheckBox ID="CheckBox1"
      runat="server" Checked='<%# DataBinder.Eval (Container.Data Item, "PL") %>'
      Line 27: Enabled="false" />
      Line 28: </ItemTemplate>

      Thanks

      Comment

      • Mark Rae [MVP]

        #4
        Re: GridView\CheckB ox

        "gh" <gh@att.netwrot e in message
        news:OY8g92nuIH A.2068@TK2MSFTN GP05.phx.gbl...
        >>What could be causing the error?
        >>
        >The Checked tag of an <asp:CheckBox /webcontrol needs to be populated
        >with something that can be evaluated as a Boolean.
        >>
        >What happens if you modify your markup like so..?
        >>
        >Checked='<%# DataBinder.Eval (Container.Data Item, "PL") %>'
        >>
        >>
        Mark:
        >
        I get the same error message (System.Invalid CastException: Specified cast
        is not valid.) on Line 26.
        >
        </EditItemTemplat e>
        Line 25: <ItemTemplate >
        Line 26: <asp:CheckBox ID="CheckBox1"
        runat="server" Checked='<%# DataBinder.Eval (Container.Data Item, "PL") %>'
        Line 27: Enabled="false" />
        Line 28: </ItemTemplate>
        OK. What *precisely* is the value of "PL" out of the database, and what is
        the datatype?

        For this to work, it will need to be a datatype / value combination which
        can be converted into a Boolean...


        --
        Mark Rae
        ASP.NET MVP


        Comment

        • gh

          #5
          Re: GridView\CheckB ox

          Mark Rae [MVP] wrote:
          "gh" <gh@att.netwrot e in message
          news:OY8g92nuIH A.2068@TK2MSFTN GP05.phx.gbl...
          >
          >>>What could be causing the error?
          >>>
          >>The Checked tag of an <asp:CheckBox /webcontrol needs to be
          >>populated with something that can be evaluated as a Boolean.
          >>>
          >>What happens if you modify your markup like so..?
          >>>
          >>Checked='<% # DataBinder.Eval (Container.Data Item, "PL") %>'
          >>>
          >>>
          >Mark:
          >>
          >I get the same error message (System.Invalid CastException: Specified
          >cast is not valid.) on Line 26.
          >>
          > </EditItemTemplat e>
          >Line 25: <ItemTemplate >
          >Line 26: <asp:CheckBox ID="CheckBox1"
          >runat="serve r" Checked='<%# DataBinder.Eval (Container.Data Item, "PL") %>'
          >Line 27: Enabled="false" />
          >Line 28: </ItemTemplate>
          >
          OK. What *precisely* is the value of "PL" out of the database, and what
          is the datatype?
          >
          For this to work, it will need to be a datatype / value combination
          which can be converted into a Boolean...
          >
          >
          It is a varchar. The value is 0 or 1. I tried a Convert.ToInt32 , but
          receeved an error about the bool value. I don' t control the table
          structures and have to use what is given to me. Shouldn' t an integer 1
          or 0 be evaluated as a bool?

          Thanks

          Comment

          • Mark Rae [MVP]

            #6
            Re: GridView\CheckB ox

            "gh" <gh@att.netwrot e in message
            news:OxcVZIouIH A.4876@TK2MSFTN GP02.phx.gbl...
            >OK. What *precisely* is the value of "PL" out of the database, and what
            >is the datatype?
            >>
            >For this to work, it will need to be a datatype / value combination which
            >can be converted into a Boolean...
            >>
            It is a varchar. The value is 0 or 1.
            OK, well that's *never* going to work natively as you've already discovered.
            I tried a Convert.ToInt32 , but receeved an error about the bool value. I
            don' t control the table structures and have to use what is given to me.
            Hmm - OK... Where did you do the Convert.ToInt32 ?
            Shouldn' t an integer 1 or 0 be evaluated as a bool?
            Yes it should - which is why I'm wondering where you did the conversion...


            --
            Mark Rae
            ASP.NET MVP


            Comment

            • gh

              #7
              Re: GridView\CheckB ox

              Mark Rae [MVP] wrote:
              "gh" <gh@att.netwrot e in message
              news:OxcVZIouIH A.4876@TK2MSFTN GP02.phx.gbl...
              >
              >>OK. What *precisely* is the value of "PL" out of the database, and
              >>what is the datatype?
              >>>
              >>For this to work, it will need to be a datatype / value combination
              >>which can be converted into a Boolean...
              >>>
              >It is a varchar. The value is 0 or 1.
              >
              OK, well that's *never* going to work natively as you've already
              discovered.
              >
              >I tried a Convert.ToInt32 , but receeved an error about the bool
              >value. I don' t control the table structures and have to use what is
              >given to me.
              >
              Hmm - OK... Where did you do the Convert.ToInt32 ?
              >
              >Shouldn' t an integer 1 or 0 be evaluated as a bool?
              >
              Yes it should - which is why I'm wondering where you did the conversion...
              >
              >
              <asp:TemplateFi eld>
              <EditItemTempla te>
              <asp:CheckBox ID="CheckBox1" runat="server"
              Checked='<%# Convert.ToInt32 (DataBinder.Eva l(Container.Dat aItem, "PL"))
              %>' />
              </EditItemTemplat e>
              <ItemTemplate >
              <asp:CheckBox ID="CheckBox1" runat="server"
              Checked='<%# Convert.ToInt32 (DataBinder.Eva l(Container.Dat aItem, "PL"))
              %>'
              Enabled="false" />
              </ItemTemplate>

              Thanks

              Comment

              • Mark Rae [MVP]

                #8
                Re: GridView\CheckB ox

                "gh" <gh@att.netwrot e in message
                news:eoGJLiouIH A.5288@TK2MSFTN GP06.phx.gbl...
                >Yes it should - which is why I'm wondering where you did the
                >conversion.. .
                >
                <asp:TemplateFi eld>
                <EditItemTempla te>
                <asp:CheckBox ID="CheckBox1" runat="server"
                Checked='<%# Convert.ToInt32 (DataBinder.Eva l(Container.Dat aItem, "PL"))
                %>' />
                </EditItemTemplat e>
                <ItemTemplate >
                <asp:CheckBox ID="CheckBox1" runat="server"
                Checked='<%# Convert.ToInt32 (DataBinder.Eva l(Container.Dat aItem, "PL"))
                %>'
                Enabled="false" />
                </ItemTemplate>
                OK, indulge me...

                Change the above to the code below, and tell me what the Text shows next to
                the CheckBoxes...

                <asp:TemplateFi eld>
                <ItemTemplate >
                <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#
                DataBinder.Eval (Container.Data Item, "PL") %>' Enabled="false" />
                </ItemTemplate>
                </ asp:TemplateFie ld>


                --
                Mark Rae
                ASP.NET MVP


                Comment

                • Mark Rae [MVP]

                  #9
                  Re: GridView\CheckB ox

                  "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
                  news:%23r0mLyou IHA.3680@TK2MSF TNGP05.phx.gbl. ..
                  <asp:TemplateFi eld>
                  <ItemTemplate >
                  <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#
                  DataBinder.Eval (Container.Data Item, "PL") %>' Enabled="false" />
                  </ItemTemplate>
                  </ asp:TemplateFie ld>
                  Apologies - I meant this:

                  <asp:TemplateFi eld>
                  <ItemTemplate >
                  <asp:CheckBox ID="CheckBox1" runat="server" Text='<%#
                  DataBinder.Eval (Container.Data Item, "PL") %>' />
                  </ItemTemplate>
                  </ asp:TemplateFie ld>



                  --
                  Mark Rae
                  ASP.NET MVP


                  Comment

                  • =?Utf-8?B?YnJ1Y2UgYmFya2Vy?=

                    #10
                    Re: GridView\CheckB ox

                    as stated the "checked" value needs to be a bool. so:

                    Checked='<%# DataBinder.Eval (Container.Data Item, "PL").ToString( ) == "1" %>'

                    will work.

                    -- bruce (sqlwork.com)


                    "gh" wrote:
                    Mark Rae [MVP] wrote:
                    "gh" <gh@att.netwrot e in message
                    news:OY8g92nuIH A.2068@TK2MSFTN GP05.phx.gbl...
                    >>What could be causing the error?
                    >>
                    >The Checked tag of an <asp:CheckBox /webcontrol needs to be
                    >populated with something that can be evaluated as a Boolean.
                    >>
                    >What happens if you modify your markup like so..?
                    >>
                    >Checked='<%# DataBinder.Eval (Container.Data Item, "PL") %>'
                    >>
                    >>
                    Mark:
                    >
                    I get the same error message (System.Invalid CastException: Specified
                    cast is not valid.) on Line 26.
                    >
                    </EditItemTemplat e>
                    Line 25: <ItemTemplate >
                    Line 26: <asp:CheckBox ID="CheckBox1"
                    runat="server" Checked='<%# DataBinder.Eval (Container.Data Item, "PL") %>'
                    Line 27: Enabled="false" />
                    Line 28: </ItemTemplate>
                    OK. What *precisely* is the value of "PL" out of the database, and what
                    is the datatype?

                    For this to work, it will need to be a datatype / value combination
                    which can be converted into a Boolean...
                    It is a varchar. The value is 0 or 1. I tried a Convert.ToInt32 , but
                    receeved an error about the bool value. I don' t control the table
                    structures and have to use what is given to me. Shouldn' t an integer 1
                    or 0 be evaluated as a bool?
                    >
                    Thanks
                    >

                    Comment

                    • gh

                      #11
                      Re: GridView\CheckB ox

                      Mark Rae [MVP] wrote:
                      "Mark Rae [MVP]" <mark@markNOSPA Mrae.netwrote in message
                      news:%23r0mLyou IHA.3680@TK2MSF TNGP05.phx.gbl. ..
                      >
                      ><asp:TemplateF ield>
                      > <ItemTemplate >
                      > <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#
                      >DataBinder.Eva l(Container.Dat aItem, "PL") %>' Enabled="false" />
                      > </ItemTemplate>
                      ></ asp:TemplateFie ld>
                      >
                      Apologies - I meant this:
                      >
                      <asp:TemplateFi eld>
                      <ItemTemplate >
                      <asp:CheckBox ID="CheckBox1" runat="server" Text='<%#
                      DataBinder.Eval (Container.Data Item, "PL") %>' />
                      </ItemTemplate>
                      </ asp:TemplateFie ld>
                      >
                      >
                      >
                      Mark:

                      I have it working using the suggested markup:

                      Checked='<%# DataBinder.Eval (Container.Data Item, "PL").ToString( ) ==
                      "1" %>'.

                      Thanks

                      Comment

                      Working...