Error updating the employee details!", help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Smurfas
    New Member
    • Jul 2008
    • 30

    Error updating the employee details!", help

    What's wrong?
    Compiletor don't show anything, but whet I start program show this error:
    "Error updating the employee details!"

    Code:
       protected void Button1_Click(object sender, EventArgs e)
        {
            //416 puslapis
            // 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,  City=@City " +
            "MobilePhone=@MobilePhone " +
            "WHERE EmployeeID=@EmployeeID", conn);
            // Add command parameters
            comm.Parameters.Add("@Name",
            System.Data.SqlDbType.NVarChar, 50);
            comm.Parameters["@Name"].Value = TextBox1.Text;
    
            comm.Parameters.Add("@City",
            System.Data.SqlDbType.NVarChar, 50);
            comm.Parameters["@City"].Value = TextBox2.Text;
         
            comm.Parameters.Add("@EmployeeID", System.Data.SqlDbType.Int);
            comm.Parameters["@EmployeeID"].Value = TextBox3.Text;
            // Enclose database code in Try-Catch-Finally
            try
            {
                // Open the connection
                conn.Open();
                // Execute the command
                comm.ExecuteNonQuery();
            }
            catch
            {
                // Display error message
                Label1.Text =
                "Error updating the employee details!<br />";
            }
            finally
            {
                // Close the connection
                conn.Close();
            }
            // Refresh the employees list
            //LoadEmployeesList();
        }
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Try to get your query by displaying it or showing it in a prompt or messagebox and paste it on your query analyzer and see the actual error.

    -- CK

    Comment

    • Delerna
      Recognized Expert Top Contributor
      • Jan 2008
      • 1134

      #3
      You have two statements in your try and if either one fails you get the same error message. I just hope you are not assuming its the update query that is at fault because that is what the error message says.
      In fact the problem could also be caused by the

      conn.Open();

      I suggest moving it outside the try/catch, just to eliminate it as the cause.
      Or even into its own try/catch.
      Once you have proven its not opening the connection that is causing the error then you need to bug find the update query.

      [code=text]
      conn.Open();
      try
      {
      // Open the connection

      // Execute the command
      comm.ExecuteNon Query();
      }
      catch
      {
      // Display error message
      Label1.Text =
      "Error updating the employee details!<br />";
      }
      finally
      {
      // Close the connection
      conn.Close();
      }
      [/code]

      It's always a process of isolate and eliminate when debugging errors.
      Break it up into smaller or simpler bits and test. If it works add the next bit

      Comment

      Working...