ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Insert (save) data in Master and detail Table using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="akhter"> akhter</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>SQL</strong></span></p> <pre class="brush: 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)</pre> <p>&nbsp;<span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">Descriptionitem: &lt;asp:TextBox runat=&#34;server&#34; ID=&#34;txtDescriptionitem&#34; /&gt; &lt;br /&gt; PName : &lt;asp:TextBox ID=&#34;TextBox1&#34; runat=&#34;server&#34; ID=&#34;txtPname&#34; /&gt; &lt;br /&gt; QTY : &lt;asp:TextBox ID=&#34;txtQty&#34; runat=&#34;server&#34;&gt;&lt;/asp:TextBox&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data.SqlClient; using System.Configuration;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data.SqlClient</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Save(object sender, EventArgs e) { &nbsp;&nbsp;&nbsp; string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; &nbsp;&nbsp;&nbsp; SqlConnection con = new SqlConnection(constr); &nbsp;&nbsp;&nbsp; SqlCommand cmd = null; &nbsp;&nbsp;&nbsp; cmd = new SqlCommand(&#34;INSERT INTO ItemMasterFile(Descriptionitem) VALUES(@Descriptionitem)&#34;, con); &nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@Descriptionitem&#34;, txtDescriptionitem.Text); &nbsp;&nbsp;&nbsp; con.Open(); &nbsp;&nbsp;&nbsp; cmd.ExecuteNonQuery(); &nbsp;&nbsp;&nbsp; cmd = new SqlCommand(&#34;INSERT INTO Pack(PName) VALUES(@PName)&#34;, con); &nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@PName&#34;, txtPname.Text); &nbsp;&nbsp;&nbsp; cmd.ExecuteNonQuery(); &nbsp;&nbsp;&nbsp; cmd = new SqlCommand(&#34;SELECT CodeItem FROM&nbsp; ItemMasterFile WHERE Descriptionitem = @Descriptionitem&#34;, con); &nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@Descriptionitem&#34;, txtDescriptionitem.Text); &nbsp;&nbsp;&nbsp; int codeItem = Convert.ToInt16(cmd.ExecuteScalar()); &nbsp;&nbsp;&nbsp; cmd = new SqlCommand(&#34;SELECT PID FROM&nbsp; Pack WHERE PName = @PName&#34;, con); &nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@PName&#34;, txtPname.Text); &nbsp;&nbsp;&nbsp; int pid = Convert.ToInt16(cmd.ExecuteScalar()); &nbsp;&nbsp;&nbsp; cmd = new SqlCommand(&#34;INSERT INTO PackDetails(PID,CodeItem,QTY) VALUES(@PID,@CodeItem,@QTY)&#34;, con); &nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@PID&#34;, pid); &nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@CodeItem&#34;, codeItem); &nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@QTY&#34;, txtQty.Text); &nbsp;&nbsp;&nbsp; cmd.ExecuteNonQuery(); &nbsp;&nbsp;&nbsp; con.Close(); }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs) &nbsp;&nbsp;&nbsp; Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString &nbsp;&nbsp;&nbsp; Dim con As SqlConnection = New SqlConnection(constr) &nbsp;&nbsp;&nbsp; Dim cmd As SqlCommand = Nothing &nbsp;&nbsp;&nbsp; cmd = New SqlCommand(&#34;INSERT INTO ItemMasterFile(Descriptionitem) VALUES(@Descriptionitem)&#34;, con) &nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@Descriptionitem&#34;, txtDescriptionitem.Text) &nbsp;&nbsp;&nbsp; con.Open() &nbsp;&nbsp;&nbsp; cmd.ExecuteNonQuery() &nbsp;&nbsp;&nbsp; cmd = New SqlCommand(&#34;INSERT INTO Pack(PName) VALUES(@PName)&#34;, con) &nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@PName&#34;, txtPname.Text) &nbsp;&nbsp;&nbsp; cmd.ExecuteNonQuery() &nbsp;&nbsp;&nbsp; cmd = New SqlCommand(&#34;SELECT CodeItem FROM&nbsp; ItemMasterFile WHERE Descriptionitem = @Descriptionitem&#34;, con) &nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@Descriptionitem&#34;, txtDescriptionitem.Text) &nbsp;&nbsp;&nbsp; Dim codeItem As Integer = Convert.ToInt16(cmd.ExecuteScalar()) &nbsp;&nbsp;&nbsp; cmd = New SqlCommand(&#34;SELECT PID FROM&nbsp; Pack WHERE PName = @PName&#34;, con) &nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@PName&#34;, txtPname.Text) &nbsp;&nbsp;&nbsp; Dim pid As Integer = Convert.ToInt16(cmd.ExecuteScalar()) &nbsp;&nbsp;&nbsp; cmd = New SqlCommand(&#34;INSERT INTO PackDetails(PID,CodeItem,QTY) VALUES(@PID,@CodeItem,@QTY)&#34;, con) &nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@PID&#34;, pid) &nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@CodeItem&#34;, codeItem) &nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@QTY&#34;, txtQty.Text) &nbsp;&nbsp;&nbsp; cmd.ExecuteNonQuery() &nbsp;&nbsp;&nbsp; con.Close() End Sub</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/207826/Insert-save-data-in-Master-and-detail-Table-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/207826/Insert-save-data-in-Master-and-detail-Table-using-C-and-VBNet-in-ASPNet/Tue, 06 Aug 2019 05:52:28 GMT