2013-10-03 51 views
0

我一直在嘗試在文本框和標籤中添加一個值。標籤內的值是在頁面加載時自動生成的。它可以是負值,正值或零(小數點)。當我試圖用文本框的值添加它時,我得到以下錯誤。以下是錯誤和代碼。在文本框中添加一個值並在C#中添加標籤

輸入字符串格式不正確。 描述:執行當前Web請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。

異常詳細信息:System.FormatException:輸入字符串的不正確的格式

SqlCommand cmd6 = new SqlCommand("update dues set amount = @due where person='rahul'", con); 
      cmd6.Parameters.Add("@due", SqlDbType.Int); 
      cmd6.Parameters["@due"].Value = int.Parse(txt_rahul.Text + lbl_rahul.Text); 
      cmd6.ExecuteNonQuery(); 
+0

這意味着txt_rahul.Text或lbl_rahul.Text的內容不是數字。 int.Parse不能將其轉換爲數字。 – ema

+0

DB中參數的數據類型與要插入數據庫的值不匹配,或者自動生成的值與您的字段數據類型不匹配。試試這個 int.Parse(txt_rahul.Text)+ int.Parse(lbl_rahul.Text) –

回答

0

試試這個

SqlCommand cmd6 = new SqlCommand("update dues set amount = @due where person='rahul'", con); 
      cmd6.Parameters.Add("@due", SqlDbType.Int); 
      cmd6.Parameters["@due"].Value = Convert.ToDouble(txt_rahul.Text) + Convert.ToDouble(lbl_rahul.Text); 
      cmd6.ExecuteNonQuery(); 
+0

輸入字符串格式不正確。 描述:執行當前Web請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。 異常詳細信息:System.FormatException:輸入字符串的格式不正確。 –

+0

那麼你在兩個盒子裏放什麼樣的價值,你是整數嗎?他們是小數?他們是字符串嗎? – Arran

+0

對於十進制,它給出了一個錯誤。 –

0

txt_rahul.Text + lbl_rahul.Text將串連的string的在一起,它不會增加他們向上。

您需要首先將每個解析成一個整數。如在:

int something = int.Parse(txt_rahul.Text); 
int somethingElse = int.Parse(lbl_rahul.Text); 
cmd6.Parameters["@due"].Value = something + somethingElse; 
+0

它對整數工作正常,但當標籤中的值是小數時會出錯,如-162.8 輸入字符串格式不正確。 描述:執行當前Web請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。 異常詳細信息:System.FormatException:輸入字符串的格式不正確。 –

+0

那麼你的數據庫列是一個int。你沒有提供一個int。你期望會發生什麼?如果您也提供小數點,請更改數據庫列。 – Arran