Aslam o alikum
I m using vb.net 2010 with sql server 2014, at present I m inserting the data in three tables with following code
I want to insert the data in these tables by using SqlTransaction but have no idea, can some body let me know the vb.net code for the same.
Thanks in advance.
I m using vb.net 2010 with sql server 2014, at present I m inserting the data in three tables with following code
VB.NET:
Private Sub BtnSave_Click(sender As System.Object, e As System.EventArgs) Handles BtnSave.Click
If PurchaseGrid.Rows.Item(0).Cells(0).Value = "" Or PurchaseGrid.Rows.Item(0).Cells(2).Value = "" Then
MessageBox.Show("No Bill Details found", "Grid Data", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
PurchaseGrid.AllowUserToAddRows = False
Try
GetConnect()
Using cmd As New SqlCommand("INSERT INTO PurchaseMaster (PurBillNo,PurBillDate,VendorID,PurAmount,Remarks) Values (@PurBillNo,@PurBillDate,@VendorID,@Puramount,@Remarks)", Conn)
Conn.Open()
'parameters
cmd.Parameters.AddWithValue("@PurBillNo", MyTxtBillNo.Text)
cmd.Parameters.AddWithValue("@PurBillDate", CDate(TxtBillDate.Text))
cmd.Parameters.AddWithValue("@VendorID", TxtVendorID.Text)
cmd.Parameters.AddWithValue("@PurAmount", TxtNetTotal.Text)
cmd.Parameters.AddWithValue("@Remarks", TxtRemarks.Text)
cmd.ExecuteNonQuery()
End Using
For Each rw As DataGridViewRow In PurchaseGrid.Rows
If rw.Cells(0).Value <> "" And rw.Cells(2).Value <> "" Then
Using cmd = New SqlCommand("INSERT INTO PurchaseDetail (PurBillNo,ProductID,PurDescription,PurQty,PurRate) values ('" & MyTxtBillNo.Text & "','" & rw.Cells(0).Value & "','" & rw.Cells(1).Value & "','" & rw.Cells(2).Value & "','" & rw.Cells(3).Value & "')", Conn)
cmd.ExecuteNonQuery()
End Using
End If
Next
For Each lgrw As DataGridViewRow In PurchaseGrid.Rows
Using cmd = New SqlCommand("INSERT INTO LogDetail (LogDate,Action,ProductID,LocationID,VendorID,VoucherNo,Trans,RATE) VALUES (@LogDate,@Action,@ProductID,@LocationID,@VendorID,@VoucherNo,@Trans,@RATE)", Conn)
cmd.Parameters.AddWithValue("@LogDate", TxtBillDate.Text)
cmd.Parameters.AddWithValue("@Action", "PS")
cmd.Parameters.AddWithValue("@ProductID", lgrw.Cells(0).Value)
cmd.Parameters.AddWithValue("@LocationID", "001")
cmd.Parameters.AddWithValue("@vendorID", TxtVendorID.Text)
cmd.Parameters.AddWithValue("@VoucherNO", MyTxtBillNo.Text)
cmd.Parameters.AddWithValue("@Trans", lgrw.Cells(2).Value)
cmd.Parameters.AddWithValue("@Rate", lgrw.Cells(3).Value)
cmd.ExecuteNonQuery()
End Using
Next
MessageBox.Show("New Record has Added Successfuly", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show("Error :" & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
I want to insert the data in these tables by using SqlTransaction but have no idea, can some body let me know the vb.net code for the same.
Thanks in advance.