0

am trying to read the data using SqlDataReader.

But during execution of this code reader shows "Enumeration yielded no results".

 string connetionString = null;
            SqlConnection cnn;
            SqlCommand cmd;
            string sql = null;
            SqlDataReader reader;

            connetionString = "Data Source=INBAGHPC00840;Initial Catalog=Testing;Persist Security Info=True;User ID=sa;Password=12345";
            sql = "select * from [Category]";

            cnn = new SqlConnection(connetionString);
            try
            {
                cnn.Open();
                cmd = new SqlCommand(sql, cnn);
                reader = cmd.ExecuteReader();
                while (reader.Read()) //Here reader shows : Enumeration yielded no results
                {


                  // MessageBox.Show(reader.GetValue(0) + " - " + reader.GetValue(1) + " - " + reader.GetValue(2));
                }
                reader.Close();
                cmd.Dispose();
                cnn.Close();
            }
            catch (Exception ex)
            {
                //MessageBox.Show("Can not open connection ! ");
            }

I couldn't quite get what I am missing here. Please let me know..

6
  • 1
    For a start, we are missing the code which "shows no results". Commented Nov 16, 2016 at 12:37
  • 3
    There are no records, i.e. reader.HasRows will be false. Double check the table has data. Commented Nov 16, 2016 at 12:39
  • reader.HasRows showing true. and table has the 7 rows. Commented Nov 16, 2016 at 12:56
  • Where do you get that error message? The catch block is empty, the while loop has no code. Can you explain better how this code fails? Commented Nov 16, 2016 at 12:58
  • the control went to check while (reader.Read()) --> Here reader shows : Enumeration yielded no results.so control jump to reader.Close(); line.... Commented Nov 16, 2016 at 13:01

0

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.