i have gridview pulling data want added button when button clicked updates lastleak date set... know query works in sql not sure missing make work...
<asp:gridview id="gridview1" runat="server" autogeneratecolumns="false" datasourceid="lastleakcheck" width="850px"> <columns> <asp:boundfield datafield="customername" headertext="customer name" sortexpression="customername" /> <asp:boundfield datafield="acctnum" headertext="acct num" sortexpression="acctnum" /> <asp:boundfield datafield="phonenum" headertext="phone num" sortexpression="phonenum" /> <asp:boundfield datafield="city" headertext="city" sortexpression="city" /> <asp:boundfield datafield="address" headertext="address" sortexpression="address" /> <asp:boundfield datafield="lastleak" headertext="last leak" sortexpression="lastleak" /> <asp:buttonfield buttontype="button" commandname="update" headertext="update date" showheader="true" text="completed" /> </columns> </asp:gridview> <asp:sqldatasource id="lastleakcheck" runat="server" connectionstring="data source=server;initial catalog=propane;user id=user;password=pass;integrated security=true" selectcommand="select customername, acctnum, phonenum, city, address, lastleak custinfo lastleak <= convert(datetime, '4-6-2012' ) order convert(datetime, lastleak) asc" updatecommand = "update custinfo set lastleak='4/5/2017'where customername='@customername';"></asp:sqldatasource>
here button click:
cn = new sqlconnection(@"data source=server;initial catalog=propane;user id=id;password=pass;integrated security=true"); cmd = new sqlcommand("update custinfo set lastleak='4/5/2017' customername='@customername'", cn); cn.open(); cmd.executenonquery();
here's complete solution have create web page , data table , tested this. uploaded 1 of website can download if needed
<asp:gridview id="gridview1" runat="server" autogeneratecolumns="false" width="850px" onrowcommand="gridview1_rowcommand"> <columns> <asp:boundfield datafield="customername" headertext="customername" sortexpression="customername" /> <asp:boundfield datafield="acctnum" headertext="acctnum" sortexpression="acctnum" /> <asp:boundfield datafield="phonenum" headertext="phonenum" sortexpression="phonenum" /> <asp:boundfield datafield="city" headertext="city" sortexpression="city" /> <asp:boundfield datafield="address" headertext="address" sortexpression="address" /> <asp:boundfield datafield="lastleak" headertext="lastleak" sortexpression="lastleak" /> <asp:buttonfield buttontype="button" commandname="updateleak" headertext="update date" showheader="true" text="completed" /> </columns> </asp:gridview>
page load
protected void page_load(object sender, eventargs e) { if (!ispostback) { bindleaks(); } }
bind leaks grid
private void bindleaks() { using (sqlconnection conn = new sqlconnection(configurationmanager.connectionstrings["leakconnection"].connectionstring)) { sqldataadapter da = new sqldataadapter("select customername, acctnum, phonenum, city, address, lastleak leak lastleak >= convert(datetime, '4-6-2012') order lastleak asc", conn); dataset dsleaks = new dataset(); conn.open(); da.fill(dsleaks); conn.close(); gridview1.datasource = dsleaks; gridview1.databind(); } }
row command event update data
protected void gridview1_rowcommand(object sender, gridviewcommandeventargs e) { if (e.commandname == "updateleak") { if (e.commandargument != null) { int rowid = convert.toint32(e.commandargument); gridviewrow row = gridview1.rows[rowid]; using (sqlconnection conn = new sqlconnection(configurationmanager.connectionstrings["leakconnection"].connectionstring)) { sqlcommand cmd = new sqlcommand("update leak set lastleak='4/5/2017' customername=@customername", conn); if (e.commandargument != null) cmd.parameters.addwithvalue("@customername", row.cells[0].text.trim()); conn.open(); cmd.executenonquery(); conn.close(); bindleaks(); } } } }
Comments
Post a Comment