my code returns error saying
violation of PRIMARY KEY constraint 'PK_tblOfficeEquipmentProfile'. Cannot insert duplicate key in object 'tblOfficeEquipmentProfile'.
This is my code:
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
Dim sqlconn As New SqlClient.SqlConnection
sqlconn.ConnectionString = "server = SKPI-APPS1;" & _
"Database = EOEMS;integrated security=true"
Dim myCommand As SqlCommand
'parametrized update sql command
Try
sqlconn.Open()
myCommand = New SqlCommand("UPDATE tblOfficeEquipmentProfile SET OE_Category = @OE_Category, OE_SubCategory = @OE_SubCategory, OE_ID = @OE_ID, OE_Name = @OE_Name, OE_User = @OE_User, OE_Brand = @OE_Brand, OE_Model =@OE_Model, OE_Specs =@OE_Specs, OE_SerialNo =@OE_SerialNo, OE_PropertyNo = @OE_PropertyNo, OE_MacAddress = @OE_MacAddress, OE_Static_IP = @OE_Static_IP, OE_Vendor = @OE_Vendor, OE_PurchaseDate =@OE_PurchaseDate, OE_WarrantyInclusiveYear=@OE_WarrantyInclusiveYear, OE_WarrantyStatus=@OE_WarrantyStatus,OE_Status=@OE_Status,OE_Dept_Code=@OE_Dept_Code,OE_Location_Code=@OE_Location_Code,OE_Remarks=@OE_Remarks", sqlconn)
myCommand.Parameters.Add("@OE_Category", cmbCategory.Text)
myCommand.Parameters.Add("@OE_SubCategory", cmbSubCategory.Text)
myCommand.Parameters.Add("@OE_ID", txtOEID.Text)
myCommand.Parameters.Add("@OE_Name", txtName.Text)
myCommand.Parameters.Add("@OE_User", txtUser.Text)
myCommand.Parameters.Add("@OE_Brand", cmbBrand.Text)
myCommand.Parameters.Add("@OE_Model", cmbModel.Text)
myCommand.Parameters.Add("@OE_Specs", txtSpecs.Text)
myCommand.Parameters.Add("@OE_SerialNo", txtSerialNo.Text)
myCommand.Parameters.Add("@OE_PropertyNo", txtPropertyNo.Text)
myCommand.Parameters.Add("@OE_MacAddress", txtMacAddress.Text)
myCommand.Parameters.Add("@OE_Static_IP", txtStaticIp.Text)
myCommand.Parameters.Add("@OE_Vendor", cmbVendor.Text)
myCommand.Parameters.Add("@OE_PurchaseDate", txtPurchaseDate.Text)
myCommand.Parameters.Add("@OE_WarrantyInclusiveYear", cmbWarrantyInclusiveYear.Text)
myCommand.Parameters.Add("@OE_WarrantyStatus", txtWarrantyStatus.Text)
myCommand.Parameters.Add("@OE_Status", txtStatus.Text)
myCommand.Parameters.Add("@OE_Dept_Code", cmbDeptCode.Text)
myCommand.Parameters.Add("@OE_Location_Code", cmbLocationCode.Text)
myCommand.Parameters.Add("@OE_Remarks", cmbRemarks.Text)
myCommand.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
MsgBox("Successfully Updated Records")
End Try
End Sub
Check primary key of table tblOfficeEquipmentProfile
Based on primary filed you can update rest of the fields, means remove updation of primary key fileds
您好,此查询更新表中的所有数据,因此,如果您有一些唯一的ID,则无法使用相同的ID更新,我认为您需要向该查询添加Where语句。
Check where is utilized based on your primary field OE_ID
myCommand = New SqlCommand("UPDATE tblOfficeEquipmentProfile SET OE_Category = @OE_Category, OE_SubCategory = @OE_SubCategory, OE_Name = @OE_Name, OE_User = @OE_User, OE_Brand = @OE_Brand, OE_Model =@OE_Model, OE_Specs =@OE_Specs, OE_SerialNo =@OE_SerialNo, OE_PropertyNo = @OE_PropertyNo, OE_MacAddress = @OE_MacAddress, OE_Static_IP = @OE_Static_IP, OE_Vendor = @OE_Vendor, OE_PurchaseDate =@OE_PurchaseDate, OE_WarrantyInclusiveYear=@OE_WarrantyInclusiveYear, OE_WarrantyStatus=@OE_WarrantyStatus,OE_Status=@OE_Status,OE_Dept_Code=@OE_Dept_Code,OE_Location_Code=@OE_Location_Code,OE_Remarks=@OE_Remarks
where OE_ID = @OE_ID", sqlconn)
myCommand.Parameters.Add("@OE_Category", cmbCategory.Text)
myCommand.Parameters.Add("@OE_SubCategory", cmbSubCategory.Text)
myCommand.Parameters.Add("@OE_ID", txtOEID.Text)
myCommand.Parameters.Add("@OE_Name", txtName.Text)
myCommand.Parameters.Add("@OE_User", txtUser.Text)
myCommand.Parameters.Add("@OE_Brand", cmbBrand.Text)
myCommand.Parameters.Add("@OE_Model", cmbModel.Text)
myCommand.Parameters.Add("@OE_Specs", txtSpecs.Text)
myCommand.Parameters.Add("@OE_SerialNo", txtSerialNo.Text)
myCommand.Parameters.Add("@OE_PropertyNo", txtPropertyNo.Text)
myCommand.Parameters.Add("@OE_MacAddress", txtMacAddress.Text)
myCommand.Parameters.Add("@OE_Static_IP", txtStaticIp.Text)
myCommand.Parameters.Add("@OE_Vendor", cmbVendor.Text)
myCommand.Parameters.Add("@OE_PurchaseDate", txtPurchaseDate.Text)
myCommand.Parameters.Add("@OE_WarrantyInclusiveYear", cmbWarrantyInclusiveYear.Text)
myCommand.Parameters.Add("@OE_WarrantyStatus", txtWarrantyStatus.Text)
myCommand.Parameters.Add("@OE_Status", txtStatus.Text)
myCommand.Parameters.Add("@OE_Dept_Code", cmbDeptCode.Text)
myCommand.Parameters.Add("@OE_Location_Code", cmbLocationCode.Text)
myCommand.Parameters.Add("@OE_Remarks", cmbRemarks.Text)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.