Hi akhter,
Refer below sample.
HTML
Qty :
<asp:TextBox runat="server" ID="txtqty" />
Description :
<asp:DropDownList runat="server" ID="ddlItems">
</asp:DropDownList>
<asp:Button Text="Insert" runat="server" OnClick="GVadd_Click" />
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Descriptionitem" HeaderText="Descriptionitem" />
<asp:BoundField DataField="Codeitem" HeaderText="Codeitem" />
<asp:BoundField DataField="QTY" HeaderText="QTY" />
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Insert" runat="server" OnClick="Insert" />
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Code
C#
DataTable dt = new DataTable();
DataRow dr;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
dt.Columns.Add("Descriptionitem");
dt.Columns.Add("Codeitem");
dt.Columns.Add("Qty");
DataTable dtddl = new DataTable();
dtddl.Columns.Add("Descriptionitem");
dtddl.Columns.Add("Codeitem");
dtddl.Rows.Add("A", 1);
dtddl.Rows.Add("B", 2);
dtddl.Rows.Add("C", 3);
ddlItems.DataSource = dtddl;
ddlItems.DataTextField = "Descriptionitem";
ddlItems.DataValueField = "Codeitem";
ddlItems.DataBind();
ddlItems.Items.Insert(0, new ListItem("Select", "0"));
ViewState["dt"] = dt;
}
}
protected void GVadd_Click(object sender, EventArgs e)
{
dt = ViewState["dt"] as DataTable;
dr = dt.NewRow();
dr["Descriptionitem"] = ddlItems.SelectedItem.Text;
dr["Codeitem"] = ddlItems.SelectedItem.Value;
dr["QTY"] = txtqty.Text;
dt.Rows.Add(dr);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void Insert(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
foreach (GridViewRow row in GridView1.Rows)
{
SqlCommand cmd = new SqlCommand("INSERT INTO tblItems(Descriptionitem,Codeitem,QTY) VALUES(@Descriptionitem,@Codeitem,@QTY)", con);
cmd.Parameters.AddWithValue("@Descriptionitem", row.Cells[0].Text);
cmd.Parameters.AddWithValue("@Codeitem", row.Cells[1].Text);
cmd.Parameters.AddWithValue("@QTY", row.Cells[2].Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
VB.Net
Private dt As DataTable = New DataTable()
Private dr As DataRow
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
dt.Columns.Add("Descriptionitem")
dt.Columns.Add("Codeitem")
dt.Columns.Add("Qty")
Dim dtddl As DataTable = New DataTable()
dtddl.Columns.Add("Descriptionitem")
dtddl.Columns.Add("Codeitem")
dtddl.Rows.Add("A", 1)
dtddl.Rows.Add("B", 2)
dtddl.Rows.Add("C", 3)
ddlItems.DataSource = dtddl
ddlItems.DataTextField = "Descriptionitem"
ddlItems.DataValueField = "Codeitem"
ddlItems.DataBind()
ddlItems.Items.Insert(0, New ListItem("Select", "0"))
ViewState("dt") = dt
End If
End Sub
Protected Sub GVadd_Click(ByVal sender As Object, ByVal e As EventArgs)
dt = TryCast(ViewState("dt"), DataTable)
dr = dt.NewRow()
dr("Descriptionitem") = ddlItems.SelectedItem.Text
dr("Codeitem") = ddlItems.SelectedItem.Value
dr("QTY") = txtqty.Text
dt.Rows.Add(dr)
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim con As SqlConnection = New SqlConnection(constr)
For Each row As GridViewRow In GridView1.Rows
Dim cmd As SqlCommand = New SqlCommand("INSERT INTO tblItems(Descriptionitem,Codeitem,QTY) VALUES(@Descriptionitem,@Codeitem,@QTY)", con)
cmd.Parameters.AddWithValue("@Descriptionitem", row.Cells(0).Text)
cmd.Parameters.AddWithValue("@Codeitem", row.Cells(1).Text)
cmd.Parameters.AddWithValue("@QTY", row.Cells(2).Text)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
Next
End Sub