c# - Update Database From Check Box -
i have gridview itemtemplate field capturing id of checkbox checked, , have autopostback=false not updated immediately, , have asp:button want use c# run update statement update database of id's checked checkboxes. setting things keep getting error of
object reference not set instance of object
when button pressed, more line of code throws error:
string dbrowtoupdate = ((label)row.findcontrol("dataid")).text; and html\c# grid, it's source sql query , 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; } }
change offending line of code this:
label labeltoupdate = row.findcontrol("dataid") label; if (labeltoupdate) { string dbrowtoupdate = labeltoupdate.text; if (row.rowtype == datacontrolrowtype.datarow) { } } i'm willing bet labeltoupdate null won't loop. put breakpoint @ spot you're searching label control , inspect "row" variable if want see controls contained in row, changing use "as label" , checking null before getting .text should protect null reference moving forward.
Comments
Post a Comment