Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / May 2008

Tip: Looking for answers? Try searching our database.

GridView\CheckBox

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
gh - 20 May 2008 13:21 GMT
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>
Mark Rae [MVP] - 20 May 2008 13:43 GMT
> 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") %>'

Signature

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

gh - 20 May 2008 14:30 GMT
>> What could be causing the error?
>
[quoted text clipped - 4 lines]
>
> 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
Mark Rae [MVP] - 20 May 2008 14:36 GMT
>>> What could be causing the error?
>>
[quoted text clipped - 16 lines]
> 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...

Signature

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

gh - 20 May 2008 15:02 GMT
>>>> What could be causing the error?
>>>
[quoted text clipped - 22 lines]
> 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
Mark Rae [MVP] - 20 May 2008 15:19 GMT
>> OK. What *precisely* is the value of "PL" out of the database, and what
>> is the datatype?
[quoted text clipped - 3 lines]
>>
> 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...

Signature

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

gh - 20 May 2008 15:48 GMT
>>> OK. What *precisely* is the value of "PL" out of the database, and
>>> what is the datatype?
[quoted text clipped - 16 lines]
>
> 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
Mark Rae [MVP] - 20 May 2008 16:16 GMT
>> Yes it should - which is why I'm wondering where you did the
>> conversion...
[quoted text clipped - 11 lines]
>                             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>

Signature

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

Mark Rae [MVP] - 20 May 2008 16:23 GMT
> <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>

Signature

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

gh - 21 May 2008 12:32 GMT
>> <asp:TemplateField>
>>    <ItemTemplate>
[quoted text clipped - 11 lines]
>   </ItemTemplate>
> </ asp:TemplateField>

Mark:

I have it working using the suggested markup:

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

Thanks
bruce barker - 20 May 2008 16:34 GMT
as stated the "checked" value needs to be a bool. so:

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

will work.

-- bruce (sqlwork.com)

> >>>> What could be causing the error?
> >>>
[quoted text clipped - 29 lines]
>
> Thanks

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.