0

プロファイルを更新しようとしていますが、更新時に古いデータを取得し続けます。この問題を解決するために何ができるか。この問題で私を助けてくださいありがとう!

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.";
    }
}
4

1 に答える 1

5

IsPostBackメソッドにチェックを適用するだけでよいようですPage_Loadhttp://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx

protected void Page_Load(object sender, EventArgs e)
{

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

注: コードは SQL インジェクションの影響を受けやすいようです。

于 2013-08-15T02:17:03.377 に答える