Unhandled Exception on ObjectDataSource Insert Method RRS feed

  • Question

  • User136156066 posted

    I have a datasource component that is coded as follows:


    [DataObjectMethod(DataObjectMethodType.Insert, true)]
            public void CreateMember(string Username,
                                 string Password,
                                 string Email,
                                 string Gender,
                                 string DOB)
                SqlCommand objCommand = new SqlCommand();
                objCommand.CommandText = "spCreateMember";
                objCommand.CommandType = CommandType.StoredProcedure;
                objCommand.Parameters.AddWithValue("@Username", Username);
                objCommand.Parameters.AddWithValue("@Password", Password);
                objCommand.Parameters.AddWithValue("@Email", Email);
                objCommand.Parameters.AddWithValue("@Gender", Gender);
                objCommand.Parameters.AddWithValue("@DOB", DOB);
                if (!CheckUsername(Username))
                    throw new Exception("The username entered is registered by another member.");
                if (!CheckEmail(Email))
                    throw new Exception("The e-mail address entered is registered by another member.");
                int intRowsAffected = DAL.SendData(objCommand);
                if (intRowsAffected < 1)
                    throw new Exception("An error occured during the registration process.");

    The CheckUsername and CheckEmail are left out but you get the idea. Problem is, if I try to call this passing a Username that exists, it tells me something about an unhandled exception. Now this is in my default.aspx.cs page....


     protected void btnRegister_Click(object sender, EventArgs e)
                    ObjectDataSource1.InsertParameters["Username"].DefaultValue = txtUsername.Text;
                    ObjectDataSource1.InsertParameters["Password"].DefaultValue = txtPassword.Text;
                    ObjectDataSource1.InsertParameters["Email"].DefaultValue = txtEmail.Text;
                    ObjectDataSource1.InsertParameters["Gender"].DefaultValue = ddlGender.SelectedValue;
                    ObjectDataSource1.InsertParameters["DOB"].DefaultValue = ddlMonth.SelectedValue + "/" + ddlDay.SelectedValue + "/" + ddlYear.SelectedValue;
                    RegistrationError.Text = "Your registration has been successful.<br />An e-mail has been sent to you with activation instructions.";
                catch (Exception ex)
                    RegistrationError.Text = ex.Message;
            protected void ObjectDataSource1_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
                if (e.Exception != null)
                    RegistrationError.Text = e.Exception.InnerException.Message;
                    e.ExceptionHandled = true;
    What's the dealio?

    Saturday, April 30, 2011 6:36 PM


  • User1288251396 posted

    You have try/catch logic in btnRegister_Click, but not in the other two functions.  If you are running in debug mode and an exception is thrown by code not within a "try" block, the debugger will stop and give you an "unhandled exception" message.

    You should:

    • inspect the exception that is being thrown to figure out why it's happening
    • consider putting everything within try/catch blocks, or adopt some other global exception-handling strategy

    If that doesn't help, tell us what the exception is (its message) and what line is throwing it.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 30, 2011 9:15 PM