We help IT Professionals succeed at work.

C# reader.Read if statement

elliottbenzle asked
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

The Read() method on a DataReader attempts to advance to the next record in a result set returns true if the reader has a result row in scope.  Another use for it is in a while(reader.Read()) loop in which you want to perform some operation for each row returned by a query.  The loop will end when no more rows remain.

One logical problem with attempting to check (!reader.Read()) after checking (reader.Read()) is that you're attempting to advance through two rows in the result set, when you probably only intend to advance to the first row and then fill the textboxes with values if a row existed and with empty strings otherwise.  Using "else" is a better choice for what you need to do, but I am unsure why (!reader.Read()) would not also work.
Top Expert 2010


Try the below change:

comm.Parameters["@EmployeeID"].Value = Conver.ToInt32(employeeList.SelectedItem.Value); // convert Value to int and then set.

I think employeeList.SelectedItem.Value is a string type. since EmployeeID's type is System.Data.SqlDbType.Int, better you set the parameter value to int type as well.

If there is no record fetched from db, then if (reader.Read()) will not pass...


Great. Thank you.