We help IT Professionals succeed at work.
Get Started

C# reader.Read if statement

Last Modified: 2012-05-09
On my site I have a drop down menu that is used to select the value for a WHERE clause in my SQL statment. The user picks a name from the drop down menu which activates the SQL statement and selects the corresponding record from the database and populates text fields below with the corresponding data. The problem I'm running into is that the first item in the drop down menu is an entry that says "SELECT EMPLOYEE" if a user selects this after selecting an employee name I want the text fields to clear out the value. I got this to work using an if else statement but don't understand why. Can someone please explain the use of:
if (reader.Read())
to me in the following context.
Also I tried to use
but this didn't work. Why not?


 protected void employeeList_SelectedIndexChanged(object sender, EventArgs e)
            SqlConnection conn;
            SqlCommand comm;
            SqlDataReader reader;
            string connectionString = ConfigurationManager.ConnectionStrings["Dorknozzle"].ConnectionString;
            conn = new SqlConnection(connectionString);
            comm = new SqlCommand("SELECT Name, EmployeeID, DepartmentID, Username, Password, Address, City, State, Zip, HomePhone, Extension, MobilePhone FROM Employees WHERE EmployeeID = @EmployeeID", conn);
            comm.Parameters.Add("@EmployeeID", System.Data.SqlDbType.Int);
            comm.Parameters["@EmployeeID"].Value = employeeList.SelectedItem.Value;
                reader = comm.ExecuteReader();
                if (reader.Read())
                    nameTextBox.Text = reader["Name"].ToString();
                    userNameTextBox.Text = reader["Username"].ToString();
                    addressTextBox.Text = reader["Address"].ToString();
                    cityTextBox.Text = reader["City"].ToString();
                    stateTextBox.Text = reader["State"].ToString();
                    zipTextBox.Text = reader["Zip"].ToString();
                    homePhoneTextBox.Text = reader["HomePhone"].ToString();
                    extensionTextBox.Text = reader["Extension"].ToString();
                    mobilePhoneTextBox.Text = reader["MobilePhone"].ToString();
                else         <<<<<<<<<<<<<<<<<<<here i tried if(!reader.Read()) but it didn't work
                    nameTextBox.Text = "";
                    userNameTextBox.Text = "";
                    addressTextBox.Text = "";
                    cityTextBox.Text = "";
                    stateTextBox.Text = "";
                    zipTextBox.Text = "";
                    homePhoneTextBox.Text = "";
                    extensionTextBox.Text = "";
                    mobilePhoneTextBox.Text = "";
                updateButton.Enabled = true;
                deleteButton.Enabled = true;
                dbErrorLabel.Text =
                "Error loading the employee details!<br />";
Watch Question
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE