I have been working through the book and every thing was going fine till I got to chapter 9 after adding the admin tools web from i just keep getting “Error loading the employee details!” when I hit the select button when it executes the reader = comm.ExecuteReader() (in the slecetButton_Click procedure)line it jumps to the catch statement. I am using visual studio express and sql express would like some help. PS I have loved the book so far very informative and easy to follow also I am using C# code not the VB code thanks in advance
Here is the code that i am using
protected void selectButton_Click(object sender, EventArgs e)
{
// Declare objects
SqlConnection conn;
SqlCommand comm;
SqlDataReader reader;
// Read the connection string from Web.config
string connectionString =
ConfigurationManager.ConnectionStrings[
“Dorknozzle”].ConnectionString;
// Initialize connection
conn = new SqlConnection(connectionString);
// Create command
comm = new SqlCommand(
“SELECT Name, Username, Address, City, State, Zip, " +
“HomePhone, Extension, MobilePhone FROM Employees " +
“WHERE EmployeeID = @EmployeeID”, conn);
// Add command parameters
comm.Parameters.Add(”@EmployeeID”, System.Data.SqlDbType.Int);
comm.Parameters[“@EmployeeID”].Value =
employeesList.SelectedItem.Value;
// Enclose database code in Try-Catch-Finally
try
{
// Open the connection
conn.Open();
// Execute the command reader = comm.ExecuteReader(); // Display the data on the form
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();
}
// Close the reader
reader.Close();
// Enable the Update button
updateButton.Enabled = true;
// Enable the Delete button
deleteButton.Enabled = true;
}
catch
{
// Display error message
dbErrorLabel.Text =
“Error loading the employee details!<br />”;
}
finally
{
// Close the connection
conn.Close();
}
}
I tried to get them to pull the empty catch {} blocks out. They are pretty bad. Anyhow, it is probably a pretty easy database-level error, what you should do is comment out or remove the:
I did comment out the catch and this is what I found out.
protected void selectButton_Click(object sender, EventArgs e)
{
// Declare objects
SqlConnection conn;
SqlCommand comm;
SqlDataReader reader;
// Read the connection string from Web.config
string connectionString =
ConfigurationManager.ConnectionStrings[
“Dorknozzle”].ConnectionString;
// Initialize connection
conn = new SqlConnection(connectionString);
// Create command
comm = new SqlCommand(
“SELECT Name, Username, Address, City, State, Zip, " +
“HomePhone, Extension, MobilePhone FROM Employees " +
“WHERE EmployeeID = @EmployeeID”, conn);
// Add command parameters
comm.Parameters.Add(”@EmployeeID”, System.Data.SqlDbType.Int);
comm.Parameters[“@EmployeeID”].Value =
employeesList.SelectedItem.Value;
// Enclose database code in Try-Catch-Finally
try
{
// Open the connection
conn.Open();
// Execute the command
reader = comm.ExecuteReader(); // Display the data on the form
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();
}
// Close the reader
reader.Close();
// Enable the Update button
updateButton.Enabled = true;
// Enable the Delete button
deleteButton.Enabled = true;
}
catch
{
// Display error message
dbErrorLabel.Text =
“Error loading the employee details!<br />”;
}
finally
{
// Close the connection
conn.Close();
}
}
In both places the phrase “MobilePhone” is spelled wrong it should be “MobliePhone” the “l” and the “i” are reverse once I fixed the spelling it worked just fine thanks for the debugging tip worked great!