sql = "insert into reservation (product_id, product_name, product_warehouse, product_amount, product_qty, product_lotno, product_color, product_date) values (@product_id, @product_name, @product_warehouse, @product_amount, @product_qty, @product_lotno, @product_color, @product_date)"
Dim start As Integer = dgv1.CurrentRow.Index
For i As Integer = start To (start + CInt(tb01.Text)) - 1
If Not dgv1.Rows(i).IsNewRow Then
cmd = New System.Data.SqlClient.SqlCommand(sql, cn)
With cmd
.Parameters.Add(New SqlParameter("@product_id", dgv1.Rows(i).Cells(0).Value))
.Parameters.Add(New SqlParameter("@product_name", dgv1.Rows(i).Cells(1).Value))
.Parameters.Add(New SqlParameter("@product_warehouse", dgv1.Rows(i).Cells(2).Value))
.Parameters.Add(New SqlParameter("@product_amount", dgv1.Rows(i).Cells(3).Value))
.Parameters.Add(New SqlParameter("@product_qty", dgv1.Rows(i).Cells(4).Value))
.Parameters.Add(New SqlParameter("@product_lotno", dgv1.Rows(i).Cells(5).Value))
.Parameters.Add(New SqlParameter("@product_color", dgv1.Rows(i).Cells(6).Value))
.Parameters.Add(New SqlParameter("@product_date", dgv1.Rows(i).Cells(7).Value))
End With
cmd.ExecuteNonQuery()
End If
Next
'MessageBox.Show("คุณได้ทำการจองสินค้าเรียบร้อยแล้ว")
Catch ex As Exception
MessageBox.Show(Err.Description)
End Try
If Me.DataGridView2.Rows.Count <> 0 Then
โค๊ตเดิมตอนที่แล้ว
Dim start As Integer = DataGridView2.CurrentRow.Index
For s As Integer = start To (start + CInt(Me.TextBox1.Text)) - 1
Dim r = DataGridView1.RowCount
DataGridView1.Rows.Add()
DataGridView1.Rows(r).Cells(0).Value = DataGridView2.Rows(s).Cells(0).Value
Next
'เพิ่มเติมลบตารางออก
For f As Integer = 0 To Me.DataGridView1.Rows.Count - 1
For g As Integer = 0 To Me.DataGridView2.Rows.Count - 1
If Me.DataGridView1.Rows(f).Cells(0).Value = Me.DataGridView2.Rows(g).Cells(0).Value Then
'ใส่ตรงนี้ก็ได้ครับ
sql = "DELETE FROM reservation WHERE id='" & Me.DataGridView2.Rows(g).Cells(0).Value & "';"
cmd = New System.Data.SqlClient.SqlCommand(sql, cn)
cn.open()
cmd.ExecuteNonQuery()
cn.close()
'จบส่วนลบในฐานข้อมูล
Me.DataGridView2.Rows.Remove(DataGridView2.Rows(g))
Exit For
End If
Next
Next
End If
sql = "INSERT INTO reservation (xxx)VALUES(xxx);"
cmd = New System.Data.SqlClient.SqlCommand(sql, cn)
cn.open()
cmd.ExecuteNonQuery()
cn.close()
แล้วก็คอยมาลบอีก 1 คำสั่ง
Code (VB.NET)
sql = "DELETE FROM reservation WHERE id='" & Me.DataGridView2.Rows(g).Cells(0).Value & "';"
cmd = New System.Data.SqlClient.SqlCommand(sql, cn)
cn.open()
cmd.ExecuteNonQuery()
cn.close()
เช่น
Code (VB.NET)
sql = "insert into reservation (product_id, product_name, product_warehouse, product_amount, product_qty, product_lotno, product_color, product_date) values (@product_id, @product_name, @product_warehouse, @product_amount, @product_qty, @product_lotno, @product_color, @product_date)"
Dim start As Integer = dgv1.CurrentRow.Index
For i As Integer = start To (start + CInt(tb01.Text)) - 1
If Not dgv1.Rows(i).IsNewRow Then
cmd = New System.Data.SqlClient.SqlCommand(sql, cn)
With cmd
.Parameters.Add(New SqlParameter("@product_id", dgv1.Rows(i).Cells(0).Value))
.Parameters.Add(New SqlParameter("@product_name", dgv1.Rows(i).Cells(1).Value))
.Parameters.Add(New SqlParameter("@product_warehouse", dgv1.Rows(i).Cells(2).Value))
.Parameters.Add(New SqlParameter("@product_amount", dgv1.Rows(i).Cells(3).Value))
.Parameters.Add(New SqlParameter("@product_qty", dgv1.Rows(i).Cells(4).Value))
.Parameters.Add(New SqlParameter("@product_lotno", dgv1.Rows(i).Cells(5).Value))
.Parameters.Add(New SqlParameter("@product_color", dgv1.Rows(i).Cells(6).Value))
.Parameters.Add(New SqlParameter("@product_date", dgv1.Rows(i).Cells(7).Value))
End With
cmd.ExecuteNonQuery()
End If
Next
For f As Integer = 0 To Me.DataGridView1.Rows.Count - 1
For g As Integer = 0 To Me.DataGridView2.Rows.Count - 1
'ตรวจสอบว่าตารางรหัสของ Me.DataGridView1 เหมือนกับ Me.DataGridView2 ให้ลบตารางนั้นออก
If Me.DataGridView1.Rows(f).Cells(0).Value = Me.DataGridView2.Rows(g).Cells(0).Value Then
'ส่วนลบในฐานข้อมูล
sql = "DELETE FROM xxx WHERE id='" & Me.DataGridView2.Rows(g).Cells(0).Value & "';"
cmd = New System.Data.SqlClient.SqlCommand(sql, cn)
cn.open()
cmd.ExecuteNonQuery()
cn.close()
'จบส่วนลบในฐานข้อมูล
'ลบตารางในดาต้กริด DataGridView2
Me.DataGridView2.Rows.Remove(DataGridView2.Rows(g))
Exit For
End If
Next
Next
'MessageBox.Show("คุณได้ทำการจองสินค้าเรียบร้อยแล้ว")
Catch ex As Exception
MessageBox.Show(Err.Description)
End Try
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
open_database()
Try
If dgv02.Rows.Count <> 0 Then
Dim start As Integer = dgv02.CurrentRow.Index
For i As Integer = start To (start + CInt(tb01.Text)) - 1
If Not dgv02.Rows(i).IsNewRow Then
sql = "insert into reservation (product_id, product_name, product_warehouse, product_amount, product_qty, product_lotno, product_color, product_date)" & _
"values (@product_id, @product_name, @product_warehouse, @product_amount, @product_qty, @product_lotno, @product_color, @product_date)"
cmd = New System.Data.SqlClient.SqlCommand(sql, cn)
With cmd
.Parameters.Add(New SqlParameter("@product_id", dgv02.Rows(i).Cells(0).Value))
.Parameters.Add(New SqlParameter("@product_name", dgv02.Rows(i).Cells(1).Value))
.Parameters.Add(New SqlParameter("@product_warehouse", dgv02.Rows(i).Cells(2).Value))
.Parameters.Add(New SqlParameter("@product_amount", dgv02.Rows(i).Cells(3).Value))
.Parameters.Add(New SqlParameter("@product_qty", dgv02.Rows(i).Cells(4).Value))
.Parameters.Add(New SqlParameter("@product_lotno", dgv02.Rows(i).Cells(5).Value))
.Parameters.Add(New SqlParameter("@product_color", dgv02.Rows(i).Cells(6).Value))
.Parameters.Add(New SqlParameter("@product_date", dgv02.Rows(i).Cells(7).Value))
End With
cmd.ExecuteNonQuery()
End If
Next
End If
cn.Close()
Catch ex As Exception
MessageBox.Show(Err.Description)
End Try
If dgv02.Rows.Count > 0 Then
Dim start1 As Integer = dgv02.CurrentRow.Index
For i As Integer = start1 To (start1 + CInt(tb01.Text)) - 1
If Not dgv02.Rows(i).IsNewRow Then
sql &= "delete from in_stock where id='" & dgv02.Rows(start1).Cells(0).Value & "';"
cmd = New System.Data.SqlClient.SqlCommand(sql, cn)
End If
Next
End If
load_dgv3()
load_dgv2()
End Sub
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
open_database()
If dgv02.Rows.Count <> 0 Then
Dim start As Integer = dgv02.CurrentRow.Index
For i As Integer = start To (start + CInt(tb01.Text)) - 1
If Not dgv02.Rows(i).IsNewRow Then
Try
sql = "insert into reservation (product_id, product_name, product_warehouse, product_amount, product_qty, product_lotno, product_color, product_date)" & _
"values (@product_id, @product_name, @product_warehouse, @product_amount, @product_qty, @product_lotno, @product_color, @product_date)"
cmd = New System.Data.SqlClient.SqlCommand(sql, cn)
With cmd
.Parameters.Add(New SqlParameter("@product_id", dgv02.Rows(i).Cells(0).Value))
.Parameters.Add(New SqlParameter("@product_name", dgv02.Rows(i).Cells(1).Value))
.Parameters.Add(New SqlParameter("@product_warehouse", dgv02.Rows(i).Cells(2).Value))
.Parameters.Add(New SqlParameter("@product_amount", dgv02.Rows(i).Cells(3).Value))
.Parameters.Add(New SqlParameter("@product_qty", dgv02.Rows(i).Cells(4).Value))
.Parameters.Add(New SqlParameter("@product_lotno", dgv02.Rows(i).Cells(5).Value))
.Parameters.Add(New SqlParameter("@product_color", dgv02.Rows(i).Cells(6).Value))
.Parameters.Add(New SqlParameter("@product_date", dgv02.Rows(i).Cells(7).Value))
End With
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Try
sql &= "delete from in_stock where id='" & dgv02.Rows(start).Cells(0).Value & "';"
cmd = New System.Data.SqlClient.SqlCommand(sql, cn)
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
Next
End If
cn.Close()
load_dgv3()
load_dgv2()
End Sub
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
open_database()
If dgv02.Rows.Count <> 0 Then
Dim start As Integer = dgv02.CurrentRow.Index
For i As Integer = start To (start + CInt(tb01.Text)) - 1
If Not dgv02.Rows(i).IsNewRow Then
Try
sql = "insert into reservation (product_id, product_name, product_warehouse, product_amount, product_qty, product_lotno, product_color, product_date)" & _
"values (@product_id, @product_name, @product_warehouse, @product_amount, @product_qty, @product_lotno, @product_color, @product_date)"
cmd = New System.Data.SqlClient.SqlCommand(sql, cn)
With cmd
.Parameters.Add(New SqlParameter("@product_id", dgv02.Rows(i).Cells(0).Value))
.Parameters.Add(New SqlParameter("@product_name", dgv02.Rows(i).Cells(1).Value))
.Parameters.Add(New SqlParameter("@product_warehouse", dgv02.Rows(i).Cells(2).Value))
.Parameters.Add(New SqlParameter("@product_amount", dgv02.Rows(i).Cells(3).Value))
.Parameters.Add(New SqlParameter("@product_qty", dgv02.Rows(i).Cells(4).Value))
.Parameters.Add(New SqlParameter("@product_lotno", dgv02.Rows(i).Cells(5).Value))
.Parameters.Add(New SqlParameter("@product_color", dgv02.Rows(i).Cells(6).Value))
.Parameters.Add(New SqlParameter("@product_date", dgv02.Rows(i).Cells(7).Value))
End With
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Try
sql &= "UPDATE in_stock SET reservations='1' where id='" & dgv02.Rows(start).Cells(0).Value & "';"
cmd = New System.Data.SqlClient.SqlCommand(sql, cn)
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
Next
End If
cn.Close()
load_dgv3()
load_dgv2()
End Sub