将数据库中的值转换为整数时,输入字符串的格式不正确

时间:2016-01-10 02:59:34

标签: c# .net

我在将字符串从数据库转换为整数时遇到问题。

当我查看Locals时,该变量会显示该值,但通知仍然表示存在错误。有人可以帮帮我吗?

OleDbConnection kon = new OleDbConnection(koneksi);
OleDbCommand command1 = kon.CreateCommand();

kon.Open();
string selectkoordgaris = "select * from koordinatgaris where namakamera = '" + PilihKameraComboBox.Text + "'";
command1.CommandText = selectkoordgaris;
OleDbDataReader bacakoordgaris = command1.ExecuteReader();   

while (bacakoordgaris.Read())
  {        
    var templateGaris = Directory.GetFiles(@"D:\Dokumen\Alfon\TA Alfon\CobaFitur\Template\Garis\" + bacakoord["namakamera"].ToString());

foreach (var fileGaris in templateGaris)
 {
    counterbanyakgaris++;
    Image<Bgr, byte> garis = new Image<Bgr, byte>(fileGaris);

    for (cntgaris = 0; cntgaris < banyakgaris; cntgaris++)
     {
      int x1garis = int.Parse(bacakoordgaris["x" + ((cntgaris * 4) + 1) + "garis"].ToString()); //here the error. It says Input string was not in a correct format
      int x2garis = int.Parse(bacakoordgaris["x" + ((cntgaris * 4) + 2) + "garis"].ToString());
      int y1garis = int.Parse(bacakoordgaris["y" + ((cntgaris * 4) + 1) + "garis"].ToString());
      int y2garis = int.Parse(bacakoordgaris["y" + ((cntgaris * 4) + 2) + "garis"].ToString());
      int y3garis = int.Parse(bacakoordgaris["y" + ((cntgaris * 4) + 3) + "garis"].ToString());

      int gariswidth = x2garis - x1garis;
      int garisheight = y3garis - y2garis;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

这是因为bacakoordgaris["x" + ((cntgaris * 4) + 1) + "garis"]的输出不是数字格式(它可能包含一些字符串)。

您可以向该行添加breakpoint并添加监视以检查bacakoordgaris["x" + ((cntgaris * 4) + 1) + "garis"]中返回的值并在将其解析为整数之前进行更正(例如,通过String.Replace()方法)。