Click here to Skip to main content
15,899,935 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!
I have a page whose purpose is to allow the User to edit his/her personal details. For that, first I pull the details in the Page_load function and then there is a button, which when clicked pushes the changes made to the database.
However, the changes are NOT posted back to the database for some reason - meaning that the changes are not posted back : the ORIGINAL data that I extracted is ONLY being used for the updating purpose as well. The code is as below. Please Help ... can't seem to figure out where I'm going wrong .. Thanks!


Function 1 - Page_Load :

C#
protected void Page_Load(object sender, EventArgs e)
        {
            string str = "<MARQUEE> NOTIFICATIONS </MARQUEE>";
            Literal1.Text = str;

            using (SqlConnection con1 = new SqlConnection("server=.\\SQLEXPRESS;database=dB;Integrated Security=SSPI;"))
            {
                string strValue = Session["User"].ToString();

                using (var connection = new SqlConnection("server=.\\SQLEXPRESS;database=dB;Integrated Security=SSPI;"))
                {
                    using (var command = connection.CreateCommand())
                    {
                        command.CommandText = "SELECT empFname,empLname,pwd,empBday,empTitle,city,empEmail,empAddr,empMobNo,empLocNo,empLoginId FROM [emp] WHERE ( [empLoginId] = @empLogin ) ";
                        command.Parameters.AddWithValue("@empLogin", strValue);
                        connection.Open();
                        using (var reader = command.ExecuteReader())
                        {
                            if (reader.Read())
                            {
                                TextBox1.Text = reader["empFname"].ToString().ToUpper().Trim();
                                TextBox2.Text = reader["empLname"].ToString().ToUpper().Trim();
                                TextBox3.Text = reader["empLoginId"].ToString();
                                TextBox4.Text = reader["pwd"].ToString();
                                TextBox5.Text = reader["empBday"].ToString().ToUpper().Trim();
                                TextBox6.Text = reader["empTitle"].ToString().ToUpper().Trim();
                                TextBox7.Text = reader["city"].ToString().ToUpper().Trim();
                                TextBox8.Text = reader["empEmail"].ToString();
                                TextBox11.Text = reader["empAddr"].ToString().ToUpper().Trim();
                                TextBox9.Text = reader["empMobNo"].ToString().ToUpper().Trim();
                                TextBox10.Text = reader["empLocNo"].ToString().ToUpper().Trim();
                            }
                        }
                        connection.Close();
                    }
                }
            }
        }


Function 2 - Button1_Click :

C#
protected void Button1_Click(object sender, EventArgs e)
        {
            string pwdChange = TextBox4.Text.ToString().Trim();
            string bdayChange = TextBox5.Text.ToString() ;
            string mailChange = TextBox8.Text.ToString();
            string addrChange = TextBox11.Text.ToString().ToUpper().Trim();
            string mobChange = TextBox9.Text.ToString();
            string locChange = TextBox10.Text.ToString();
            string strValue = Session["User"].ToString();

                using (SqlConnection connection = new SqlConnection("server=.\\SQLEXPRESS;database=dB;Integrated Security=SSPI;"))
                {
                    connection.Open();
                    using (SqlCommand command = new SqlCommand("UPDATE emp set pwd = @pwdChan, empBday = @bdayChan, empEmail = @mailChan, empAddr = @addrChan, empMobNo = @mobChan, empLocNo = @locChan  where empLoginId = @empLogin", connection))
                    {
                        command.Parameters.AddWithValue("@pwdChan", pwdChange);
                        command.Parameters.AddWithValue("@bdayChan", bdayChange);
                        command.Parameters.AddWithValue("@mailChan", mailChange);
                        command.Parameters.AddWithValue("@addrChan", addrChange);
                        command.Parameters.AddWithValue("@mobChan", mobChange);
                        command.Parameters.AddWithValue("@locChan", locChange);
                        command.Parameters.AddWithValue("@empLogin",  strValue);
                        if (command.ExecuteNonQuery()>0)
                        {
                            Label5.Text="success" + pwdChange;
                        }


                    }
                    connection.Close();
                }
        }


The pwd for the user is 'vd' originally. After changing the pwd text box to 'darkd' and clicking the Submit Button, the label5 displays 'successvd' - which implies that the data taken from the the database table is being used again only!
Posted
Updated 23-May-13 1:11am
v2

1 solution

Hello,

You have to wrap your entire page_load code inside IsPostback
e.g.
C#
protected void Page_Load(object sender, EventArgs e)
{
     if(!IsPostBack)
     {    
            string str = "<MARQUEE> NOTIFICATIONS </MARQUEE>";
            ......
            ......
     }    
}

Thanks,
Imdadhusen
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900