Edit Sql Database Using Gridview Asp.net c# -


i'm trying edit information of student table using gridview in c# code updates gridview without changing table in sql database don't know problem
here gridview :

<asp:gridview id="gridview1" runat="server" autogeneratecolumns="false"                datakeynames="s_id"  onrowcancelingedit="gridview1_rowcancelingedit"             onrowediting="gridview1_rowediting" onrowupdating="gridview1_rowupdating" allowpaging="true" >               <columns>                <asp:templatefield headertext="id">                     <edititemtemplate>                         <asp:textbox id="s_id" runat="server" text='<%# eval("s_id") %>'></asp:textbox>                     </edititemtemplate>                     <itemtemplate>                         <asp:label id="label1" runat="server" text='<%# bind("s_id") %>'></asp:label>                     </itemtemplate>                 </asp:templatefield>                  <asp:templatefield headertext="name">                     <edititemtemplate>                         <asp:textbox id="s_name" runat="server" text='<%# eval("s_name") %>'></asp:textbox>                     </edititemtemplate>                     <itemtemplate>                         <asp:label id="label1" runat="server" text='<%# bind("s_name") %>'></asp:label>                     </itemtemplate>                 </asp:templatefield>                  <asp:templatefield headertext="email">                     <edititemtemplate>                         <asp:textbox id="s_email" runat="server" text='<%# eval("s_email") %>'></asp:textbox>                     </edititemtemplate>                     <itemtemplate>                         <asp:label id="s_email" runat="server" text='<%# bind("s_email") %>'></asp:label>                     </itemtemplate>                 </asp:templatefield>                    <asp:commandfield showeditbutton="true" />               </columns>           </asp:gridview> 

and code behind:

protected void gvbind()     {         sqlconnection conn = new sqlconnection(@"myconnection");         conn.open();         sqlcommand cmd = new sqlcommand("select s_id , s_name , s_email , b_id student", conn);          datatable dt = new datatable();         dt.load(cmd.executereader());         gridview1.datasource = dt; // assign datatable dt gridview datasource         gridview1.databind();     }     protected void page_load(object sender, eventargs e)     {         if (!this.ispostback)         {             gvbind();         }     }      protected void gridview1_rowediting(object sender, gridviewediteventargs e)     {         gridview1.editindex = e.neweditindex;         gvbind();     }      protected void gridview1_rowupdating(object sender, gridviewupdateeventargs e)     {           int suserid = convert.toint32(gridview1.datakeys[e.rowindex].value.tostring());         textbox name = (textbox)gridview1.rows[e.rowindex].findcontrol("s_name"); //give edititem template id         textbox email = (textbox)gridview1.rows[e.rowindex].findcontrol("s_email");         try         {             sqlconnection conn = new sqlconnection(@"myconnection");             conn.open();             sqlcommand c = new sqlcommand("update student set s_name='" + name.text + "',s_email='" + email.text + "'where s_id='" + suserid + "'");             c.connection = conn;             c.executenonquery();              gridview1.editindex = -1;             gvbind();         }         catch (exception ex)         {             page.clientscript.registerstartupscript(typeof(string), "key", "alert('error : \\n" + ex.message + "');", true);         }     }      protected void gridview1_rowcancelingedit(object sender, gridviewcancelediteventargs e)     {         gridview1.editindex = -1;         gvbind();     }  please can me why change gridview not db ?! 

i think first problem i've seen in code is:

sqlcommand c = new sqlcommand("update student set s_name='" + name.text + "',s_email='" + email.text + "'where s_id='" + suserid + "'"); 

if s_id column in database integer shouldn't using ' character indent parameter suserid :

where s_id = " + suserid +")" 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -