Vinayaka Karjigi
Vinayaka Karjigi

Reputation: 1090

Not able to edit the textbox in asp.net

I have below code running on page_load

 using (SqlConnection conn = new SqlConnection("myconnectionString"))
        {
            conn.Open();
            using (SqlCommand cmmnd = new SqlCommand("", conn))
            {



                cmmnd.CommandText = "SELECT * FROM addsetting;";


                SqlDataReader rdr = cmmnd.ExecuteReader();
                while (rdr.Read())
                {
                    count++;
                    param = Convert.ToString(rdr["rowno"]);
                    TextBox1.Text = Convert.ToString(rdr["tostudent"]);
                    TextBox2.Text = Convert.ToString(rdr["tofaculty"]);
                    TextBox3.Text = Convert.ToString(rdr["studentday"]);
                    TextBox4.Text = Convert.ToString(rdr["facultyday"]);
                    TextBox5.Text = Convert.ToString(rdr["firstweek"]);
                    TextBox6.Text = Convert.ToString(rdr["secondweek"]);
                    TextBox7.Text = Convert.ToString(rdr["thirdweek"]);

                }
                rdr.Close();
            }
            conn.Close();}

and now after this form is filled, i am changing the values and have a "save" button.

strangly on Button2_Click, when i try to access the edited values from the textbox, i am not getting new values. the textbox is having old values.

why textbox is not having the edited values.

below is the code which i am using to acces the values.

 using (SqlConnection conn = new SqlConnection("myconnectionstring"))
            {
                conn.Open();



                using (SqlCommand cmd = new SqlCommand("", conn))
                {
                    cmd.Parameters.Add("@rowno", SqlDbType.VarChar).Value = param; 

                    cmd.Parameters.AddWithValue("@tostudent", TextBox1.Text); 
                    cmd.Parameters.AddWithValue("@tofaculty", TextBox2.Text);
                    cmd.Parameters.AddWithValue("@studentday", TextBox3.Text);
                    cmd.Parameters.AddWithValue("@facultyday", TextBox4.Text);
                    cmd.Parameters.AddWithValue("@firstweek", TextBox5.Text);
                    cmd.Parameters.AddWithValue("@secondweek", TextBox6.Text);
                    cmd.Parameters.AddWithValue("@thirdweek", TextBox7.Text);


                    cmd.CommandText = "UPDATE addsetting SET tostudent=@tostudent,tofaculty=@tofaculty,studentday=@studentday,facultyday=@facultyday,firstweek=@firstweek,secondweek=@secondweek,thirdweek=@thirdweek WHERE rowno=@rowno";
                    cmd.ExecuteNonQuery();

                }

                conn.Close();

Upvotes: 3

Views: 1395

Answers (3)

Esha Garg
Esha Garg

Reputation: 144

if(!IsPostBack)
{
    using (SqlConnection conn = new SqlConnection("myconnectionString"))
    {
        conn.Open();
        using (SqlCommand cmmnd = new SqlCommand("", conn))
        {



            cmmnd.CommandText = "SELECT * FROM addsetting;";


            SqlDataReader rdr = cmmnd.ExecuteReader();
            while (rdr.Read())
            {
                count++;
                param = Convert.ToString(rdr["rowno"]);
                TextBox1.Text = Convert.ToString(rdr["tostudent"]);
                TextBox2.Text = Convert.ToString(rdr["tofaculty"]);
                TextBox3.Text = Convert.ToString(rdr["studentday"]);
                TextBox4.Text = Convert.ToString(rdr["facultyday"]);
                TextBox5.Text = Convert.ToString(rdr["firstweek"]);
                TextBox6.Text = Convert.ToString(rdr["secondweek"]);
                TextBox7.Text = Convert.ToString(rdr["thirdweek"]);

            }
            rdr.Close();
        }
        conn.Close();}

}

Upvotes: 1

mr_plum
mr_plum

Reputation: 2437

It's all about the page life cycle. Page_Load fires before the Click events are handled. So, when you click Button2, Page_Load runs and repopulates your form with old values, then Button2.Click is executed and saves the current (now old) values back to the database.

Upvotes: 0

nkvu
nkvu

Reputation: 5831

Are you checking the IsPostBack property in your page_load? Or does the code you have outlined run regardless of whether there is a postback or not?

If the code runs regardless of postback then what you are doing is overwriting the TextBox values before your Button2_Click has a chance to access them. This is because of the page lifecycle where the Load is run before event handling.

You may only want the database retrieval in your page_load to run if it is not a postback (i.e. user has not clicked a submit button), for example:

if (!Page.IsPostBack)
{
    using (SqlConnection conn = new SqlConnection("myconnectionString"))
    {
        conn.Open();
        using (SqlCommand cmmnd = new SqlCommand("", conn))
        {
            cmmnd.CommandText = "SELECT * FROM addsetting;";
            SqlDataReader rdr = cmmnd.ExecuteReader();

            while (rdr.Read())
            {
                count++;
                param = Convert.ToString(rdr["rowno"]);
                TextBox1.Text = Convert.ToString(rdr["tostudent"]);
                TextBox2.Text = Convert.ToString(rdr["tofaculty"]);
                TextBox3.Text = Convert.ToString(rdr["studentday"]);
                TextBox4.Text = Convert.ToString(rdr["facultyday"]);
                TextBox5.Text = Convert.ToString(rdr["firstweek"]);
                TextBox6.Text = Convert.ToString(rdr["secondweek"]);
                TextBox7.Text = Convert.ToString(rdr["thirdweek"]);
            }

            rdr.Close();
        }
    }
}

Upvotes: 5

Related Questions