Hi indradeo,
Check this sample. now take its reference.
HTML
First.aspx
<asp:TextBox ID="txtID" runat="server" />
<asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="OnSubmit" />
Second.aspx
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false" OnRowEditing="OnRowEditing"
OnRowCancelingEdit="OnRowCancellingEdit" OnRowUpdating="OnRowUpdating">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID" Text='<%# Eval("ID") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" Text='<%# Eval("Name") %>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" Text='<%# Eval("Name") %>' runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<asp:Label ID="lblAmount" Text='<%# Eval("Amount") %>' runat="server" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtAmount" Text='<%# Eval("Amount") %>' runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" />
</Columns>
</asp:GridView>
<asp:Button ID="btnSave" Text="Save" runat="server" OnClick="OnSave" />
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#
FirstPage.cs
protected void OnSubmit(object sender, EventArgs e)
{
string id = txtID.Text.Trim();
Response.Redirect("SecondPage.aspx?ID=" + id);
}
SecondPage.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (Request.QueryString["ID"] != "")
{
this.BndDetails();
}
}
}
protected void OnSave(object sender, EventArgs e)
{
foreach (GridViewRow row in gvDetails.Rows)
{
string id = (row.FindControl("lblID") as Label).Text;
string name = (row.FindControl("lblName") as Label).Text;
string amount = (row.FindControl("lblAmount") as Label).Text;
this.Insert(id, name, amount);
}
}
protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
DataTable dt = new DataTable();
dt = ViewState["dt"] as DataTable;
this.gvDetails.EditIndex = e.NewEditIndex;
this.BndDetails();
}
protected void OnRowCancellingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.gvDetails.EditIndex = -1;
this.BndDetails();
}
protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
DataTable dt = ViewState["dt"] as DataTable;
GridViewRow row = gvDetails.Rows[e.RowIndex];
string id = (row.FindControl("lblID") as Label).Text;
string name = (row.FindControl("txtName") as TextBox).Text;
string amount = (row.FindControl("txtAmount") as TextBox).Text;
dt.Rows[row.RowIndex]["Name"] = name;
dt.Rows[row.RowIndex]["Amount"] = amount;
ViewState["dt"] = dt;
this.gvDetails.EditIndex = -1;
this.BndDetails();
}
private void BndDetails()
{
DataTable dt = new DataTable();
if (ViewState["dt"] == null)
{
int id = Convert.ToInt32(Request.QueryString["ID"]);
dt.Columns.Add("ID");
dt.Columns.Add("Name");
dt.Columns.Add("Amount");
dt.Rows.Add(id, "", "");
ViewState["dt"] = dt;
}
else
{
dt = ViewState["dt"] as DataTable;
}
this.gvDetails.DataSource = ViewState["dt"] as DataTable;
this.gvDetails.DataBind();
}
private void Insert(string id, string name, string amount)
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Employee VALUES(@ID,@Name,@Amount)", con))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@ID", id);
cmd.Parameters.AddWithValue("@Name", name);
cmd.Parameters.AddWithValue("@Amount", amount);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
VB.Net
FirstPage.asx.vb
Protected Sub OnSubmit(ByVal sender As Object, ByVal e As EventArgs)
Dim id As String = txtID.Text.Trim()
Response.Redirect("SecondPage.aspx?ID=" & id)
End Sub
SecondPage.aspx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
If Request.QueryString("ID") <> "" Then
Me.BndDetails()
End If
End If
End Sub
Protected Sub OnSave(ByVal sender As Object, ByVal e As EventArgs)
For Each row As GridViewRow In gvDetails.Rows
Dim id As String = (TryCast(row.FindControl("lblID"), Label)).Text
Dim name As String = (TryCast(row.FindControl("lblName"), Label)).Text
Dim amount As String = (TryCast(row.FindControl("lblAmount"), Label)).Text
Me.Insert(id, name, amount)
Next
End Sub
Protected Sub OnRowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
Dim dt As DataTable = New DataTable()
dt = TryCast(ViewState("dt"), DataTable)
Me.gvDetails.EditIndex = e.NewEditIndex
Me.BndDetails()
End Sub
Protected Sub OnRowCancellingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
Me.gvDetails.EditIndex = -1
Me.BndDetails()
End Sub
Protected Sub OnRowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Dim dt As DataTable = TryCast(ViewState("dt"), DataTable)
Dim row As GridViewRow = gvDetails.Rows(e.RowIndex)
Dim id As String = (TryCast(row.FindControl("lblID"), Label)).Text
Dim name As String = (TryCast(row.FindControl("txtName"), TextBox)).Text
Dim amount As String = (TryCast(row.FindControl("txtAmount"), TextBox)).Text
dt.Rows(row.RowIndex)("Name") = name
dt.Rows(row.RowIndex)("Amount") = amount
ViewState("dt") = dt
Me.gvDetails.EditIndex = -1
Me.BndDetails()
End Sub
Private Sub BndDetails()
Dim dt As DataTable = New DataTable()
If ViewState("dt") Is Nothing Then
Dim id As Integer = Convert.ToInt32(Request.QueryString("ID"))
dt.Columns.Add("ID")
dt.Columns.Add("Name")
dt.Columns.Add("Amount")
dt.Rows.Add(id, "", "")
ViewState("dt") = dt
Else
dt = TryCast(ViewState("dt"), DataTable)
End If
Me.gvDetails.DataSource = TryCast(ViewState("dt"), DataTable)
Me.gvDetails.DataBind()
End Sub
Private Sub Insert(ByVal id As String, ByVal name As String, ByVal amount As String)
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO Employee VALUES(@ID,@Name,@Amount)", con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@ID", id)
cmd.Parameters.AddWithValue("@Name", name)
cmd.Parameters.AddWithValue("@Amount", amount)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub
Screenshot