无法将标签文本转换为int(标签中的数字是从MySQL获取的)

时间:2016-12-28 13:58:05

标签: c#

标签是在代码中从MySQL设置的,我把值转换为按钮

上的int
var x = Convert.ToInt32(lbl_Balance.Text);

        label4.Text = x.ToString(); //temp

我一直收到“mscorlib.dll中发生类型'System.FormatException'的未处理异常附加信息:输入字符串的格式不正确。”

以下是将值放入lbl_Balance(label)

的代码
 MySqlConnection myconnbalance = new MySqlConnection(balanceConn);
            string sqlbalance = "SELECT Balance FROM user_data_new WHERE Username='" + loggeduser + "'"; // string sqlbalance = "SELECT 'Balance' FROM 'user_data_new' WHERE 'Username'='" + loggeduser + "'"; 
            MySqlDataAdapter dabalance = new MySqlDataAdapter(sqlbalance, myconnbalance);
            DataTable dtbalance = new DataTable();
            dabalance.Fill(dtbalance);
            if (dtbalance.Rows.Count == 0)
            {
                MessageBox.Show("Doesn't exists");
            }
            else
            {
                timer1.Enabled = true;
                lbl_Balance.Visible = true;
                lbl_Balance.Text = "Balance: "+dtbalance.Rows[0][0].ToString(); //not working?! 


            }
            myconnbalance.Close();
        }

        catch (Exception ex) { MessageBox.Show(ex.ToString()); }
        {
        }

1 个答案:

答案 0 :(得分:0)

var x = Convert.ToInt32(lbl_Balance.Text.Trim());

如果文字中有空格,请尝试修剪。