The INSERT statement conflicted with the FOREIGN KEY constraint "FK_order_detail_material". The conflict occurred in database "db_spaAnanta", table "dbo.material", column 'id_material'. The statement has been terminated.
หมายความว่าอย่างไรครับ
Tag : .NET, Ms SQL Server 2008, VBScript, Win (Windows App), VS 2008 (.NET 3.x)
Date :
2012-12-17 20:12:32
By :
บี
View :
1407
Reply :
4
No. 1
Guest
Code (VB.NET)
With cn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strconn
.Open()
End With
Dim s As String = ""
If status = "add" Then
s = "insert into [order](id_order,id_sup,dateorder,total)"
s &= " values(@id_or,@id_sup,@dateor,@to)"
Else
s = "update [order] set total = @to where id_order = @id_or"
End If
Dim cm As New SqlCommand
With cm
.CommandType = CommandType.Text
.CommandText = s
.Connection = cn
.Parameters.Clear()
.Parameters.Add("@id_or", SqlDbType.NVarChar, 5).Value = id_order.Text
.Parameters.Add("@id_sup", SqlDbType.NVarChar, 6).Value = ComboBox1.SelectedValue
.Parameters.Add("@dateor", SqlDbType.NVarChar, 10).Value = order_date.Text
.Parameters.Add("@to", SqlDbType.Int).Value = CSng(txttotal.Text)
'ขั้นที่ 3 รัน
.ExecuteNonQuery()
End With
For i = 0 To ListView1.Items.Count - 1
Dim s1 As String = ""
s1 = "insert into [order_detail](id_order,id_material,unitorder,unitrub)"
s1 &= "values (@id_or,@id_mat,@unor,@unru)"
Dim cm1 As New SqlCommand
With cm1
.CommandType = CommandType.Text
.CommandText = s1
.Connection = cn
.Parameters.Clear()
.Parameters.Add("@id_or", SqlDbType.NVarChar, 5).Value = id_order.Text
.Parameters.Add("@id_mat", SqlDbType.NVarChar, 3).Value = ListView1.Items(i).SubItems(2).Text
.Parameters.Add("@unor", SqlDbType.Int).Value = ListView1.Items(i).SubItems(4).Text
.Parameters.Add("@unru", SqlDbType.Int).Value = ListView1.Items(i).SubItems(5).Text
.ExecuteNonQuery()
End With
Next
MessageBox.Show("บันทึกข้อมูลเรียบร้อย")