Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > GridView\CheckBox

Reply
Thread Tools

GridView\CheckBox

 
 
gh
Guest
Posts: n/a
 
      05-20-2008
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:TemplateField>
<EditItemTemplate>
asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#
Bind("PL") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#
Bind("PL") %>'
Enabled="false" />
</ItemTemplate>
 
Reply With Quote
 
 
 
 
gh
Guest
Posts: n/a
 
      05-20-2008
Mark Rae [MVP] wrote:
> "gh" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> 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.DataItem, "PL") %>'
>
>

Mark:

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

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

Thanks
 
Reply With Quote
 
 
 
 
gh
Guest
Posts: n/a
 
      05-20-2008
Mark Rae [MVP] wrote:
> "gh" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>>> 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.DataItem, "PL") %>'
>>>
>>>

>> Mark:
>>
>> I get the same error message (System.InvalidCastException: Specified
>> cast is not valid.) on Line 26.
>>
>> </EditItemTemplate>
>> Line 25: <ItemTemplate>
>> Line 26: <asp:CheckBox ID="CheckBox1"
>> runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "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
 
Reply With Quote
 
gh
Guest
Posts: n/a
 
      05-20-2008
Mark Rae [MVP] wrote:
> "gh" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>>> 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:TemplateField>
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"
Checked='<%# Convert.ToInt32(DataBinder.Eval(Container.DataItem , "PL"))
%>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"
Checked='<%# Convert.ToInt32(DataBinder.Eval(Container.DataItem , "PL"))
%>'
Enabled="false" />
</ItemTemplate>

Thanks
 
Reply With Quote
 
bruce barker
Guest
Posts: n/a
 
      05-20-2008
as stated the "checked" value needs to be a bool. so:

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

will work.

-- bruce (sqlwork.com)


"gh" wrote:

> Mark Rae [MVP] wrote:
> > "gh" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> >
> >>>> 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.DataItem, "PL") %>'
> >>>
> >>>
> >> Mark:
> >>
> >> I get the same error message (System.InvalidCastException: Specified
> >> cast is not valid.) on Line 26.
> >>
> >> </EditItemTemplate>
> >> Line 25: <ItemTemplate>
> >> Line 26: <asp:CheckBox ID="CheckBox1"
> >> runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "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
>

 
Reply With Quote
 
gh
Guest
Posts: n/a
 
      05-21-2008
Mark Rae [MVP] wrote:
> "Mark Rae [MVP]" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>
>> <asp:TemplateField>
>> <ItemTemplate>
>> <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#
>> DataBinder.Eval(Container.DataItem, "PL") %>' Enabled="false" />
>> </ItemTemplate>
>> </ asp:TemplateField>

>
> Apologies - I meant this:
>
> <asp:TemplateField>
> <ItemTemplate>
> <asp:CheckBox ID="CheckBox1" runat="server" Text='<%#
> DataBinder.Eval(Container.DataItem, "PL") %>' />
> </ItemTemplate>
> </ asp:TemplateField>
>
>
>

Mark:

I have it working using the suggested markup:

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

Thanks
 
Reply With Quote
 
Sarath Kumar
Guest
Posts: n/a
 
      03-03-2010
Thanks a lot brucebarke . I spent lots of time figuring out this issue, until I read your reply.



brucebarke wrote:

Re: GridView\CheckBox
20-May-08

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

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

will work.

-- bruce (sqlwork.com)


"gh" wrote:

Previous Posts In This Thread:

On Tuesday, May 20, 2008 8:21 AM
gh wrote:

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:TemplateField>
<EditItemTemplate>
asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#
Bind("PL") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#
Bind("PL") %>'
Enabled="false" />
</ItemTemplate>

On Tuesday, May 20, 2008 8:43 AM
Mark Rae [MVP] wrote:

Re: GridView\CheckBox
"gh" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...


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.DataItem, "PL") %>'


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

On Tuesday, May 20, 2008 9:30 AM
gh wrote:

Re: GridView\CheckBox
Mark Rae [MVP] wrote:
Mark:

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

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

Thanks

On Tuesday, May 20, 2008 9:36 AM
Mark Rae [MVP] wrote:

Re: GridView\CheckBox
"gh" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...


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
http://www.markrae.net

On Tuesday, May 20, 2008 10:02 AM
gh wrote:

Re: GridView\CheckBox
Mark Rae [MVP] wrote:
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

On Tuesday, May 20, 2008 10:19 AM
Mark Rae [MVP] wrote:

Re: GridView\CheckBox
"gh" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...


OK, well that's *never* going to work natively as you've already discovered.


Hmm - OK... Where did you do the Convert.ToInt32?


Yes it should - which is why I'm wondering where you did the conversion...


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

On Tuesday, May 20, 2008 10:48 AM
gh wrote:

Re: GridView\CheckBox
Mark Rae [MVP] wrote:

<asp:TemplateField>
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"
Checked='<%# Convert.ToInt32(DataBinder.Eval(Container.DataItem , "PL"))
%>' />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"
Checked='<%# Convert.ToInt32(DataBinder.Eval(Container.DataItem , "PL"))
%>'
Enabled="false" />
</ItemTemplate>

Thanks

On Tuesday, May 20, 2008 11:16 AM
Mark Rae [MVP] wrote:

Re: GridView\CheckBox
"gh" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...


OK, indulge me...

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

<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#
DataBinder.Eval(Container.DataItem, "PL") %>' Enabled="false" />
</ItemTemplate>
</ asp:TemplateField>


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

On Tuesday, May 20, 2008 11:23 AM
Mark Rae [MVP] wrote:

Re: GridView\CheckBox
"Mark Rae [MVP]" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...


Apologies - I meant this:

<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "PL") %>' />
</ItemTemplate>
</ asp:TemplateField>



--
Mark Rae
ASP.NET MVP
http://www.markrae.net

On Tuesday, May 20, 2008 11:34 AM
brucebarke wrote:

Re: GridView\CheckBox
as stated the "checked" value needs to be a bool. so:

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

will work.

-- bruce (sqlwork.com)


"gh" wrote:

On Wednesday, May 21, 2008 7:32 AM
gh wrote:

Re: GridView\CheckBox
Mark Rae [MVP] wrote:
Mark:

I have it working using the suggested markup:

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

Thanks


Submitted via EggHeadCafe - Software Developer Portal of Choice
Measuring SharePoint Page Rendering
http://www.eggheadcafe.com/tutorials...oint-page.aspx
 
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




Advertisments