Violation of PRIMARY KEY constraint 'PK_Purchases'. Cannot insert duplicate key in object 'dbo.Purchases'. The statement has been terminated.
ส่วนนี่เป็นโค๊ดค่ะ
Code (VB.NET)
Private Sub BTSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTSave.Click
Dim sb As StringBuilder
If TxtPurID.Text.Trim() = "" Then
MessageBox.Show("กรุณากดปุ่มเพิ่มใบสั่งซื้อก่อน!!", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
If MessageBox.Show("คุณต้องการเพิ่มใบสั่งซื้อใช่หรือไม่", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
Exit Sub
End If
Dim sqlSave As String = ""
sb = New StringBuilder()
sb.Remove(0, sb.Length)
sb.Append("INSERT INTO Purchases(PurID,SupplieID,Date,NetTotal)")
sb.Append(" VALUES(@PurID,@SupID,@Date,@Tot)")
sqlSave = sb.ToString()
With cm
.Parameters.Clear()
.CommandText = sqlSave
.Connection = cn
cn.Open()
.Parameters.AddWithValue("@PurID", TxtPurID.Text)
.Parameters.AddWithValue("@SupID", CStr(ComboBox1.SelectedValue))
.Parameters.Add("@Date", SqlDbType.DateTime).Value = TxtPurDate.Text
.Parameters.Add("@Tot", SqlDbType.Float).Value = CSng(TxtTotal.Text)
.ExecuteNonQuery()
End With
Dim i As Integer = 0
For i = 0 To ListView1.Items.Count - 1
sb.Remove(0, sb.Length)
sb.Append("INSERT INTO PurchaseDetails (PurID,No,Amount,Price,Total)")
sb.Append(" VALUES (@PurID,@NO,@Amount,@Price,@Tot)")
sqlSave = sb.ToString()
With cm
.Parameters.Clear()
.CommandText = sqlSave
.Parameters.AddWithValue("@PurID", TxtPurID.Text)
.Parameters.AddWithValue("@No", ListView1.Items(i).SubItems(1).Text)
.Parameters.AddWithValue("@Amount", CInt(ListView1.Items(i).SubItems(4).Text))
.Parameters.AddWithValue("@Price", CSng(ListView1.Items(i).SubItems(3).Text))
.Parameters.Add("@Tot", SqlDbType.Float).Value = CSng(ListView1.Items(i).SubItems(5).Text)
.ExecuteNonQuery()
End With
sb.Remove(0, sb.Length())
sb.Append("UPDATE Products")
sb.Append(" SET UnitInStock = @un")
sb.Append(" WHERE (No=@ProID)")
Dim sqlUP As String
sqlUP = sb.ToString()
With cm
.Parameters.Clear()
.CommandText = sqlUP
.Parameters.AddWithValue("@un", CInt(ListView1.Items(i).SubItems(6).Text))
.Parameters.AddWithValue("@ProID", ListView1.Items(i).SubItems(1).Text)
.ExecuteNonQuery()
End With
Next
MessageBox.Show("บันทึกข้อมูลเรียบร้อยแล้ว")
cn.Close()
End Sub
End Class
Dim sqlSave As String = ""
sb = New StringBuilder()
sb.Remove(0, sb.Length)
sb.Append("INSERT INTO Purchases(PurID,SupplieID,Date,NetTotal)")
sb.Append(" VALUES(@PurID,@SupID,@Date,@Tot)")
sqlSave = sb.ToString()
With cm
.Parameters.Clear()
.CommandText = sqlSave
.Connection = cn
cn.Open()
.Parameters.AddWithValue("@PurID", TxtPurID.Text)
.Parameters.AddWithValue("@SupID", CStr(ComboBox1.SelectedValue))
.Parameters.Add("@Date", SqlDbType.DateTime).Value = TxtPurDate.Text
.Parameters.Add("@Tot", SqlDbType.Float).Value = CSng(TxtTotal.Text)
.ExecuteNonQuery()
End With
Dim i As Integer = 0
For i = 0 To ListView1.Items.Count - 1
sb.Remove(0, sb.Length)
sb.Append("INSERT INTO PurchaseDetails (PurID,No,Amount,Price,Total)")
sb.Append(" VALUES (@PurID,@NO,@Amount,@Price,@Tot)")
sqlSave = sb.ToString()
With cm
.Parameters.Clear()
.CommandText = sqlSave
.Parameters.AddWithValue("@PurID", TxtPurID.Text)
.Parameters.AddWithValue("@No", ListView1.Items(i).SubItems(1).Text)
.Parameters.AddWithValue("@Amount", CInt(ListView1.Items(i).SubItems(4).Text))
.Parameters.AddWithValue("@Price", CSng(ListView1.Items(i).SubItems(3).Text))
.Parameters.Add("@Tot", SqlDbType.Float).Value = CSng(ListView1.Items(i).SubItems(5).Text)
.ExecuteNonQuery()
End With
sb.Remove(0, sb.Length())
sb.Append("UPDATE Products")
sb.Append(" SET UnitInStock = @un")
sb.Append(" WHERE (No=@ProID)")
Dim sqlUP As String
sqlUP = sb.ToString()
With cm
.Parameters.Clear()
.CommandText = sqlUP
.Parameters.AddWithValue("@un", CInt(ListView1.Items(i).SubItems(6).Text))
.Parameters.AddWithValue("@ProID", ListView1.Items(i).SubItems(1).Text)
.ExecuteNonQuery()
End With