Private Sub InsertOrderTab1()
Dim insertSQL As String
con.ConnectionString = cnnStr
insertSQL = "INSERT INTO [Order](Order_ID,Cus_ID,Emp_ID) VALUES(@a,@b,@c)"
Try
con.Open()
cmd.Connection = con
cmd.CommandText = insertSQL
cmd.Parameters.AddWithValue("@a", TxtBox1.Text)
cmd.Parameters.AddWithValue("@b", ComboBox1.SelectValue.ToString())
cmd.Parameters.AddWithValue("@c", ComboBox2.SelectValue.ToString())
cmd.ExecuteNonQuery()
cmd.Dispose()
Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Close()
End Sub
อันนี้โค้ต ComboBox Code (VB.NET)
Private Function ComboBoxCnn()
con.ConnectionString = cnnStr
Dim sql As String = "SELECT * FROM Product"
Try
con.Open()
cmd.Connection = con
cmd.CommandText = sql
Dim dt As New DataTable
With ComboBox1
da.SelectCommand = cmd
da.Fill(dt)
.DataSource = dt
.DisplayMember = Pro_ID
.ValueMember = Pro_ID
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Close()
Return tmp
End Function
ลองแบบนี้แล้วแต่ยังไม่ได้ค่ะ
Code (VB.NET)
Private Sub InsertOrderTab1()
Dim insertSQL As String
con.ConnectionString = cnnStr
insertSQL = "INSERT INTO [Order](Order_ID,Emp_ID) VALUES('OD001','Emp007')"
Try
con.Open()
cmd.Connection = con
cmd.CommandText = insertSQL
cmd.ExecuteNonQuery()
cmd.Dispose()
Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Close()
End Sub
Private Sub InsertOrderTab1()
Dim insertSQL As String
tmpOrderDate = Date.Now.ToString("dd/MM/yyyy")
tmpRequstDate = DateTimePicker1.Value.ToString("dd/MM/yyyy")
con.ConnectionString = cnnStr
Dim b as string = "Emp0007"
Dim a as string = "a0002"
insertSQL = "INSERT INTO [Order] (Order_ID,Emp_ID)"
insertSQL &= "VALUES(@a , (SELECT Emp_ID FROM Employee WHERE Emp_ID = @b))"
Try
con.Open()
cmd.Connection = con
cmd.CommandText = insertSQL
cmd.Parameters.AddWithValue("@a", a)
cmd.Parameters.AddWithValue("@b", b)
cmd.ExecuteNonQuery()
cmd.Dispose()
Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Close()
End Sub