Answered by:
CheckBox inside gridView Bind Table

Question
-
User-909867351 posted
Hi
Please why this code does not work?
The ativo field is a boolean field on MYsql database
<asp:TemplateField HeaderText="ativo" SortExpression="ativo"> <EditItemTemplate> <asp:HiddenField ID="hid_OldName" Value='<%# Eval("ativo") %>' runat="server" /> <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("ativo") %>' /> </EditItemTemplate> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("ativo") %>' Enabled="false" /> </ItemTemplate> </asp:TemplateField>
Got error somethins about conversion not valid. The field is boolean ( tinyint(1))
Why is does not work?
Tuesday, October 16, 2018 10:20 PM
Answers
-
User839733648 posted
Hi mariolopes,
According to your description and code, I suggest that you could modify the vlaue of CheckBox.
You may add Convert.ToBoolean() to the value you've got from database like below.
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Convert.ToBoolean(Eval("ativo")) %>' Enabled="false" />
result:
Best Regards,
Jenifer
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, October 17, 2018 3:03 AM -
User-1716253493 posted
for null issue, you can modify select statement like below
SELECT ISNULL(activo,0) as activo
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, October 17, 2018 6:59 AM -
User475983607 posted
As suggested many times above, convert the result to a Boolean and write code to handle null if you allow nulls.- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, October 17, 2018 10:52 AM
All replies
-
User409696431 posted
What is the value when it throws the error? Is the field nullable, and is it sometimes null?
Wednesday, October 17, 2018 1:55 AM -
User839733648 posted
Hi mariolopes,
According to your description and code, I suggest that you could modify the vlaue of CheckBox.
You may add Convert.ToBoolean() to the value you've got from database like below.
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Convert.ToBoolean(Eval("ativo")) %>' Enabled="false" />
result:
Best Regards,
Jenifer
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, October 17, 2018 3:03 AM -
User-1716253493 posted
for null issue, you can modify select statement like below
SELECT ISNULL(activo,0) as activo
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, October 17, 2018 6:59 AM -
User-909867351 posted
Ok Janifer
Almost done. One problem when I edit the record the value stored is null and not the boolean value. Look at my code:
<asp:TemplateField HeaderText="ativo" SortExpression="ativo"> <EditItemTemplate> <asp:HiddenField ID="hid_OldName" Value='<%# Convert.ToBoolean(Eval("ativo")) %>' runat="server" /> <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Convert.ToBoolean(Eval("ativo")) %>' /> </EditItemTemplate> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Convert.ToBoolean(Eval("ativo")) %>' Enabled="false" /> </ItemTemplate> </asp:TemplateField>
and my sql datasource
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="select * from alunos where ativo=1 order by nome" UpdateCommand="update alunos set ativo=?,nome=?,telemovel=?,email=?,nome_pai=?,telemovel_pai=? where id=?"> <UpdateParameters> <asp:Parameter Name="ativo" Type="Boolean" /> <asp:Parameter Name="nome" Type="String" /> <asp:Parameter Name="telemovel" Type="String" /> <asp:Parameter Name="email" Type="String" /> <asp:Parameter Name="nome_pai" Type="String" /> <asp:Parameter Name="telemovel_pai" Type="String" /> <asp:Parameter Name="id" Type="Int16" /> </UpdateParameters> </asp:SqlDataSource>
It stores null value and not the expected value.
Thank you
Wednesday, October 17, 2018 8:06 AM -
User-1716253493 posted
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("ativo") %>' />
Eval is one way binding, it's read db value but not pass when update/insert
Use Bind() instead to save it
To avoid null binding error, convert it to 0 in select query, or update all nulls to zeros
Wednesday, October 17, 2018 8:46 AM -
User-909867351 posted
If I use Bind I got error something like the conversion is not valid.
All the values have 0 or 1 value for column ativo. None of them have Null values, I don't understand what's happened.
Detalhes da Exceção: System.InvalidCastException: A conversão especificada não é válida
Erro de origem:
Linha 89: </EditItemTemplate> Linha 90: <ItemTemplate> Linha 91: <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("ativo") %>' Enabled="False" /> Linha 92: </ItemTemplate> Linha 93: </asp:TemplateField>
Wednesday, October 17, 2018 9:02 AM -
User475983607 posted
As suggested many times above, convert the result to a Boolean and write code to handle null if you allow nulls.- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, October 17, 2018 10:52 AM -
User-909867351 posted
Sorry Guys,
Found the issue you are absolutey right.
Wednesday, October 17, 2018 11:21 AM