-1

我正在插入记录,我的对象之一是组合框。组合框连接到表。当我插入此错误时:

无法将参数值从 DataRowView 转换为 Int32

我的代码:

cn.Open();

SqlCommand Insert = new SqlCommand();
Insert.Connection = cn;
Insert.CommandType = CommandType.Text;
Insert.CommandText = "INSERT INTO Ticket  VALUES (CustomerID, Date, Store, Amount, NoStub) ";

Insert.Parameters.Add("CustomerID", SqlDbType.Int).Value = cboName.SelectedValue;
Insert.Parameters.Add("Date", SqlDbType.DateTime).Value = dtpDate.Value.Date.ToString();
Insert.Parameters.Add("Store", SqlDbType.NVarChar).Value = txtStore.Text;
Insert.Parameters.Add("Amount", SqlDbType.Decimal).Value = txtAmount.Text;
Insert.Parameters.Add("NoStub", SqlDbType.Decimal).Value = txtStub.Text;

Insert.ExecuteNonQuery();

cn.Close();
4

2 回答 2

1

使用此示例代码:

command.Parameters.Add("@CustomerID", SqlDbType.Int).Value = Convert.ToInt32(storeCode);

int.parse用于 cboName。

于 2013-07-08T03:24:30.100 回答
1

将您的代码更改为以下内容并让服务器解析数据类型

cn.Open();
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = cn;
sqlCmd.CommandType = CommandType.Text;
sqlCmd.CommandText = "INSERT INTO Ticket(CustomerID, Date, Store, Amount, NoStub)  
                       VALUES (@CustomerID, @Date, @Store, @Amount, @NoStub) ";
sqlCmd.Parameters.AddWithValue("@CustomerID", cboName.SelectedValue);
sqlCmd.Parameters.AddWithValue("@Date", dtpDate.Value.Date.ToString());
sqlCmd.Parameters.AddWithValue("@Store", txtStore.Text);
sqlCmd.Parameters.AddWithValue("@Amount", txtAmount.Text);
sqlCmd.Parameters.AddWithValue("@NoStub", txtStub.Text);
sqlCmd.ExecuteNonQuery();
cn.Close();
于 2013-07-08T03:30:12.887 回答