Hi lingers,
Refer the updated 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" Width="315px">
<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>
<asp:TemplateField HeaderText="Age">
<ItemTemplate>
<asp:DropDownList ID="ageperson" runat="server" Height="25px"
Style="margin-left: 0px" Width="416px"
AutoPostBack="True" Font-Names="Century Gothic" Font-Bold="True">
<asp:ListItem Selected="True" Value="1">Offline Die</asp:ListItem>
<asp:ListItem Value="3">Cylinder</asp:ListItem>
<asp:ListItem Value="4">Cutting Die</asp:ListItem>
<asp:ListItem Value="6">Embossing</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button 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#
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;
string age = (row.FindControl("ageperson") as DropDownList).SelectedItem.Text;
dt.Rows.Add(firstName, lastName, age);
(row.FindControl("ageperson") as DropDownList).Items.FindByText(age).Selected = true;
}
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");
dt.Columns.Add("age");
}
DataRow drow = dt.NewRow();
drow["FirstName"] = "";
drow["LastName"] = "";
drow["age"] = "";
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 age = (row.FindControl("ageperson") as DropDownList).SelectedItem.Text;
if (!string.IsNullOrEmpty(firstName) && !string.IsNullOrEmpty(lastName))
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "INSERT INTO Student VALUES(@First,@Last,@age)";
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);
cmd.Parameters.AddWithValue("@age", age);
con.Open();
cmd.ExecuteNonQuery();
}
con.Close();
}
}
}
Session["myDatatable"] = null;
Response.Redirect("material.aspx");
}
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
Dim age As String = (TryCast(row.FindControl("ageperson"), DropDownList)).SelectedItem.Text
dt.Rows.Add(firstName, lastName, age)
TryCast(row.FindControl("ageperson"), DropDownList).Items.FindByText(age).Selected = True
Next
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")
dt.Columns.Add("age")
End If
Dim drow As DataRow = dt.NewRow()
drow("FirstName") = ""
drow("LastName") = ""
drow("age") = ""
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 age As String = TryCast(row.FindControl("ageperson"), DropDownList).SelectedItem.Text
If Not String.IsNullOrEmpty(firstName) AndAlso Not String.IsNullOrEmpty(lastName) Then
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "INSERT INTO Student VALUES(@First,@Last,@age)"
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)
cmd.Parameters.AddWithValue("@age", age)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End If
Next
Session("myDatatable") = Nothing
Response.Redirect("material.aspx")
End Sub