We help IT Professionals succeed at work.

Stored Proc. checkbox parameter in gridview

pascalmartin
pascalmartin asked
on
I have an editable gridview with a checkbox used to put a product in "Active" or "Inactive" mode.
The DB fields is a BIT type value.
When I try to update my gridview the checkbox does not update the field value??
Here is my code in the gridview:

<asp:TemplateField>
<HeaderTemplate>Act.</HeaderTemplate>
<ItemTemplate><%#IIf(Eval("Active"), "<font color='lime'><i>Yes</i></font>", "<font color='red'><i>No</i></font>")%></ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="chk1" runat="server" Width="40px" Text='<%# ReplaceBoolValue(Eval("Active"))%>' Checked='<%# Eval("Active") %>' EnableViewState="true" />
</EditItemTemplate>      
</asp:TemplateField>

Here is my parameter in code behind:

 myCom.Parameters.AddWithValue("@Active", SqlDbType.Bit)
        If CType(GridProducts.Rows(e.RowIndex).FindControl("chk1"), CheckBox).Checked = True Then
            myCom.Parameters("@Active").Value = 1
        Else
            myCom.Parameters("@Active").Value = 0
        End If

All other control inthe gridview are returning the correct values, except this checkbox.
Can you tell me what is wrong with my code?
Comment
Watch Question

Miguel OzSenior Software Engineer
Top Expert 2009

Commented:
From memory, you need to compare the value returned from function Eval. (The return value could not be used as Boolean variable)
Replace  Eval("Active") with:
Eval("Active").ToString() == "1"

Open in new window

Author

Commented:
Sorry Miguel this doesn't work, the procedure doesn't fire...
Miguel OzSenior Software Engineer
Top Expert 2009

Commented:
What is the value  of CType(GridProducts.Rows(e.RowIndex).FindControl("chk1"), CheckBox).Checked ? When the UI Checkbox is checked and when it is not.
Grvidview is very sensitive to where in the page lifecycle you are getting information. Could you post your complete page or a sample page with just your issue. I have VS2013/2010 and SQL server 2012
Hi there I solve the issue on my own.
Miguel OzSenior Software Engineer
Top Expert 2009

Commented:
No worries, please post your solution as the accepted answer and close the question.

Author

Commented:
I solved it by myself after trying many different solutions...