我想用ComboBox将记录插入数据库。组合框连接到其他的桌子,这是错误:”将数据类型nvarchar转换为数字时出错。
Error converting data type nvarchar to numeric.
private void InsertReceipt()
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO Ticket(CustomerID, Date, Store, Amount, NoStub)" +
"VALUES (@CustomerID, @Date, @Store, @Amount, @NoStub) ";
cmd.Parameters.AddWithValue("@CustomerID", cboName.SelectedValue);
cmd.Parameters.AddWithValue("@Date", dtpDate.Value.Date.ToString());
cmd.Parameters.AddWithValue("@Store", txtStore.Text);
cmd.Parameters.AddWithValue("@Amount", txtAmount.Text);
cmd.Parameters.AddWithValue("@NoStub", txtStub.Text);
cmd.ExecuteNonQuery();
}
void GetRecords2()
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.CommandText = "SELECT CustomerID, firstname + ', ' + lastname AS Name FROM Customer";
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds, "Customer");
cboName.DataSource = ds;
cboName.DisplayMember = "Customer.Name";
cboName.ValueMember = "Customer.CustomerID";
}
使用Int.ParseInt(txtAmount.Text)将其另存为数字或Float.ParseFloat。你没有给出一个模式,所以我猜测。 –