samedi 6 février 2016

Update Database From Check Box

I have a GridView with a ItemTemplate field that is capturing the ID of the checkbox that is checked, and I have AutoPostBack=false so that it is not updated immediately, and I have a asp:Button that I want to use C# to run an update statement to update a database with all of the ID's from the checked checkboxes. I am setting things up but I keep getting an error of

Object reference not set to an instance of an object

when the button is pressed, more specifically it is this line of code that throws the error:

string dbrowtoupdate = ((Label)row.FindControl("dataid")).Text;

And this is the HTML\C# for my grid, it's source is a SQL Query and it populates perfectly.

<tr>
<td valign="top">
    <asp:GridView runat="server" ID="gridviewtrythis" AutoGenerateColumns="false"  >
        <Columns>
        <asp:BoundField DataField="Name" HeaderText="Full Name" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label runat="server" Text='<%#Eval("dataid") %>'   ID="dataid" Visible="false"></asp:Label>
             </ItemTemplate>
             <ItemTemplate>
                <asp:CheckBox ID="cbox" runat="server" AutoPostBack="false" Checked='<%# Convert.ToBoolean(Eval("cbox")) %>' />
            </ItemTemplate>
        </asp:TemplateField>
        </Columns>
    </asp:GridView>                                
        <asp:Button runat="server" ID="btnUpdateDB" Text="Add" OnClick="btnUpdateDB_Click" />
</td>
</tr>

protected void btnUpdateDB_Click(object sender, EventArgs e)
{
try
{
    foreach (GridViewRow row in gridviewtrythis.Rows)
    {
        string dbrowtoupdate = ((Label)row.FindControl("dataid")).Text;
        if (row.RowType == DataControlRowType.DataRow)
        {
        }
    }
}
catch (Exception exception) { throw exception; }
}




Aucun commentaire:

Enregistrer un commentaire