![](/img/trans.png)
[英]decimal to hexadecimal in c# with mistake “An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll”
[英]Run-time error in c# - An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll
我是 C# 的新手,在這里運行 C# 代碼時遇到了這個錯誤。 請任何人給我一種有效的方法來解決這個問題..
運行時錯誤 - mscorlib.dll 中發生類型為“System.FormatException”的未處理異常
private void textBox13_TextChanged_1(object sender, EventArgs e)
{
conn.Open();
MySqlCommand comm1 = new MySqlCommand("SELECT `l_points` FROM `customer` WHERE `ph_no` ='" + Convert.ToInt32(textBox13.Text) + "'", conn);
//MySqlDataReader mdr = comm1.ExecuteReader();
//MessageBox.Show(comm1.ExecuteScalar().ToString());
//textBox12.Text = comm1.ExecuteScalar().ToString();
MySqlDataReader red = comm1.ExecuteReader();
if(red.Read())
{
textBox12.Text = red.GetString("l_points");
conn.Close();
conn.Open();
//int x = (Convert.ToInt32(textBox12.Text) - Convert.ToInt32(textBox2.Text)) +Convert.ToInt32 (textBox10.Text);
//string qry = "Update customer SET l-points = x where ph_no= '"+Convert.ToInt32(textBox13.Text)+"'";
//////
MySqlDataAdapter da = new MySqlDataAdapter("Update customer SET l_points = '"+((Convert.ToInt32(textBox12.Text) - Convert.ToInt32(textBox2.Text))
+Convert.ToInt32 (textBox10.Text))+"' where ph_no= '" + Convert.ToInt32(textBox13.Text) + "'", conn);
da.SelectCommand.ExecuteNonQuery();
MessageBox.Show("update sucess!!!!!!");
}
conn.Close();
//double bal = Convert.ToDouble(textBox7.Text) * 0.01;
////double bal = 1000;
////MessageBox.Show(textBox7.Text);
//textBox10.Text = bal.ToString();
}
使用 int.TryParse 在解析發生之前先嘗試將文本解析為整數。 使用Convert.ToInt32,如果字符串有錯誤,就會加載異常。
int number10 = 0, number12 = 0, number2 = 0;
int.TryParse(textBox10.Text, out number10);
int.TryParse(textBox12.Text, out number12);
int.TryParse(textBox2.Text, out number2);
int l_points = number12 - number2 + number10;
您也可以調試每一行以了解問題所在
似乎您試圖將日期時間值與字符串值相等,您可以嘗試 DateTime.Parse,以防您想轉換它
其中一個文本框中的值不可轉換為數字或整數。 它是純文本,您正在使用 Convert.ToInt32 將其轉換為整數。 這就是為什么,您會收到錯誤消息。 另外,在此語句之前計算您的數字。
int l_points = ((Convert.ToInt32(textBox12.Text) -
Convert.ToInt32(textBox2.Text)) +Convert.ToInt32 (textBox10.Text));
int phone = Convert.ToInt32(textBox13.Text);
MySqlDataAdapter da = new MySqlDataAdapter("Update customer SET l_points = "+l_points +" where ph_no= " + phone, conn);
此外,您不應該創建這樣的 SQL 語句以避免 SQL 注入。 將其用作輸入參數,並在可能的情況下用作存儲過程。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.