0

我想用 ComboBox 将记录插入数据库。ComboBox 连接到另一个表,这是错误:

将数据类型 nvarchar 转换为数值时出错。

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";
}
4

4 回答 4

3

当您调用时,请AddWithValue确保您传递的数据类型与列类型匹配。这是一个可能的候选人:

cmd.Parameters.AddWithValue("@Amount", txtAmount.Text);

在这一行中,您将文本字符串传递给显然需要数值(数量)的东西。您应该先将其解析txtAmount.Text为小数,然后传递该值:

decimal amount = decimal.Parse(txtAmount.Text);
cmd.Parameters.AddWithValue("@Amount", amount);

使用此代码,如果无法将其中的字符串解析为小数,您仍然可能会遇到异常txtAmount.Text,但至少您会知道是哪个值导致了问题。您可以/应该对其他值执行相同的操作,以确保它们与其列类型匹配。

于 2013-07-09T01:31:49.110 回答
0

试试 string.isNullorEmpty(txtAmount.text);

于 2014-12-12T11:49:12.287 回答
-1
 private void button1_Click(object sender, EventArgs e)
        {
            string sql;
            sql = "insert into slab (date,sober_visor,tesh,shift,group,heat_no,st_grade,thick,width,length,location,pcs,remarkes,slab_no) values (@date,@sober_vsor,@tesh,@shift,@group,@heat_no,@st_grade,@thick,@width,@length,@loction,@pcs,@slab_no);select scope_identity()";

            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.Parameters.AddWithValue("@date", txt_date.Text);
            cmd.Parameters.AddWithValue("@sober_visor", com_sober_visor.ToString());
            cmd.Parameters.AddWithValue("@shift", txt_shift.Text);
            cmd.Parameters.AddWithValue("@heat_no", txt_heat_no.Text);
            cmd.Parameters.AddWithValue("@thick", txt_shift.Text);
            cmd.Parameters.AddWithValue("@width", txt_heat_no.Text);
            cmd.Parameters.AddWithValue("@length", txt_length.Text);
            cmd.Parameters.AddWithValue("@pcs", txt_pcs.Text);
            cmd.Parameters.AddWithValue("@st_grade", txt_st_gread.Text);
            cmd.Parameters.AddWithValue("@location", txt_loction.Text);
            cmd.Parameters.AddWithValue("@slab_no", txt_slab_no.Text);
            con.Open();
            cmd.ExecuteNonQuery();
            txt_heat_no.Text = cmd.ExecuteScalar().ToString();

            con.Close();
            MessageBox.Show("تمت عملية الإضافة");
        }
    }
}
于 2016-11-22T14:43:37.780 回答
-1
cmd.Parameters.AddWithValue("@ödenecektutar", Convert.ToDecimal(tutar1.Text.Substring(0, tutar.Text.Length - 1)));
于 2020-04-25T11:04:42.940 回答