簡體   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