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

本文关键字:输入 字符串 格式 不正确 数据库 转换 整数 | 更新日期: 2023-09-27 18:29:50

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

当我查看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;
            }
        }
    }

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

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

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