3

Here is my OnClick event:

protected void Submit_Click(object sender, EventArgs e)
{
    try
    {
        using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlCommand cmd = new SqlCommand("administratorUpdate", con))
            {
                cmd.Parameters.Add("@originalID", SqlDbType.VarChar).Value = IDHF.Value;
                cmd.Parameters.Add("@firstName", SqlDbType.VarChar).Value = firstNameTB.Text;
                cmd.Parameters.Add("@lastName", SqlDbType.VarChar).Value = lastNameTB.Text;
                cmd.Parameters.Add("@userName", SqlDbType.VarChar).Value = userNameTB.Text;
                cmd.Parameters.Add("@emailAddress", SqlDbType.VarChar).Value = emailAddressTB.Text;
                cmd.CommandType = CommandType.StoredProcedure;
            }
        }
        GetAllRPT.DataBind();
        Label ErrorMessageLabel = (Label)Master.FindControl("ErrorMessageLabel");
        new myFunctions().DisplayUserMessage("success", "Administrator Updated!", ErrorMessageLabel);
        AdminForm.Visible = false;
    }
    catch (Exception ex)
    {
        Label ErrorMessageLabel = (Label)Master.FindControl("ErrorMessageLabel");
        new myFunctions().DisplayUserMessage("error", ex.Message, ErrorMessageLabel);
    }
}

Here is my Stored Procedure [administratorUpdate]:

ALTER PROCEDURE [dbo].[administratorUpdate]
    @originalID UNIQUEIDENTIFIER,
    @firstName varchar (100),
    @lastName varchar (100),
    @userName varchar (100),
    @emailAddress varchar (100),
    @password varchar (100),
    @isActive bit
AS

UPDATE administrator
SET userName = @userName,
    emailAddress = @emailAddress,
    password = @password,
    firstName = @firstName,
    lastName = @lastName,
    isActive = @isActive
WHERE
    ID = @originalID

The code executes and does the success message but the datatable doesn't update. Who knows why? I am new to using Stored Procedures in ASP.

7
  • @Raj More Thanks, I didn't know how to format the SQL properly Commented Dec 15, 2010 at 23:57
  • Whats with your naming conventions? Commented Dec 15, 2010 at 23:57
  • @Phill What do you mean? Camel case? Commented Dec 15, 2010 at 23:59
  • 'IDHF.Value' 'firstNameTB.Text' 'administratorUpdate' 'ErrorMessageLabel' 'myFunctions' It's all very inconsistent. txtFirstName, AdministratorUpdate, lblErrorMessage, MyFunctions. The two that stand out most to me is your function class and the server controls. Commented Dec 16, 2010 at 0:02
  • @Phill Hmm good point. I will work on that. Thanks! Commented Dec 16, 2010 at 0:03

2 Answers 2

5

Are you executing the command?

    using (SqlConnection con = new SqlConnection(conString))
    {
        conn.Open(); //this was missing

        using (SqlCommand cmd = new SqlCommand("administratorUpdate", con))
        {
            cmd.Parameters.Add("@originalID", SqlDbType.VarChar).Value = IDHF.Value;
            cmd.Parameters.Add("@firstName", SqlDbType.VarChar).Value = firstNameTB.Text;
            cmd.Parameters.Add("@lastName", SqlDbType.VarChar).Value = lastNameTB.Text;
            cmd.Parameters.Add("@userName", SqlDbType.VarChar).Value = userNameTB.Text;
            cmd.Parameters.Add("@emailAddress", SqlDbType.VarChar).Value = emailAddressTB.Text;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.ExecuteNonQuery(); //this was missing.
        }

        conn.Close();
    }
Sign up to request clarification or add additional context in comments.

6 Comments

Also need to open the connection to the database by doing con.Open(); before doing cmd.ExecuteNonQuery();
@Dismissile I thought when I am using 'using' it's open whilest the data is in that statement. I can pull data down, but sending data i guess is different?
That too. I have modified the answer with the proper implementation.
@Babak Don't I have to close it?
@Bry4n, yes you are correct. My main point was that you're not executing.
|
1

My trick here is to do this in debug mode without the Try / Catch. The error(s) generated will point to the problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.