XiAnG
XiAnG

Reputation: 215

How to not catch the old data in page load

I trying to update my profile but it keep catching the old data when it update. What can be done to solve this problem. Please help me out on this problem thanks!

protected void Page_Load(object sender, EventArgs e)
{

    String nric = (String)Session["nric"];

    SqlConnection con = new SqlConnection("Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI");
    con.Open();
    SqlCommand cm = new SqlCommand("Select * from MemberAccount where nric = '" + nric + "'", con);
    SqlDataReader dr;
    dr = cm.ExecuteReader();
    if (dr.Read())
    {
        txtFullName.Text = dr["fullname"].ToString();
        txtAddress.Text = dr["address"].ToString();
        txtContact.Text = dr["contact"].ToString();
        txtEmail.Text = dr["email"].ToString();
    }

    con.Close();

}

protected void btnUpdate_Click(object sender, EventArgs e)
{
    String nric = (String)Session["nric"];

    if (txtContact.Text.Equals("") || txtEmail.Text.Equals(""))
    {
        lblMessage.Text = "Do not leave blank fill to update your profile!";
    }
    else
    {    
        string strQuery = "Update MemberAccount Set address = '" + txtAddress.Text + "', contact = '" + txtContact.Text + "', email = '" + txtEmail.Text + "' Where nric = '" + nric + "'";
        SqlCommand cmd = new SqlCommand(strQuery);
        InsertUpdateData(cmd);
        lblMessage.ForeColor = System.Drawing.Color.Green;
        lblMessage.Text = "Profile Updated.";
    }
}

Upvotes: 0

Views: 71

Answers (1)

Jonathan
Jonathan

Reputation: 5993

Sounds like you could just apply an IsPostBack check in your Page_Load method. http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx

protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack) {
        // Load data
    }
}

Note: your code looks susceptible to SQL injection.

Upvotes: 5

Related Questions