Solved

Trouble Updating Data

Posted on 2011-09-28
6
209 Views
Last Modified: 2012-05-12
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();
                }
            }
    }
0
Comment
Question by:asp_net2
  • 4
  • 2
6 Comments
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36716704
Why your are doing like this

 Server.Transfer("courses_update_success.aspx");

Instead call below method

RetrieveCoursesDesc();

OR

RetrieveCoursesValues();
0
 
LVL 4

Author Comment

by:asp_net2
ID: 36716717
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.
0
 
LVL 4

Author Comment

by:asp_net2
ID: 36716733
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;
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 17

Accepted Solution

by:
sonawanekiran earned 500 total points
ID: 36716765
I guess hf_ghaoc_id is a hidden field and and value is not set to hidden field during page load or something.
Check why not value is assigned to hidden field hf_ghaoc_id
0
 
LVL 4

Author Comment

by:asp_net2
ID: 36716816
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.
0
 
LVL 4

Author Closing Comment

by:asp_net2
ID: 36718733
I figured it out using a different method.
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question