简体   繁体   English

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

[英]“Error converting datatype nvarchar to int”

In this part of my app I am trying to do a invoice generator where it requires certain information.在我的应用程序的这一部分中,我试图在需要某些信息的地方创建一个发票生成器。 First of all it has an auto-generated Id, then we have some other infos just as: the date, the name of a student and the month that you want to pay, all the fields mentioned before have to be selected from a dropdown list of its types.首先它有一个自动生成的 ID,然后我们有一些其他信息,例如:日期、学生姓名和您要支付的月份,前面提到的所有字段都必须从下拉列表中选择其类型。 And then we have some other data such as the amount, the ncf and rnc things that must be written by the cashier.然后我们还有一些其他的数据,例如金额,ncf 和 rnc 必须由收银员写的东西。

I've been trying to solve this issue in many ways but I can't really find a way to do it.我一直在尝试以多种方式解决这个问题,但我真的找不到解决方法。 I've been trying parsing and some other crazy ideas that came to my mind but none of those worked.我一直在尝试解析和一些其他疯狂的想法,但这些想法都没有奏效。

Here's the code:这是代码:

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();
    }
}

Here's the stored procedure:这是存储过程:

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

Given you are grabbing your stored procedure inputs from what appears to be some sort of input fields, double check the formatting of your numbers.鉴于您正在从某种输入字段中获取存储过程输入,请仔细检查数字的格式。 For instance, check txtFeeID , CB_StdFee , CB_PayMonth to make sure that they are not represented as a decimal.例如,检查txtFeeIDCB_StdFeeCB_PayMonth以确保它们不表示为小数。 As an example 100.00 is a valid number, but the decimal cannot be parsed, so it would most certainly cause the conversion error.例如100.00是一个有效数字,但小数点无法解析,所以肯定会导致转换错误。 I'd add Convert.ToInt32() to all of your Integer fields.我会将Convert.ToInt32()添加到您的所有 Integer 字段中。

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

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