“将数据类型nvarchar转换为int时出错”

时间:2020-04-27 16:53:16

标签: c# sql-server

在我的应用程序的这一部分中,我试图做一个发票生成器,它需要某些信息。首先,它具有一个自动生成的ID,然后我们还有一些其他信息,例如:日期,学生姓名和您要支付的月份,必须从下拉列表中选择前面提到的所有字段其类型。然后,我们还有一些其他数据,例如出纳员必须写的金额,ncf和rnc东西。

我一直在尝试以多种方式解决此问题,但是我真的找不到解决之道。我一直在尝试解析和其他一些疯狂的想法,但这些想法都没有起作用。

这是代码:

private void SaveStudentFee()
{
    try
    {
        command = new SqlCommand("uspStudentFee", connection);
        command.CommandType = CommandType.StoredProcedure;
        connection.Open();
        command.Parameters.AddWithValue("@Action", (UpdateMode) ? "Update" : "Insert");
        command.Parameters.AddWithValue("@FeeID", txtFeeID.Text);
        command.Parameters.AddWithValue("@FKStudentID", CB_StdFee.Text);
        command.Parameters.AddWithValue("@FeeDate", DT_FeePayDate.Value);
        command.Parameters.AddWithValue("@FeeRNC",txtRNC.Text);
        command.Parameters.AddWithValue("@FeeNCF",txtNCF.Text);
        command.Parameters.AddWithValue("@Fees", Txt_Fee.Text);
        command.Parameters.AddWithValue("@FeeMonth", CB_PayMonth.Text);
        command.ExecuteNonQuery();
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
    finally
    {
        connection.Close();
    }
}

这是存储过程:

create PROC uspStudentFee
    @Action NVARCHAR(50),
    @FeeID INT=NULL,
    @FKStudentID INT=NULL,
    @FeeDate DATE=NULL,
    @FeeRNC NVARCHAR(50)=NULL,
    @FeeNCF NVARCHAR(50)=NULL,
    @Fees NVARCHAR(50)=NULL,
    @FeeMonth INT=NULL

1 个答案:

答案 0 :(得分:0)

鉴于您正在从似乎是某种输入字段的地方获取存储过程输入,请仔细检查数字的格式。例如,检查txtFeeIDCB_StdFeeCB_PayMonth,以确保它们不表示为小数。例如,100.00是有效数字,但是无法解析十进制数,因此肯定会导致转换错误。我会将Convert.ToInt32()添加到您所有的Integer字段中。