Solved

Trouble Updating Data

Posted on 2011-09-28
6
214 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
Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

624 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