SqlDataReader doesn't read

Hello experts and merry Christmas,

I have a class with a simple job of retrieving userName and passWord from a table called users in an Sql database.

The reader gives me the following error which I don't understand:

Invalid attempt to call Read when reader is closed.

The code for the whole class is listed below
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public class Logining
    {
        private string _userName;
        private string _passWord;

        public string userName
        {
            get { return _userName; }
            set { _userName = value; }

        }

        public string passWord
        {
            get { return _passWord; }
            set { _passWord = value; }
        }
        public void ValidateUserCredentials(string name,string password)
        {
            SqlConnection con = default(SqlConnection);
            con = new SqlConnection("Server=PC;Database=HelpDesk;integrated security=True");
            SqlCommand comm = default(SqlCommand);
            comm = new SqlCommand("Select UserName,PassWord from Users", con);

            con.Open();
            SqlDataReader reader = comm.ExecuteReader();
           // reader = comm.ExecuteReader();
            while (reader.Read()) 
            {
                userName = Convert.ToString(reader["UserName"]);
                passWord = Convert.ToString(reader["passWord"]);
               // reader.Close();
                con.Close();
            
            }
        }




        }


    }

Open in new window

adamtraskAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
If you close the connection, as you are doing in line 37, then the reader is effectively closed as well. Close the connection once you finish reading (i.e. outside of your loop).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
adamtraskAuthor Commented:
Thank you kaufmed:, I am still having a hard time getting used to the role of the braces.
0
adamtraskAuthor Commented:
Thank you very much
0
käµfm³d 👽Commented:
NP. Glad to help  = )
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.