繁体   English   中英

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

[英]“Error converting datatype nvarchar to int”

在我的应用程序的这一部分中,我试图在需要某些信息的地方创建一个发票生成器。 首先它有一个自动生成的 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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM