SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with Build Your Own ASP.NET 4 Website

    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();
    }
    }

  2. #2
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,576
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code:
    catch
    {
    // Display error message
    dbErrorLabel.Text =
    "Error loading the employee details!<br />";
    }
    Bit, you should then get a yellow screen of death telling you that you didn't have the connection string correct of what have you.

  3. #3
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!

  4. #4
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just in the spirit of completeness this procedures in the same Admintools.aspx.cs file needs fixing as well

    protected void updateButton_Click(object sender, EventArgs e)
    {
    // Declare objects
    SqlConnection conn;
    SqlCommand comm;
    // Read the connection string from Web.config
    string connectionString =
    ConfigurationManager.ConnectionStrings[
    "Dorknozzle"].ConnectionString;
    // Initialize connection
    conn = new SqlConnection(connectionString);
    // Create command
    comm = new SqlCommand(
    "UPDATE Employees SET Name=@Name, Username=@Username, " +
    "Address=@Address, City=@City, State=@State, Zip=@Zip, " +
    "HomePhone=@HomePhone, Extension=@Extension, " +
    "MobilePhone=@MobilePhone " +
    "WHERE EmployeeID=@EmployeeID", conn);
    // Add command parameters
    comm.Parameters.Add("@Name",
    System.Data.SqlDbType.NVarChar, 50);
    comm.Parameters["@Name"].Value = nameTextBox.Text;
    comm.Parameters.Add("@Username",
    System.Data.SqlDbType.NVarChar, 50);
    comm.Parameters["@Username"].Value = userNameTextBox.Text;
    comm.Parameters.Add("@Address",
    System.Data.SqlDbType.NVarChar, 50);
    comm.Parameters["@Address"].Value = addressTextBox.Text;
    comm.Parameters.Add("@City",
    System.Data.SqlDbType.NVarChar, 50);
    comm.Parameters["@City"].Value = cityTextBox.Text;
    comm.Parameters.Add("@State",
    System.Data.SqlDbType.NVarChar, 50);
    comm.Parameters["@State"].Value = stateTextBox.Text;
    comm.Parameters.Add("@Zip",
    System.Data.SqlDbType.NVarChar, 50);
    comm.Parameters["@Zip"].Value = zipTextBox.Text;
    comm.Parameters.Add("@HomePhone",
    System.Data.SqlDbType.NVarChar, 50);
    comm.Parameters["@HomePhone"].Value = homePhoneTextBox.Text;
    comm.Parameters.Add("@Extension",
    System.Data.SqlDbType.NVarChar, 50);
    comm.Parameters["@Extension"].Value = extensionTextBox.Text;
    comm.Parameters.Add("@MobilePhone",
    System.Data.SqlDbType.NVarChar, 50);
    comm.Parameters["@MobilePhone"].Value = mobilePhoneTextBox.Text;
    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
    comm.ExecuteNonQuery();
    }
    catch
    {
    // Display error message
    dbErrorLabel.Text =
    "Error updating the employee details!<br />";
    }
    finally
    {
    // Close the connection
    conn.Close();
    }

    The correct spelling again is MobliePhone

  5. #5
    SitePoint Member
    Join Date
    Feb 2012
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The mistake was Mine I spelled it worng when I created my employee's table in sql I have changed the name in that column to the correct spelling


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •