此代码无效。我试图将数据插入SQL Server:
int a = 1;
DateTime cDate;
cDate = DateTime.Today;
string insertString = "insert into tbl_complaint(Date, User_id, department_name, Product_name, complaint_details, Status) values (@date, @uid, @dept, @product, @details, @status)";
SqlCommand cmd = new SqlCommand(insertString, con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@date", cDate);
cmd.Parameters.AddWithValue("@uid", a);
cmd.Parameters.AddWithValue("@dept", ddlDept.SelectedValue);
cmd.Parameters.AddWithValue("@product", txtPName.Text);
cmd.Parameters.AddWithValue("@details", txtCDes.Text);
cmd.Parameters.AddWithValue("@status", "Submited");
try {
int check;
con.Open();
check= cmd.ExecuteNonQuery();
con.Close();
}
我得到的错误:
SqlException(0x80131904):
无法将值NULL插入列' Complaint_id',table' complaint_management.dbo.tbl_complaint';列不允许空值 INSERT失败。该声明已被终止System.Data.SqlClient.SqlConnection.OnError(SqlException exception,Boolean breakConnection,Action1 wrapCloseInAction)+2442126
System.Data.SqlClient.SqlInternalConnection.OnError(SqlExcepration exception,Boolean breakConnection,Action1 wrapCloseInAction)+5736904
答案 0 :(得分:0)
看起来你有一个专栏' Complaint_id'在您的表中使用Not Null属性。您可以采取以下任何步骤: