c# - rowindex always starting from zero onwards...why? -
this code ..while editing , deleting row index taking 0 on-wards...delete command not @ working....if try edit 2nd row onwards working...delete command not @ working..i think because of row index..please 1 me in advance....
using system; using system.data; using system.configuration; using system.collections; using system.web; using system.web.security; using system.web.ui; using system.web.ui.webcontrols; using system.web.ui.webcontrols.webparts; using system.web.ui.htmlcontrols; using system.data.sqlclient; public partial class manager_payments : system.web.ui.page { //sqlconnection con =new sqlconnection("data source=sqlexpress;initial catalog=isoqrmssys;user id=sa;password=123456;integrated security=true"); business bl = new business(); //protected int64 stf_id, vmember; //protected datetime srdt; private system.drawing.color a; string mystr = configurationmanager.appsettings["connectionstring"].tostring(); protected void page_load(object sender, eventargs e) { loadgridview(); } private void loadgridview() { sqlconnection con = new sqlconnection(mystr); sqlcommand cmd = new sqlcommand("select * customerprofmain", con); //string sql = "select * customerprofmain"; sqldataadapter sda = new sqldataadapter(cmd); con.open(); dataset ds = new dataset(); sda.fill(ds); //return ds.tables[0]; grd_view.datasource = ds.tables[0]; grd_view.databind(); con.close(); } protected void grd_view_rowcommand(object sender, system.web.ui.webcontrols.gridviewcommandeventargs e) { int index = grd_view.selectedindex; if (e.commandname == "edit") { //string rowindex = int.parse(e.commandargument.tostring()); // session["rowid"] = rowindex; datatable dt = new datatable(); sqlconnection con = new sqlconnection(mystr); sqlcommand cmd = new sqlcommand("select * customerprofmain customercode='" + e.commandargument.tostring() + "'", con); sqldataadapter sda = new sqldataadapter(cmd); con.open(); dataset ds = new dataset(); sda.fill(ds); dt=ds.tables[0]; textbox1.text = dt.rows[0]["customername"].tostring(); textbox2.text=dt.rows[0]["address"].tostring(); textbox3.text=dt.rows[0]["tellno"].tostring(); textbox4.text=dt.rows[0]["faxno"].tostring(); textbox5.text=dt.rows[0]["email"].tostring(); button1.text = "update"; } if (e.commandname == "delete") { int rowindex = int.parse(e.commandargument.tostring()); session["rowid"] = rowindex; // datatable dt = new datatable(); sqlconnection con = new sqlconnection(mystr); sqlcommand cmd = new sqlcommand("delete customerprofmain customercode='" + rowindex + "' ", con); sqldataadapter sda = new sqldataadapter(cmd); con.open(); cmd.executenonquery(); con.close(); } } protected void grd_view_rowediting(object sender, gridviewediteventargs e) { } protected void grd_view_rowdatabound(object sender, gridviewroweventargs e) { } protected void grd_view_rowdeleting(object sender, gridviewdeleteeventargs e) { } protected void button1_click(object sender, eventargs e) { if (button1.text == "add") { string mystr = configurationmanager.appsettings["connectionstring"].tostring(); sqlconnection con = new sqlconnection(mystr); con.open(); string sql = string.empty; sql = "insert customerprofmain(customername,address,tellno,faxno,email) values('" + textbox1.text.trim() + "','" + textbox2.text.trim() + "','" + textbox3.text.trim() + "','" + textbox4.text.trim() + "','" + textbox5.text.trim() + "') "; sqlcommand cmd = new sqlcommand(sql, con); cmd.executenonquery(); con.close(); textbox1.text = ""; textbox2.text = ""; textbox3.text = ""; textbox4.text = ""; textbox5.text = ""; button1.text = "add"; loadgridview(); } if (button1.text == "update") { string mystr = configurationmanager.appsettings["connectionstring"].tostring(); sqlconnection con = new sqlconnection(mystr); con.open(); string sql = string.empty; sql = "update customerprofmain set customername='" + textbox1.text.trim() + "',address='" + textbox2.text.trim() + "',tellno='" + textbox3.text.trim() + "',faxno='" + textbox4.text.trim() + "',email='" + textbox5.text.trim() + "' customercode='" + session["rowid"] + "'"; sqlcommand cmd = new sqlcommand(sql, con); cmd.executenonquery(); con.close(); textbox1.text = ""; textbox2.text = ""; textbox3.text = ""; textbox4.text = ""; textbox5.text = ""; button1.text = "add"; loadgridview(); } } } "<asp:gridview id="grd_view" showfooter="true" runat="server" onrowediting="grd_view_rowediting" autogeneratecolumns="false" datakeynames="customercode" cellpadding="4" onrowcommand="grd_view_rowcommand" gridlines="none" allowpaging="true" allowsorting="true" cssclass="style2" forecolor="#333333" width="569px" onrowdatabound="grd_view_rowdatabound" onrowdeleting="grd_view_rowdeleting"> <footerstyle backcolor="#555555" forecolor="white" font-bold="true" /> <columns> <asp:boundfield datafield="customercode" headertext="customercode" insertvisible="false" readonly="true" sortexpression="customercode" /> <asp:boundfield datafield="customername" headertext="customername" sortexpression="customername" /> <asp:boundfield datafield="address" headertext="address" sortexpression="address" /> <asp:boundfield datafield="tellno" headertext="tellno" sortexpression="tellno" /> <asp:boundfield datafield="faxno" headertext="faxno" sortexpression="faxno" /> <asp:boundfield datafield="email" headertext="email" sortexpression="email" /> <asp:commandfield showeditbutton="true" selecttext="edit" /> <asp:commandfield showdeletebutton="true" selecttext="delete" /> </columns> <rowstyle backcolor="#f7f6f3" forecolor="#333333" /> <selectedrowstyle backcolor="#e2ded6" font-bold="true" forecolor="#333333" /> <pagerstyle backcolor="#777777" forecolor="white" horizontalalign="center" /> <headerstyle backcolor="#555555" font-bold="true" forecolor="white" /> <editrowstyle backcolor="#999999" /> <alternatingrowstyle backcolor="white" forecolor="#284775" /> </asp:gridview> "
replace grid view code
<asp:gridview id="grd_view" showfooter="true" runat="server" onrowediting="grd_view_rowediting" autogeneratecolumns="false" datakeynames="customercode" cellpadding="4" onrowcommand="grd_view_rowcommand" gridlines="none" allowpaging="true" allowsorting="true" cssclass="style2" forecolor="#333333" width="569px" onrowdatabound="grd_view_rowdatabound" onrowdeleting="grd_view_rowdeleting"> <footerstyle backcolor="#555555" forecolor="white" font-bold="true" /> <columns> <asp:boundfield datafield="customercode" headertext="customercode" insertvisible="false" readonly="true" sortexpression="customercode" /> <asp:boundfield datafield="customername" headertext="customername" sortexpression="customername" /> <asp:boundfield datafield="address" headertext="address" sortexpression="address" /> <asp:boundfield datafield="tellno" headertext="tellno" sortexpression="tellno" /> <asp:templatefield> <itemtemplate> <asp:linkbutton id="btnedit" runat="server" commandargument='<%#eval("customercode")%>' commandname="edit" text="edit"> </asp:linkbutton> </itemtemplate> </asp:templatefield> <asp:templatefield> <itemtemplate> <asp:linkbutton id="btndelete" runat="server" commandargument='<%#eval("customercode")%>' commandname="delete" text="delete"> </asp:linkbutton> </itemtemplate> </asp:templatefield> </columns> <rowstyle backcolor="#f7f6f3" forecolor="#333333" /> <selectedrowstyle backcolor="#e2ded6" font-bold="true" forecolor="#333333" /> <pagerstyle backcolor="#777777" forecolor="white" horizontalalign="center" /> <headerstyle backcolor="#555555" font-bold="true" forecolor="white" /> <editrowstyle backcolor="#999999" /> <alternatingrowstyle backcolor="white" forecolor="#284775" /> </asp:gridview> you didn't set commandargument here set commandargument='<%#eval("customercode")%>' edit button , delete button
Comments
Post a Comment