reading data from sql table

peterpiper

Member
Joined
Nov 6, 2013
Messages
5
Programming Experience
Beginner
I am attempting to retrieve data from an sql database. I have the following piece of code.
Can somebody kindly point out to me what or where I have gone wrong. When stepping
through the code with F10 it breaks when it gets to while

C#:
   SqlConnection cn = new SqlConnection(@"Server=WD\S789;Database=WinDI;User Id=;Password=;");
            SqlCommand cmd = new SqlCommand("SELECT zone,country,book,audio FROM Vand ", cn);
            cn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            {
                while (reader.Read())
                {
                    catalog(reader["zone"].ToString(), reader["country"].ToString(), reader["book"].ToString(),
                            reader["audio"].ToString());
                }
            }
            cn.Close();

Thank you
 
I put a red blob alongside the line where the button code starts and pressed start debugging.
Then I press F10 (step over). When the yellow marker gets to while (reader.Read()) it then
jumps down to the curly brace just before cn.Close(); It skips the part that starts with
catalog
 
I put a red blob alongside the line where the button code starts and pressed start debugging.
That red blob indicates a break point. If you say that you set a break point on that line then we'll know what you mean.
Then I press F10 (step over). When the yellow marker gets to while (reader.Read()) it then
jumps down to the curly brace just before cn.Close(); It skips the part that starts with
catalog
That's not "breaking". That's doing exactly what it should. That simply means that Read returned false on the first call. If you test the value of the HasRows property of the data reader at that point, you will presumably find that it is false. It simply means that the result set from your query is empty. Your loop reads data while there's data to read so, if there's no data to read, you'll never enter the loop body.
 
Thanks JM. I checked the table and there were indeed no entries/rows. Working as it should do now. Will be more
careful in the future. As to the red blob, didn't know what it was called. Saw a c# man do it once, so copied him.
I am, as you can gather, a novice
 
Back
Top Bottom