c# - Button Click in Gridview to Update SQL value -
i have gridview pulling values customername, acctnum, phonenum, city, address , lastleak. data pulls great pull lastleak thats 5 years or older. have button on right once driver check hits button , row updates lastleak today's date. reason can't work know has close can shine light.
here gridview:
<asp:gridview id="gridview1" runat="server" autogeneratecolumns="false" datasourceid="lastleakcheck" width="950px" 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="updatedate" 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=id;password=pass;integrated security=true" selectcommand="select customername, acctnum, phonenum, city, address, lastleak custinfowhere lastleak <= convert(datetime, '4-6-2012' )order convert(datetime, lastleak) asc" ></asp:sqldatasource>
and click event row:
protected void gridview1_rowcommand(object sender, gridviewcommandeventargs e) {
if (e.commandname == "updatedate") { int index = convert.toint32(e.commandargument); gridviewrow row = gridview1.rows[index]; 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/7/2017' customername='@customername'", cn); cmd.parameters.addwithvalue("@customername", row.cells[0].text); cn.open(); cmd.executenonquery(); cn.close(); gridview1.databind(); } }
Comments
Post a Comment