附加信息:无法转换类型&System; System.Int32'输入' System.String'

时间:2015-03-23 13:04:45

标签: c#

我撤回金额时如何删除错误

 con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\anaabenoja\Documents\sample connection.accdb";
 sql = "SELECT * FROM Acc_info where  account_no = " +txtaccno.Text;     
 cmd.Connection = con;
 cmd.CommandText = sql;
        da.SelectCommand = cmd;
        da.Fill(Log_in);
        if (Log_in.Rows.Count > 0)
        {
error -->  *balance = (string  )(Log_in.Rows[0]["balance"]);*
            num1 = int.Parse("balance");
            num2 = int.Parse(txtamount.Text);

2 个答案:

答案 0 :(得分:1)

替换 balance = (string )(Log_in.Rows[0]["balance"]);

balance = Log_in.Rows[0]["balance"].ToString();

这将确保无论存在什么值,都会以字符串格式返回。如果您还必须处理空值,请尝试 而是Convert.ToString()

答案 1 :(得分:0)

 con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\anaabenoja\Documents\sample connection.accdb";
 sql = "SELECT * FROM Acc_info where  account_no = " +txtaccno.Text;     
 cmd.Connection = con;
 cmd.CommandText = sql;
        da.SelectCommand = cmd;
        da.Fill(Log_in);
        if (Log_in.Rows.Count > 0)
        {
error -->  *balance = (string  )(Log_in.Rows[0]["balance"]);*
            num1 = int.Parse("balance");
            num2 = int.Parse(txtamount.Text);

如果你以后要解析它,你甚至需要转换成字符串??!

只需使用:

num1 = Log_in.Rows[0]["balance"]

不确定DataAdapter做了什么,因为我没有使用它,但最坏的情况是将值转换为int,如果它不是int(即它是盒装的 - 如果它是不兼容的话显然不会转换为int型):!

num1 = (int)Log_in.Rows[0]["balance"]
相关问题