Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i'm trying to edit the information of Student table using gridview in c# but my code updates the gridview without changing the table in my sql database I don't know where is the problem
Here is my 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 this is the 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 FROM Student", conn);

        DataTable dt = new DataTable();
        dt.Load(cmd.ExecuteReader());
        GridView1.DataSource = dt; // now assign this datatable dt to gridview as 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 the 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 anyone help me why this change the gridview only but not the DB ?!
share|improve this question

1 Answer 1

I think the first problem I've seen in your 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 your database is Integer then you shouldn't be using ' character to indent the parameter sUserId it is just :

where S_ID = " + sUserId +")"
share|improve this answer
1  
Or better yet: use parameters! –  marc_s Apr 14 at 16:56
    
SqlCommand c = new SqlCommand("Update Student set S_Name= @s ,S_Email= @e where S_ID = @id"); c.Connection = conn; c.Parameters.AddWithValue("@s", name.Text); c.Parameters.AddWithValue("@e", email.Text); c.Parameters.AddWithValue("@id", sUserId); c.ExecuteNonQuery(); noting changed :( –  Maha Apr 14 at 17:05
    
@marc_s please check my comment –  Maha Apr 15 at 10:29
    
I have met this problem long time ago, but I forgot how I resolve it, but my suggest is you should be using CommandName and commandAgrument –  hieuhtgc00378 Apr 16 at 4:18
    
try using this: –  hieuhtgc00378 Apr 16 at 4:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.