StateofDK
StateofDK

Reputation: 153

stored procedure not updating records

I have a c# function calling a SQL Server stored procedure with input parameters. The stored procedure works fine when executed directly through SQL Server Management Studio, so I believe the issue must be on the c# side, however I cannot figure it out for the life of me. I've already searched through all the previous times this issue has arisen for others and I have been unable to find a solution. If it ultimately can't be determined what's wrong, some tips on debugging would be very much appreciated as I'm not too good at more advanced troubleshooting.

here is the c# function:

protected void UpdateProfile(object sender, EventArgs e)
    {
        string connstring = ConfigurationManager.ConnectionStrings["TimeHubDBCS"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connstring))
        {
            try
            {
                SqlCommand cmd = new SqlCommand("spUpdateProfile", conn);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;

                cmd.Parameters.Add("@UserId", SqlDbType.VarChar).Value = userloggedin.Text;

                cmd.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = first_name.Text;
                cmd.Parameters.Add("@LastName", SqlDbType.VarChar).Value = last_name.Text;
                cmd.Parameters.Add("@MiddleInitial", SqlDbType.VarChar).Value = middle_initial.Text;
                cmd.Parameters.Add("@Assignment", SqlDbType.VarChar).Value = ddl_assignment.Text;
                cmd.Parameters.Add("@Rank", SqlDbType.VarChar).Value = rank.Text;
                cmd.Parameters.Add("@Star", SqlDbType.VarChar).Value = star.Text;

                cmd.Parameters.Add("@ContactPhone", SqlDbType.VarChar).Value = contact_phone.Text;
                cmd.Parameters.Add("@PhoneType", SqlDbType.VarChar).Value = phone_type.Text;
                cmd.Parameters.Add("@Email", SqlDbType.VarChar).Value = email.Text;
                cmd.Parameters.Add("@Shift", SqlDbType.VarChar).Value = ddl_shift.Text;

                cmd.Parameters.Add("@ModifyDate", SqlDbType.VarChar).Value = DateTime.Now.ToString();
                cmd.Parameters.Add("@ModifiedBy", SqlDbType.VarChar).Value = userloggedin.Text;
                cmd.Parameters.Add("@StatusId", SqlDbType.VarChar).Value = "active";

                conn.Open();
                cmd.ExecuteNonQuery();

                Response.Write("Profile updated successfully!");
            }
            catch (Exception ex)
            {
                Response.Write("Profile update error:  " + ex.Message);
            }
        }
    }

here is the sql server stored procedure:

ALTER PROCEDURE spUpdateProfile

@UserId         VARCHAR(200)    =   NULL    ,

@FirstName      VARCHAR(200)    =   NULL    ,
@LastName       VARCHAR(200)    =   NULL    ,
@MiddleInitial  VARCHAR(200)    =   NULL    ,
@Assignment     VARCHAR(200)    =   NULL    ,
@Rank           VARCHAR(200)    =   NULL    ,
@Star           VARCHAR(200)    =   NULL    ,

@ContactPhone   VARCHAR(200)    =   NULL    ,
@PhoneType      VARCHAR(200)    =   NULL    ,
@Email          VARCHAR(200)    =   NULL    ,
@Shift          VARCHAR(200)    =   NULL    ,

@ModifyDate     VARCHAR(200)    =   NULL    ,
@ModifiedBy     VARCHAR(200)    =   NULL    ,
@StatusId       VARCHAR(200)    =   NULL    

AS
BEGIN
UPDATE dbo.users
    SET
        first_name          =   @FirstName          ,
        last_name           =   @LastName           ,
        middle_initial      =   @MiddleInitial      ,
        assignment          =   @Assignment         ,
        user_rank           =   @Rank               ,
        user_star           =   @Star               ,
        contact_phone       =   @ContactPhone       ,
        phone_type          =   @PhoneType          ,
        email               =   @Email              ,
        regular_shift       =   @Shift              ,
        modify_date         =   @ModifyDate         ,
        modified_by         =   @ModifiedBy         ,
        status_id           =   @StatusId           
    WHERE
        user_id             =   @UserId
END

Upvotes: 0

Views: 308

Answers (2)

StateofDK
StateofDK

Reputation: 153

Partha answered the question in the comments section under the original post.

Issue was that I had a function which populated textboxes on the page with user data on page_load. Every time the update function ran, the page posted back to the server and the page_load event fired again, repopulating the textboxes with the original data, and then using that data to update the record in my table.

Adding if(!Page.ispostback){function} around the function within the pag_load event solved the issue by only populating those fields if the page was loaded the first time, and not on refreshes/postbacks

Upvotes: 0

Peter Lange
Peter Lange

Reputation: 2896

Your description is missing a key factor in troubleshooting code. What exactly is the error? If you cant identify the error, you can't really begin to troubleshoot it.

Put a break point on your catch and try to run it. When it stops at that break point, take a look at the exception and the exception's stack trace. It will usually tell you why you failed. Also, you can put a break point at the top of the procedure then mouse over each of the variables and see if they are all present. Maybe one is missing or has an incorrect data type.

Upvotes: 1

Related Questions