Hi lingers,
Check this example. Now please take its reference and correct your code.
HTML
<asp:TextBox ID="txtNo" runat="server" />
<asp:Button ID="Button1" Text="Add" OnClick="Add" runat="server" />
<hr />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Eval("FirstName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:TextBox ID="txtLastName" runat="server" Text='<%# Eval("LastName") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button Text="Save" runat="server" OnClick="OnSave" />
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (this.GridView1.Rows.Count > 0 && Session["myDatatable"] != null)
{
DataTable dt = (DataTable)Session["myDatatable"];
dt.Clear();
foreach (GridViewRow row in this.GridView1.Rows)
{
string firstName = (row.FindControl("txtFirstName") as TextBox).Text;
string lastName = (row.FindControl("txtLastName") as TextBox).Text;
dt.Rows.Add(firstName, lastName);
}
GridView1.Visible = true;
GridView1.DataSource = dt;
GridView1.DataBind();
Session["myDatatable"] = dt;
}
}
public void AddNewData()
{
DataTable dt = new DataTable();
if (Session["myDatatable"] != null)
{
dt = (DataTable)Session["myDatatable"];
}
else
{
dt.Columns.Add("FirstName");
dt.Columns.Add("LastName");
}
DataRow drow = dt.NewRow();
drow["FirstName"] = "";
drow["LastName"] = "";
dt.Rows.Add(drow);
Session["myDatatable"] = dt;
}
public void BindGridview()
{
if (Session["myDatatable"] != null)
{
DataTable dt = (DataTable)Session["myDatatable"];
if ((dt != null) && (dt.Rows.Count > 0))
{
GridView1.Visible = true;
GridView1.DataSource = dt;
GridView1.DataBind();
}
else
{
GridView1.Visible = false;
}
}
}
protected void Add(object sender, EventArgs e)
{
for (int i = 0; i < Convert.ToInt32(txtNo.Text); i++)
{
AddNewData();
}
BindGridview();
}
protected void OnSave(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
string firstName = (row.FindControl("txtFirstName") as TextBox).Text;
string lastName = (row.FindControl("txtLastName") as TextBox).Text;
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "INSERT INTO Student VALUES(@First,@Last)";
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@First",firstName);
cmd.Parameters.AddWithValue("@Last", lastName);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Me.GridView1.Rows.Count > 0 AndAlso Session("myDatatable") IsNot Nothing Then
Dim dt As DataTable = CType(Session("myDatatable"), DataTable)
dt.Clear()
For Each row As GridViewRow In Me.GridView1.Rows
Dim firstName As String = (TryCast(row.FindControl("txtFirstName"), TextBox)).Text
Dim lastName As String = (TryCast(row.FindControl("txtLastName"), TextBox)).Text
dt.Rows.Add(firstName, lastName)
Next
GridView1.Visible = True
GridView1.DataSource = dt
GridView1.DataBind()
Session("myDatatable") = dt
End If
End Sub
Public Sub AddNewData()
Dim dt As DataTable = New DataTable()
If Session("myDatatable") IsNot Nothing Then
dt = CType(Session("myDatatable"), DataTable)
Else
dt.Columns.Add("FirstName")
dt.Columns.Add("LastName")
End If
Dim drow As DataRow = dt.NewRow()
drow("FirstName") = ""
drow("LastName") = ""
dt.Rows.Add(drow)
Session("myDatatable") = dt
End Sub
Public Sub BindGridview()
If Session("myDatatable") IsNot Nothing Then
Dim dt As DataTable = CType(Session("myDatatable"), DataTable)
If(dt IsNot Nothing) AndAlso(dt.Rows.Count > 0) Then
GridView1.Visible = True
GridView1.DataSource = dt
GridView1.DataBind()
Else
GridView1.Visible = False
End If
End If
End Sub
Protected Sub Add(ByVal sender As Object, ByVal e As EventArgs)
For i As Integer = 0 To Convert.ToInt32(txtNo.Text) -1
AddNewData()
Next
BindGridview()
End Sub
Protected Sub OnSave(ByVal sender As Object, ByVal e As EventArgs)
For Each row As GridViewRow In GridView1.Rows
Dim firstName As String = (TryCast(row.FindControl("txtFirstName"), TextBox)).Text
Dim lastName As String = (TryCast(row.FindControl("txtLastName"), TextBox)).Text
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "INSERT INTO Student VALUES(@First,@Last)"
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand(query)
cmd.Connection = con
cmd.Parameters.AddWithValue("@First", firstName)
cmd.Parameters.AddWithValue("@Last", lastName)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Next
End Sub
Screenshot