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..

Sri
  • 1
  • 5
  • 1
    For a start, we are missing the code which "shows no results". –  Nov 16 '16 at 12:37
  • 3
    There are no records, i.e. reader.HasRows will be false. Double check the table has data. – Alex K. Nov 16 '16 at 12:39
  • reader.HasRows showing true. and table has the 7 rows. – Sri Nov 16 '16 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? – Steve Nov 16 '16 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.... – Sri Nov 16 '16 at 13:01
  • Maybe this helps you: http://stackoverflow.com/questions/30041745/sqldatareader-reader-read-shows-enumeration-yielded-no-results – Sebi Nov 16 '16 at 13:08

0 Answers0