473,385 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

GridView\CheckBox

gh
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>
Jun 27 '08 #1
10 1641
"gh" <gh@att.netwrote in message
news:eT*************@TK2MSFTNGP02.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.DataItem, "PL") %>'
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #2
gh
Mark Rae [MVP] wrote:
"gh" <gh@att.netwrote in message
news:eT*************@TK2MSFTNGP02.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.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
Jun 27 '08 #3
"gh" <gh@att.netwrote in message
news:OY**************@TK2MSFTNGP05.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.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...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #4
gh
Mark Rae [MVP] wrote:
"gh" <gh@att.netwrote in message
news:OY**************@TK2MSFTNGP05.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.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
Jun 27 '08 #5
"gh" <gh@att.netwrote in message
news:Ox**************@TK2MSFTNGP02.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
http://www.markrae.net

Jun 27 '08 #6
gh
Mark Rae [MVP] wrote:
"gh" <gh@att.netwrote in message
news:Ox**************@TK2MSFTNGP02.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: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
Jun 27 '08 #7
"gh" <gh@att.netwrote in message
news:eo**************@TK2MSFTNGP06.phx.gbl...
>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>
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

Jun 27 '08 #8
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
<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 Rae
ASP.NET MVP
http://www.markrae.net

Jun 27 '08 #9
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" <gh@att.netwrote in message
news:OY**************@TK2MSFTNGP05.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.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
Jun 27 '08 #10
gh
Mark Rae [MVP] wrote:
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
><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
Jun 27 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.