-1

I have a grid in .aspx page where is three text fields like name,id,role and there is also one edit button where you click the value of this row will show in txtname,txtid,txtroll

This value i want to update and insert into database.

TheBoyan
  • 6,802
  • 3
  • 45
  • 61

1 Answers1

4
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Update")
    {
        string uname = "";
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow row = GridView1.Rows[index];
        TextBox txtbox1_val = (TextBox)row.FindControl("TxtChangeActive");
        uname = Server.HtmlDecode(row.Cells[1].Text.ToString());
        string upd_query = "update login set active='" + txtbox1_val.Text + "' where uname='" + uname + "' ";
        SqlConnection con = new SqlConnection(conn);
        con.Open();
        SqlCommand cmd = new SqlCommand(upd_query, con);
        cmd.ExecuteNonQuery();

        string query_login = "select * from Login";
        SqlDataAdapter da_login = new SqlDataAdapter(query_login, con);
        DataSet ds_login = new DataSet();
        da_login.Fill(ds_login);
        GridView1.DataSource = ds_login;
        GridView1.DataBind();
    }
}
 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

}

In Design page

  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand" 
    AlternatingRowStyle-BackColor="#006699"  
        AlternatingRowStyle-ForeColor="#FFFFFF" onrowupdating="GridView1_RowUpdating">
    <Columns >

    <asp:BoundField HeaderText="Name" DataField="uname" />
    <asp:BoundField HeaderText="Pass" DataField="upass"/>
    <asp:TemplateField>
    <HeaderTemplate>Active</HeaderTemplate>
    <ItemTemplate >
    <asp:TextBox ID="TxtChangeActive" runat="server" Text='<%#Eval("active")%>'></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateField>
     <asp:ButtonField CommandName="Update" Text="Update" />
    </Columns>
    </asp:GridView>
Prince Antony G
  • 932
  • 4
  • 18
  • 39
  • 1
    i know the code the update code bt I am unable to fetch the value in the text feild in same page and unable to update detail: i have to feild in aspx page like userid,password and submit button after submit it will shw in aspx page there is update button in grid where if you click same aspx will appear which hv userid,password feild where the value will be shown if you change the value here thn the value will be updated in databas that is my work if u can plzz send me the code – Joyeeta Biswas Dec 21 '11 at 07:50