This Way:
HTML:
<form id="form1" runat="server">
<div>
<table id="tbStudent" runat="server" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
Student Id
</td>
<td>
<asp:TextBox ID="txtId" runat="server" />
</td>
</tr>
<tr>
<td>
Student Name
</td>
<td>
<asp:TextBox ID="txtStudentName" runat="server" />
</td>
</tr>
<tr>
<td>
Course
</td>
<td>
<asp:DropDownList ID="ddlCourses" runat="server">
<asp:ListItem Text="IT" Value="0">
</asp:ListItem>
<asp:ListItem Text="CS" Value="1">
</asp:ListItem>
<asp:ListItem Text="ME" Value="2">
</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
Address
</td>
<td>
<asp:TextBox ID="txtAddress" runat="server" TextMode="MultiLine" />
</td>
</tr>
</table>
<asp:Button ID="btnSave" Text="Save" runat="server" OnClick="Save" />
<asp:Button ID="btnUpdate" Text="Update" runat="server" Visible="false" OnClick="Update" />
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
runat="server" AutoGenerateColumns="false" DataKeyNames="RollNumber" ShowFooter="true"
OnRowEditing="GridView1_RowEditing" OnRowDeleting="GridView1_OnRowDeleting">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblRollNumber" runat="server" Text='<%# Eval("RollNumber") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="StudentName" HeaderText="Student name" />
<asp:BoundField DataField="Course" HeaderText="Course" />
<asp:BoundField DataField="Address" HeaderText="Address" />
<asp:ButtonField CommandName="Edit" ButtonType="Button" Text="Edit" />
<asp:ButtonField CommandName="Delete" ButtonType="Button" Text="Delete" />
</Columns>
</asp:GridView>
</div>
</form>
C#:
private void PopulateStudent()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string sqlStatment = "SELECT * FROM Students";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sqlStatment, con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
da.Fill(ds);
this.GridView1.DataSource = ds;
this.GridView1.DataBind();
}
}
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.btnUpdate.Visible = true;
this.btnSave.Visible = false;
this.GridView1.EditIndex = e.NewEditIndex;
Label lbl = (Label)GridView1.Rows[e.NewEditIndex].Cells[0].FindControl("lblRollNumber");
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string sqlStatment = "SELECT * FROM Students WHERE RollNumber=@RollNumber";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sqlStatment, con))
{
cmd.Parameters.AddWithValue("@RollNumber", Convert.ToInt32(lbl.Text));
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
da.Fill(ds);
this.txtStudentName.Text = ds.Tables[0].Rows[0]["StudentName"].ToString();
this.txtId.Text = ds.Tables[0].Rows[0]["RollNumber"].ToString();
this.ddlCourses.SelectedItem.Text = ds.Tables[0].Rows[0]["Course"].ToString();
this.txtAddress.Text = ds.Tables[0].Rows[0]["Address"].ToString();
this.GridView1.EditIndex = -1;
}
}
}
}
protected void Save(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string sqlStatment = "INSERT INTO Students values(@RollNumbeer,@StudentName,@Course,@Address)";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sqlStatment, con))
{
con.Open();
cmd.Parameters.AddWithValue("@RollNumbeer", int.Parse(this.txtId.Text.Trim()));
cmd.Parameters.AddWithValue("@StudentName", this.txtStudentName.Text.Trim());
cmd.Parameters.AddWithValue("@Course", this.ddlCourses.SelectedItem.Text);
cmd.Parameters.AddWithValue("@Address", this.txtAddress.Text.Trim());
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
this.PopulateStudent();
}
}
}
protected void Update(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string sqlStatment = "UPDATE Students Set RollNumber =@RollNumbeer ,StudentName= @StudentName,Course= @Course,Address = @Address WHERE RollNumber= @RollNumbeer";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sqlStatment, con))
{
con.Open();
cmd.Parameters.AddWithValue("@RollNumbeer", int.Parse(this.txtId.Text.Trim()));
cmd.Parameters.AddWithValue("@StudentName", this.txtStudentName.Text.Trim());
cmd.Parameters.AddWithValue("@Course", this.ddlCourses.SelectedItem.Text);
cmd.Parameters.AddWithValue("@Address", this.txtAddress.Text.Trim());
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
this.PopulateStudent();
}
}
}
protected void GridView1_OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
Label lbl = (Label)GridView1.Rows[e.RowIndex].Cells[0].FindControl("lblRollNumber");
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string sqlStatment = "DELETE FROM Students WHERE RollNumber = @RollNumbeer";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sqlStatment, con))
{
con.Open();
cmd.Parameters.AddWithValue("@RollNumbeer", int.Parse(lbl.Text));
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
this.PopulateStudent();
}
}
}
SQL:
CREATE TABLE [dbo].[Students](
[RollNumber] [int] NOT NULL,
[StudentName] [varchar](30) NULL,
[Course] [varchar](30) NULL,
[Address] [varchar](30) NULL,
CONSTRAINT [PK_Students] PRIMARY KEY CLUSTERED
(
[RollNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
Image:
When you are updating the record the save button will be hidden and Update button will be visible.
Thank You.