elliottbenzle
asked on
C# reader.Read if statement
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
if(!read.Read())
but this didn't work. Why not?
Thanks
protected void employeeList_SelectedIndex Changed(ob ject sender, EventArgs e)
{
{
SqlConnection conn;
SqlCommand comm;
SqlDataReader reader;
string connectionString = ConfigurationManager.Conne ctionStrin gs["Dorkno zzle"].Con nectionStr ing;
conn = new SqlConnection(connectionSt ring);
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("@Empl oyeeID", System.Data.SqlDbType.Int) ;
comm.Parameters["@Employee ID"].Value = employeeList.SelectedItem. Value;
try
{
conn.Open();
reader = comm.ExecuteReader();
if (reader.Read())
{
nameTextBox.Text = reader["Name"].ToString();
userNameTextBox.Text = reader["Username"].ToStrin g();
addressTextBox.Text = reader["Address"].ToString ();
cityTextBox.Text = reader["City"].ToString();
stateTextBox.Text = reader["State"].ToString() ;
zipTextBox.Text = reader["Zip"].ToString();
homePhoneTextBox.Text = reader["HomePhone"].ToStri ng();
extensionTextBox.Text = reader["Extension"].ToStri ng();
mobilePhoneTextBox.Text = reader["MobilePhone"].ToSt ring();
}
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 = "";
}
reader.Close();
updateButton.Enabled = true;
deleteButton.Enabled = true;
}
catch
{
dbErrorLabel.Text =
"Error loading the employee details!<br />";
}
finally
{
conn.Close();
}
}
}
if (reader.Read())
to me in the following context.
Also I tried to use
if(!read.Read())
but this didn't work. Why not?
Thanks
protected void employeeList_SelectedIndex
{
{
SqlConnection conn;
SqlCommand comm;
SqlDataReader reader;
string connectionString = ConfigurationManager.Conne
conn = new SqlConnection(connectionSt
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("@Empl
comm.Parameters["@Employee
try
{
conn.Open();
reader = comm.ExecuteReader();
if (reader.Read())
{
nameTextBox.Text = reader["Name"].ToString();
userNameTextBox.Text = reader["Username"].ToStrin
addressTextBox.Text = reader["Address"].ToString
cityTextBox.Text = reader["City"].ToString();
stateTextBox.Text = reader["State"].ToString()
zipTextBox.Text = reader["Zip"].ToString();
homePhoneTextBox.Text = reader["HomePhone"].ToStri
extensionTextBox.Text = reader["Extension"].ToStri
mobilePhoneTextBox.Text = reader["MobilePhone"].ToSt
}
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 = "";
}
reader.Close();
updateButton.Enabled = true;
deleteButton.Enabled = true;
}
catch
{
dbErrorLabel.Text =
"Error loading the employee details!<br />";
}
finally
{
conn.Close();
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Great. Thank you.
Try the below change:
comm.Parameters["@Employee
I think employeeList.SelectedItem.
If there is no record fetched from db, then if (reader.Read()) will not pass...