Hi lingers,
Check this example. Now please take its reference and correct your code.
HTML
Home
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
<asp:Button Text="Update" runat="server" OnClick="OnUpdate" />
Default
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>' Width="120"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:TextBox ID="txtCountry" runat="server" Text='<%# Bind("Country") %>' Width="100"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age">
<ItemTemplate>
<asp:TextBox ID="txtAge" runat="server" Width="50"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Height">
<ItemTemplate>
<asp:TextBox ID="txtHeight" runat="server" Width="50"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button Text="Update" runat="server" OnClick="OnUpdate" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
Home
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
protected void OnUpdate(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3]
{
new DataColumn("Id"),
new DataColumn("Name"),
new DataColumn("Country")
});
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
string id = row.Cells[0].Text;
string name = row.Cells[1].Text;
string country = row.Cells[2].Text;
dt.Rows.Add(id, name, country);
}
}
Session["Data"] = dt;
Response.Redirect("Default.aspx");
}
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"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
Default
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (Session["Data"] != null)
{
DataTable dt = Session["Data"] as DataTable;
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
protected void OnUpdate(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
string id = row.Cells[0].Text;
string name = (row.FindControl("txtName") as TextBox).Text.Trim();
string country = (row.FindControl("txtCountry") as TextBox).Text.Trim();
string age = (row.FindControl("txtAge") as TextBox).Text.Trim();
string height = (row.FindControl("txtHeight") as TextBox).Text.Trim();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "UPDATE Customers SET Name = @Name, Country = @Country, Age = @Age, Height=@Height WHERE CustomerId = @CustomerId";
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = query;
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@CustomerId", id);
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Country", country);
cmd.Parameters.AddWithValue("@Age", age);
cmd.Parameters.AddWithValue("@Height", height);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
}
VB.Net
Home
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Protected Sub OnUpdate(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Country")})
For Each row As GridViewRow In GridView1.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim id As String = row.Cells(0).Text
Dim name As String = row.Cells(1).Text
Dim country As String = row.Cells(2).Text
dt.Rows.Add(id, name, country)
End If
Next
Session("Data") = dt
Response.Redirect("Default.aspx")
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")
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Using
End Using
End Using
End Sub
Default
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Me.IsPostBack Then
If Session("Data") IsNot Nothing Then
Dim dt As DataTable = TryCast(Session("Data"), DataTable)
GridView1.DataSource = dt
GridView1.DataBind()
End If
End If
End Sub
Protected Sub OnUpdate(ByVal sender As Object, ByVal e As EventArgs)
For Each row As GridViewRow In GridView1.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim id As String = row.Cells(0).Text
Dim name As String = (TryCast(row.FindControl("txtName"), TextBox)).Text.Trim()
Dim country As String = (TryCast(row.FindControl("txtCountry"), TextBox)).Text.Trim()
Dim age As String = (TryCast(row.FindControl("txtAge"), TextBox)).Text.Trim()
Dim height As String = (TryCast(row.FindControl("txtHeight"), TextBox)).Text.Trim()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Dim query As String = "UPDATE Customers SET Name = @Name, Country = @Country, Age = @Age, Height=@Height WHERE CustomerId = @CustomerId"
Using cmd As SqlCommand = New SqlCommand()
cmd.CommandText = query
cmd.Connection = con
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@CustomerId", id)
cmd.Parameters.AddWithValue("@Name", name)
cmd.Parameters.AddWithValue("@Country", country)
cmd.Parameters.AddWithValue("@Age", age)
cmd.Parameters.AddWithValue("@Height", height)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End If
Next
End Sub
Screenshot