Matthew
Matthew

Reputation: 4607

Strange Bug when getting new values from textbox

I have a website which, on page load, retrieves certain information from the database and populates a number of textboxes. The user can then modify the contents of these textboxes if he wants to make any changes and he can then save the changes.

This is what I am using in order to get the contents of the boxes before saving the changes:

string username = Session["Username"].ToString();
string vendor_name = Textbox_Vendor.Text;
string address = Textbox_Address.Text;
string city = Textbox_City.Text;
string country = Textbox_Country.Text;

Now, the main problem I am having is that the values being retrieved are the old values. For instance, let us say that on page load, the tel field was 12345678 and that the user changed the value in the textbox to 789046778. The value that is being loaded in the variable tel just before saving the changes is 12345678 instead of 789046778.

How can I retrieve the new updated value from the textboxs please? Thank you :)

Page Load

protected void Page_Load(object sender, EventArgs e)
{
    Label_Error.Visible = false;
    Encryption enc = new Encryption();

    if (Session["Username"] == null)
    {
        Response.Redirect("HomePage.aspx");
    }

    else
    {
        string username = Session["Username"].ToString();
        string vendor_name = "";
        string address = "";
        string city = "";
        string country = "";
        bool exists1 = false;
        bool error1 = true;

        try
        {
            string connection1 = ConfigurationManager.ConnectionStrings["DB_Connection"].ConnectionString;
            SqlConnection conn1 = new SqlConnection(connection1);
            SqlDataReader rdr1 = null;

            conn1.Open();
            SqlCommand cmd1 = new SqlCommand("GetByUsername", conn1);
            cmd1.CommandType = CommandType.StoredProcedure;
            cmd1.Parameters.AddWithValue("@username", username);
            rdr1 = cmd1.ExecuteReader();

Upvotes: 3

Views: 1096

Answers (1)

Andrei
Andrei

Reputation: 56716

New values are being overwritten by old values in Page_Load. You are assigning old values to textboxes on every page load, while really you only want to do this during first call. On the postbacks you do not need to assign anything:

if (!Page.IsPostback)
{
    // first call - assign values
    Textbox_Vendor.Text = vendor_name;
    ...
}

Upvotes: 5

Related Questions