If CBool(MessageBox.Show("บันทึกใช่หรือไม่ !!!", "ผลการทำงาน", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.Yes) Then
Conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\Database passport\Passport.accdb;")
Conn.Open()
Try
For i As Integer = 0 To dgvPassenger2.Rows.Count - 1
Dim sql As String
sql = "SELECT Passenger_ID FROM Passenger"
sql &= "WHERE F_Name='" & dgvPassenger2.Rows(i).Cells(3).Value.ToString & "', L_Name='" & dgvPassenger2.Rows(i).Cells(2).Value.ToString & "'"
'" & dgvPassenger2.Rows(i).Cells(3).Value.ToString & "')"
sql = "INSERT INTO [VisaSaleCard] ([SaleCard_No],[Customer],[Agent],[Embassy],[DateofIssue],[Note],[ServiceFee],[Totalcost],[Sale],[working_day],[Entries],[Valid_date],[[color=pink]Passenger_ID]) VALUES (@SaleCard_No,@Customer,@Agent,@Embassy,@DateofIssue,@Note,@ServiceFee,@Totalcost,@Sale,@working_day,@Entries,@Valid_date,@Passenger_ID)"
Dim cm As New OleDbCommand(sql, Conn)
cm.Parameters.AddWithValue("@SaleCard_No", tbxNo.Text)
cm.Parameters.AddWithValue("@Customer", cbCustomer.Text)
cm.Parameters.AddWithValue("@Agent", cbAgent.Text)
cm.Parameters.AddWithValue("@Embassy", cbEmbassy.Text)
cm.Parameters.AddWithValue("@DateofIssue", dtDate.Text)
cm.Parameters.AddWithValue("@Note", tbxNote.Text)
cm.Parameters.AddWithValue("@ServiceFee", tbxServiceFee.Text)
cm.Parameters.AddWithValue("@Totalcost", tbxTotalCost.Text)
cm.Parameters.AddWithValue("@Sale", tbxSale.Text)
cm.Parameters.AddWithValue("@working_day", cbWorkDays1.Text)
cm.Parameters.AddWithValue("@Entries", cbEntries1.Text)
cm.Parameters.AddWithValue("@Valid_date", dtDuration1.Text)
cm.Parameters.AddWithValue("@Passenger_ID", dgvPassenger2.Rows(i).Cells(3).Value.ToString)
cm.Transaction = tr
With cm
.CommandText = sql
'.ExecuteNonQuery()
End With
cm.ExecuteNonQuery()
Conn.Close()
Exit For
Next
Catch ex As Exception
success = False
errorMessage = ex.Message
End Try
MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว")
End If
ฟิลด์ที่ต้องการบันทึกให้เป็น ID คือ Passenger_ID ค่ะ
If CBool(MessageBox.Show("บันทึกใช่หรือไม่ !!!", "ผลการทำงาน", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.Yes) Then
Conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\Database passport\Passport.accdb;")
Conn.Open()
Try
Dim sql As String
Dim cmd As OleDbCommand
Dim _Read As OleDbReader
Dim _ID as String
For i As Integer = 0 To dgvPassenger2.Rows.Count - 1
sql = "SELECT Passenger_ID FROM Passenger"
sql &= "WHERE F_Name='" & dgvPassenger2.Rows(i).Cells(3).Value.ToString & "', L_Name='" & dgvPassenger2.Rows(i).Cells(2).Value.ToString & "'"
cm = new OleDbCommand(sql,Conn)
cm.Transaction = tr
_Read = cmd.ExecuteReader
Do While _Read.Read()
_ID = _Read(“Passenger_ID”)
Loop
_Read.Close
sql = "INSERT INTO [VisaSaleCard] ([SaleCard_No],[Customer],[Agent],[Embassy],[DateofIssue],[Note],[ServiceFee],[Totalcost],[Sale],[working_day],[Entries],[Valid_date],[[color=pink]Passenger_ID]) VALUES (@SaleCard_No,@Customer,@Agent,@Embassy,@DateofIssue,@Note,@ServiceFee,@Totalcost,@Sale,@working_day,@Entries,@Valid_date,@Passenger_ID)"
cm.Parameters.AddWithValue("@SaleCard_No", tbxNo.Text)
cm.Parameters.AddWithValue("@Customer", cbCustomer.Text)
cm.Parameters.AddWithValue("@Agent", cbAgent.Text)
cm.Parameters.AddWithValue("@Embassy", cbEmbassy.Text)
cm.Parameters.AddWithValue("@DateofIssue", dtDate.Text)
cm.Parameters.AddWithValue("@Note", tbxNote.Text)
cm.Parameters.AddWithValue("@ServiceFee", tbxServiceFee.Text)
cm.Parameters.AddWithValue("@Totalcost", tbxTotalCost.Text)
cm.Parameters.AddWithValue("@Sale", tbxSale.Text)
cm.Parameters.AddWithValue("@working_day", cbWorkDays1.Text)
cm.Parameters.AddWithValue("@Entries", cbEntries1.Text)
cm.Parameters.AddWithValue("@Valid_date", dtDuration1.Text)
cm.Parameters.AddWithValue("@Passenger_ID", _ID)
cm.Transaction = tr
With cm
.CommandText = sql
'.ExecuteNonQuery()
End With
cm.ExecuteNonQuery()
Conn.Close()
Exit For
Next
Catch ex As Exception
success = False
errorMessage = ex.Message
End Try
MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว")
End If
Dim Conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\Database passport\Passport.accdb;")
Try
Conn.Open()
Catch ex As Exception
' แสดงข้อผิดพลาด
Finally
Conn.Close()
Conn.Dispose()
End Try