I want to insert data on to two different tables in SQL server from VB.net. I have Identity increment set up in SQL that is where the order# comes from. The rest of the data comes from vb.net. Another issue is that every time I capture an order, everything from the Order table copies to the Order Details table which causes a lot of unnecessary duplicated rows. Here is my current code in VB:
Private Sub btnGetTotal_Click(sender As Object, e As EventArgs) Handles btnGetTotal.Click
Dim connection As SqlConnection = New SqlConnection("x")
Dim cmd As New Data.SqlClient.SqlCommand
cmd.CommandText = "INSERT INTO [dbo].[Ordenes_5]([Sub_Total]) VALUES (@SubTotal)"
cmd.Parameters.Add("@SubTotal", SqlDbType.VarChar).Value = lbltotal.Text
connection.Open()
cmd.Connection = connection
cmd.ExecuteNonQuery()
connection.Close()
Dim icmd As SqlCommand = New SqlCommand("insert into ordenes_5_details (Orden#) select Orden# from Ordenes_5", connection)
connection.Open()
icmd.ExecuteNonQuery()
connection.Close()
Dim command As New Data.SqlClient.SqlCommand
command.CommandText = "INSERT INTO dbo.Ordenes_5_details (Articulo, Cantidad, Precio) VALUES (@Articulo, @Cantidad, @Precio)"
command.Parameters.Add("@Articulo", SqlDbType.VarChar)
command.Parameters.Add("@Cantidad", SqlDbType.Int)
command.Parameters.Add("@Precio", SqlDbType.Float)
connection.Open()
command.Connection = connection
For i As Integer = 0 To DataGridView1.Rows.Count - 1
command.Parameters(0).Value = If(DataGridView1.Rows(i).Cells(0).Value, DBNull.Value)
command.Parameters(1).Value = If(DataGridView1.Rows(i).Cells(1).Value, DBNull.Value)
command.Parameters(2).Value = If(DataGridView1.Rows(i).Cells(2).Value, DBNull.Value)
command.ExecuteNonQuery()
Next
MsgBox("se capturo en ambas tablas")
connection.Close()
End Sub
These are my results in SQL server:
Order table. Orden# is the PRIMARY KEY in this table.
Orden# | Sub_Total |
---|---|
1015 | $11.28 |
Order details table. Orden# is a FOREIGN KEY.
Orden# | Articulo | Cantidad | Precio |
---|---|---|---|
1015 | NULL | NULL | NULL |
NULL | BURRITO | 3 | 6.9 |
NULL | COOKIE | 4 | 3.96 |
This is what I'm looking for:
Order table
Orden# | Sub_Total |
---|---|
1015 | $11.28 |
Order details table
Orden# | Articulo | Cantidad | Precio |
---|---|---|---|
1015 | BURRITO | 3 | 6.9 |
1015 | COOKIE | 4 | 3.96 |