Solved

Trouble Updating Data

Posted on 2011-09-28
6
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

739 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