开发者

Specific query on sqlcommand

hii,can anyone tell what is wrong with this code.??

SqlCommand command = new SqlCommand("SELECT DISTINCT TOR_Name FROM TESTCASESTATUS_TABLE WHE开发者_JS百科RE TestCaseID = '" 
        + DropDownList1.SelectedItem.Text + "'", connection);
SqlDataReader x = command.ExecuteReader();
if (null != x && x.HasRows)
   TestCaseName.Text = Convert.ToString(x["TOR_Name"]);
else 
   TestCaseName.Text = "something";
x.Close();

when i debug the code it is even getting into the if conditioon but then it is throwing an error, invalid attempt to read data when no data is present. !!! please help/.


You need to issue a DataReader.Read command for the data to be actually loaded into fields, like

SqlDataReader x = command.ExecuteReader(); 
if (null != x && x.HasRows) 
{
  x.Read();
  TestCaseName.Text = Convert.ToString(x["TOR_Name"]); 
}
....


Call x.Read() to fetch the first result.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜