Hi ArunaAbi,
Please refer below sample.
HTML
CustomerId
<br />
<asp:TextBox ID="txtCustomerId" runat="server" ReadOnly="true"></asp:TextBox>
<br />
Name
<br />
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br />
Country
<br />
<asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnupdate" runat="server" Text="Update" OnClick="btnupdate_Click1" />
<br />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="CustomerId" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" Text="View" runat="server" OnClick="View" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
da.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
}
protected void btnupdate_Click1(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("UPDATE Customers SET Name = @Name, Country = @Country WHERE CustomerId = @CustomerId", con))
{
con.Open();
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Country", txtCountry.Text);
cmd.Parameters.AddWithValue("@CustomerId", txtCustomerId.Text);
cmd.ExecuteNonQuery();
con.Close();
this.BindGrid();
}
}
}
protected void View(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow row = (GridViewRow)btn.NamingContainer;
txtCustomerId.Text = row.Cells[0].Text;
txtName.Text = row.Cells[1].Text;
txtCountry.Text = row.Cells[2].Text;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId, Name, Country FROM Customers", con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Using
End Using
End Using
End Sub
Protected Sub btnupdate_Click1(ByVal sender As Object, ByVal e As EventArgs)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("UPDATE Customers SET Name = @Name, Country = @Country WHERE CustomerId = @CustomerId", con)
con.Open()
cmd.Parameters.AddWithValue("@Name", txtName.Text)
cmd.Parameters.AddWithValue("@Country", txtCountry.Text)
cmd.Parameters.AddWithValue("@CustomerId", txtCustomerId.Text)
cmd.ExecuteNonQuery()
con.Close()
Me.BindGrid()
End Using
End Using
End Sub
Protected Sub View(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button = CType(sender, Button)
Dim row As GridViewRow = CType(btn.NamingContainer, GridViewRow)
txtCustomerId.Text = row.Cells(0).Text
txtName.Text = row.Cells(1).Text
txtCountry.Text = row.Cells(2).Text
End Sub
Screenshot
