0

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

SqlCommand command = new SqlCommand("SELECT DISTINCT TOR_Name FROM TESTCASESTATUS_TABLE WHERE 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/.

2 Answers 2

3

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"]); 
}
....
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.