Link to home
Start Free TrialLog in
Avatar of Brian
BrianFlag for United States of America

asked on

Trouble Updating Data

Hell Experts,

I have the following code below. I tested all Stored Procedures and they all execute fine. Just trouble with the code (I believe it has something to do with PostBack(s).

What happens is that every time I try to fire the onclick="btnUpdateCourseDescription_Click" code the page refreshes itself and post the data that I try to edit back to it's original copy.


protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            RetrieveCoursesValues();
        }
        RetrieveCoursesDesc();
    }

    protected void RetrieveCoursesValues()
    {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HealthCourses"].ConnectionString))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "HealthCourses_RetrieveCoursesValues";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = conn;

                DataTable dtCoursesValues = new DataTable();
                SqlDataAdapter adp = new SqlDataAdapter();

                try
                {
                    conn.Open();

                    adp.SelectCommand = cmd;
                    adp.Fill(dtCoursesValues);

                    ddlCourseUpdate.DataSource = dtCoursesValues;
                    ddlCourseUpdate.DataValueField = "ghaoc_id";
                    ddlCourseUpdate.DataTextField = "ghaco_name";
                    ddlCourseUpdate.DataBind();
                }

                catch (Exception ex)
                {
                    ex.Message.ToString();
                }

                finally
                {
                    conn.Close();
                }
            }
    }

    protected void RetrieveCoursesDesc()
    {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HealthCourses"].ConnectionString))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "HealthCourses_RetrieveCoursesDescByID";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = conn;

                cmd.Parameters.AddWithValue("@ghaoc_id", SqlDbType.Int).Value = ddlCourseUpdate.SelectedValue;

                DataTable dtCoursesDesc = new DataTable();
                SqlDataAdapter adp = new SqlDataAdapter();

                try
                {
                    conn.Open();

                    adp.SelectCommand = cmd;
                    adp.Fill(dtCoursesDesc);

                    if ((dtCoursesDesc != null))
                    {
                        DataRow data = dtCoursesDesc.Rows[0];
                        TextBox1.Text = data["ghaco_desc"].ToString();
                    }
                }

                catch (Exception ex)
                {
                    ex.Message.ToString();
                }

                finally
                {
                    conn.Close();
                }
            }
    }

    protected void btnUpdateCourseDescription_Click(object sender, EventArgs e)
    {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["HealthCourses"].ConnectionString))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "HealthCourses_UpdateGeneralHealthAwarenessOneCourses";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = conn;

                cmd.Parameters.AddWithValue("@ghaoc_id", SqlDbType.Int).Value = hf_ghaoc_id.Value;
                cmd.Parameters.AddWithValue("@ghaco_desc", SqlDbType.VarChar).Value = TextBox1.Text;

                try
                {
                    conn.Open();

                    cmd.ExecuteNonQuery();

                    Server.Transfer("courses_update_success.aspx");
                }

                catch (Exception ex)
                {
                    ex.Message.ToString();
                }

                finally
                {
                    conn.Close();
                }
            }
    }
Avatar of Kiran Sonawane
Kiran Sonawane
Flag of India image

Why your are doing like this

 Server.Transfer("courses_update_success.aspx");

Instead call below method

RetrieveCoursesDesc();

OR

RetrieveCoursesValues();
Avatar of Brian

ASKER

Hi sonawanekiran,

Not sure what you mean. Server.Transfer("courses_update_success.aspx"); is used to send user to a Success page letting them know that they updated the data fine. I don't even see that page though.
Avatar of Brian

ASKER

Also, I just put a breakpoint on line below and noticed the value was "". Could this be why it cannot update? If so, how can I fix that?

cmd.Parameters.AddWithValue("@ghaoc_id", SqlDbType.Int).Value = hf_ghaoc_id.Value;
ASKER CERTIFIED SOLUTION
Avatar of Kiran Sonawane
Kiran Sonawane
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Brian

ASKER

Yes hf_ghaoc_id is a hidden field.

>> Check why not value is assigned to hidden field hf_ghaoc_id
I'm not sure why it's not set. Can you assist me in setting that value to page_load? That may be the problem because the Update is depending on that value ghaoc_id.
Avatar of Brian

ASKER

I figured it out using a different method.