2017-05-05 92 views
-3

此代码不起作用。我试图将数据插入到SQL Server:插入到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',表'complaint_management.dbo.tbl_complaint';列不允许有空值。
INSERT失败。该语句已终止

System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔breakConnection,措施1 wrapCloseInAction)2442126
System.Data.SqlClient.SqlInternalConnection.OnError(SqlExcep重刑例外,布尔breakConnection, Action1 wrapCloseInAction)+5736904

+3

请发布错误信息 – Etienne

+0

它不显示任何错误消息 –

+0

@rahulmundaware,删除try catch。你会看到错误。 –

回答

0

看起来您的表中有一个'Complaint_id'列,它具有Not Null属性。你可以采取以下的任何提到的步骤:

  1. 通过你的插入查询
  2. 更改列“Complaint_id”的属性发送的“Complaint_id”列中的值,以接受NULL值,如果它存在作为主键,然后改变它。
  3. 更改“Complaint_id”列的属性以使其成为标识列以自动生成值。