Hi akhter,
Refer below sample.
SQL
CREATE TABLE ItemMasterFile(CodeItem INT IDENTITY,Descriptionitem VARCHAR(50))
CREATE TABLE Pack (PID INT IDENTITY,PName VARCHAR(50))
CREATE TABLE #PackDetails (PDID INT IDENTITY,PID INT,CodeItem INT,QTY INT)
HTML
Descriptionitem:
<asp:TextBox runat="server" ID="txtDescriptionitem" />
<br />
PName :
<asp:TextBox ID="TextBox1" runat="server" ID="txtPname" />
<br />
QTY :
<asp:TextBox ID="txtQty" runat="server"></asp:TextBox>
Namespaces
C#
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Code
C#
protected void Save(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = null;
cmd = new SqlCommand("INSERT INTO ItemMasterFile(Descriptionitem) VALUES(@Descriptionitem)", con);
cmd.Parameters.AddWithValue("@Descriptionitem", txtDescriptionitem.Text);
con.Open();
cmd.ExecuteNonQuery();
cmd = new SqlCommand("INSERT INTO Pack(PName) VALUES(@PName)", con);
cmd.Parameters.AddWithValue("@PName", txtPname.Text);
cmd.ExecuteNonQuery();
cmd = new SqlCommand("SELECT CodeItem FROM ItemMasterFile WHERE Descriptionitem = @Descriptionitem", con);
cmd.Parameters.AddWithValue("@Descriptionitem", txtDescriptionitem.Text);
int codeItem = Convert.ToInt16(cmd.ExecuteScalar());
cmd = new SqlCommand("SELECT PID FROM Pack WHERE PName = @PName", con);
cmd.Parameters.AddWithValue("@PName", txtPname.Text);
int pid = Convert.ToInt16(cmd.ExecuteScalar());
cmd = new SqlCommand("INSERT INTO PackDetails(PID,CodeItem,QTY) VALUES(@PID,@CodeItem,@QTY)", con);
cmd.Parameters.AddWithValue("@PID", pid);
cmd.Parameters.AddWithValue("@CodeItem", codeItem);
cmd.Parameters.AddWithValue("@QTY", txtQty.Text);
cmd.ExecuteNonQuery();
con.Close();
}
VB.Net
Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim con As SqlConnection = New SqlConnection(constr)
Dim cmd As SqlCommand = Nothing
cmd = New SqlCommand("INSERT INTO ItemMasterFile(Descriptionitem) VALUES(@Descriptionitem)", con)
cmd.Parameters.AddWithValue("@Descriptionitem", txtDescriptionitem.Text)
con.Open()
cmd.ExecuteNonQuery()
cmd = New SqlCommand("INSERT INTO Pack(PName) VALUES(@PName)", con)
cmd.Parameters.AddWithValue("@PName", txtPname.Text)
cmd.ExecuteNonQuery()
cmd = New SqlCommand("SELECT CodeItem FROM ItemMasterFile WHERE Descriptionitem = @Descriptionitem", con)
cmd.Parameters.AddWithValue("@Descriptionitem", txtDescriptionitem.Text)
Dim codeItem As Integer = Convert.ToInt16(cmd.ExecuteScalar())
cmd = New SqlCommand("SELECT PID FROM Pack WHERE PName = @PName", con)
cmd.Parameters.AddWithValue("@PName", txtPname.Text)
Dim pid As Integer = Convert.ToInt16(cmd.ExecuteScalar())
cmd = New SqlCommand("INSERT INTO PackDetails(PID,CodeItem,QTY) VALUES(@PID,@CodeItem,@QTY)", con)
cmd.Parameters.AddWithValue("@PID", pid)
cmd.Parameters.AddWithValue("@CodeItem", codeItem)
cmd.Parameters.AddWithValue("@QTY", txtQty.Text)
cmd.ExecuteNonQuery()
con.Close()
End Sub