Solved

Trouble Updating Data

Posted on 2011-09-28
6
204 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now