选择文本框并更新

时间:2011-06-25 10:29:34

标签: c# asp.net mysql

我无法更新ASP.NET文本框中的选定文本。 我想使用文本框编辑MySql数据库记录。所以,首先我选择这些值并将它们添加到文本框中。在此之后,我试图更新我检索到我的值的同一文本框。我无法更新这些已编辑的选定值。

我在单独执行“选择和更新”时验证了我的代码可以单独运行。但我不能同时使用两者。

 protected void Page_Load(object sender, EventArgs e) try
    {

        string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" + "DATABASE=malla_softmail;" + "UID=xxx;" + "PASSWORD=xxx;" + "OPTION=3";
        OdbcConnection MyConnection = new OdbcConnection(MyConString);
        try
        {
            MyConnection.Open();

            OdbcCommand cmd = new OdbcCommand("Select nickname, fullname, sex, age, city, country, company, zipcode, school, college, profession, maritial, local_search, public_search from awm_profiles where email=?", MyConnection);
            cmd.Parameters.Add("@email", OdbcType.VarChar, 255).Value = a;

            OdbcDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows == false)
            {
                throw new Exception();
            }

            if (dr.Read())
            {
                tb_nickname.Text = dr[0].ToString();
                tb_fullname.Text = dr[1].ToString();
                dd_sex.SelectedValue= dr[2].ToString();
                dd_age.SelectedValue= dr[3].ToString();
                tb_city.Text = dr[4].ToString();
                tb_country.Text = dr[5].ToString();
                tb_company.Text = dr[6].ToString();
                tb_zipcode.Text = dr[7].ToString();
                tb_school.Text = dr[8].ToString();
                tb_college.Text = dr[9].ToString();
                tb_profession.Text = dr[10].ToString();
                dd_maritial.SelectedValue = dr[11].ToString();
                String local = dr[12].ToString();
                String web = dr[13].ToString();
                if(Convert.ToInt16(local) == 1)
                {
                lsearch.Checked = true;
                }
                else
                {
                lsearch.Checked = false;
                }
                if(Convert.ToInt16(web) == 1)
                {
                wsearch.Checked = true;
                }
                else
                {
                wsearch.Checked = false;
                }
            MyConnection.Close();
            }
        }
        catch
        {

        }
    }
    catch
    {

    }
}
protected void btn_profile_submit_Click(object sender, EventArgs e)
{

        a = Convert.ToString(Session["emailx10transfer"]);
        lbl_email.Text = a;
        tb_nickname.Text = aa;
        tb_fullname.Text = ab;
        dd_sex.SelectedValue = ac;
        dd_age.SelectedValue = ad;
        tb_city.Text = ae;
        tb_country.Text = af;
        tb_company.Text = ag;
        tb_zipcode.Text = ah;
        tb_school.Text = ai;
        tb_college.Text = aj;
        tb_profession.Text = ak;
        dd_maritial.Text = al;
       // lsearch.Checked = am;
       // wsearch.Checked = an;



   try
    {
        string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" + "DATABASE=malla_softmail;" + "UID=xxx;" + "PASSWORD=xxx;" + "OPTION=3";
        OdbcConnection MyConnection = new OdbcConnection(MyConString);
       MyConnection.Open();
        OdbcCommand cmd = new OdbcCommand("UPDATE awm_profiles SET nickname=?, fullname=?, sex=?, age=?, city=?, country=?, company=?, zipcode=?, school=?, college=?, profession=?, maritial=? WHERE email=? ", MyConnection);
        cmd.Parameters.Add("@tb_nickname", OdbcType.VarChar, 255).Value = tb_nickname.Text.Trim();
        cmd.Parameters.Add("@tb_fullname", OdbcType.VarChar, 255).Value = tb_fullname.Text.Trim();
        cmd.Parameters.Add("@dd_sex", OdbcType.VarChar, 255).Value = tb_fullname.Text.Trim();//dd_sex.SelectedValue.Trim();
        cmd.Parameters.Add("@dd_age", OdbcType.VarChar, 255).Value = tb_fullname.Text.Trim();//dd_age.SelectedValue.Trim();
        cmd.Parameters.Add("@tb_city", OdbcType.VarChar, 255).Value = tb_city.Text.Trim();
        cmd.Parameters.Add("@tb_country", OdbcType.VarChar, 255).Value = tb_country.Text.Trim();
        cmd.Parameters.Add("@tb_company", OdbcType.VarChar, 255).Value = tb_company.Text.Trim();
        cmd.Parameters.Add("@tb_zipcode", OdbcType.VarChar, 255).Value = tb_zipcode.Text.Trim();
        cmd.Parameters.Add("@tb_school", OdbcType.VarChar, 255).Value = tb_school.Text.Trim();
        cmd.Parameters.Add("@tb_college", OdbcType.VarChar, 255).Value = tb_college.Text.Trim();
        cmd.Parameters.Add("@tb_profession", OdbcType.VarChar, 255).Value = tb_profession.Text.Trim();
        cmd.Parameters.Add("@dd_maritial", OdbcType.VarChar, 255).Value = tb_fullname.Text.Trim();//dd_maritial.SelectedValue.Trim();

        //if (lsearch.Checked == true) { cmd.Parameters.Add("@localsearch", OdbcType.Int, 11).Value = "1"; }
        //else { cmd.Parameters.Add("@localsearch", OdbcType.Int, 11).Value = "0"; }
        //if (wsearch.Checked == true) { cmd.Parameters.Add("@wwwsearch", OdbcType.Int, 11).Value = "1"; }
        //else { cmd.Parameters.Add("@wwwsearch", OdbcType.Int, 11).Value = "0"; }
        cmd.Parameters.Add("@email", OdbcType.VarChar, 255).Value = a;

        cmd.ExecuteNonQuery();
        MyConnection.Close();
        Response.Redirect("profile.aspx");
    }
    catch (Exception Ex)
    {
        Response.Write(Ex);
    }   
}

2 个答案:

答案 0 :(得分:3)

如果要从Page_Load中的DB加载文本,然后尝试保存在Postback上。如果是这样,你是否在Page_Load中检查PostBack。如果没有,您可能每次单击按钮

时都会覆盖您的更改
if (!IsPostBack)
{
    LoadValuesFromDBIntoTextBox();
}

http://msdn.microsoft.com/en-us/library/ms178472.aspx

答案 1 :(得分:-1)

使用if(!IsPostBack)函数...