没有数据时无效尝试读取

Rom*_*man 0 c#

这是我的代码:

 db.Open();

 string updateString = "SELECT TOP 1 RTRIM(kol) kol, RTRIM(adres) adres, RTRIM(numwave) numwave FROM sorters WHERE kodprod=@kodprod AND sorter_kod=@sorter AND moved_ok IS NULL ORDER BY CAST(kol as int)";
 try
 {
     SqlCommand command = new SqlCommand(updateString, db);
     //command.Parameters.AddWithValue("@numdoc", NumDoc);
     command.Parameters.AddWithValue("@kodprod", KodProd.Id);
     command.Parameters.AddWithValue("@sorter", SorterKod);
     SqlDataReader reader = command.ExecuteReader();
     reader.Read();//here error
     Kol = reader["kol"].ToString();
     Adres = reader["adres"].ToString();
     NumWave = reader["numwave"].ToString();
     NumDoc = reader["numdoc"].ToString();
     reader.Close();
  }
  catch (Exception ex)
  { }
Run Code Online (Sandbox Code Playgroud)

运行代码时为什么会出现此错误?:

没有数据时无效尝试读取

Sai*_*ala 5

您可以检查DataReader是否已准备好获取行

if(reader.HasRows)
{
   //do the coding here
}
Run Code Online (Sandbox Code Playgroud)