1

autopostback プロパティが true のテキスト ボックスと保存用のボタンがあります。

テキストが変更されると、データベースからデータを取得し、他のテキスト ボックスに表示します。テキスト ボックスを編集して保存すると、textchanged イベントが発生し、データが更新されません。代わりに、以前のデータのみを保存します。編集したデータを保存したい。

誰でも私がこれを行うのを手伝ってもらえますか?

ボタンクリックイベントは次のとおりです。

protected void btnSave_Click(object sender, EventArgs e)
{
    //TextBox1.Text = exists;
    if(exists=="yes")
    {
        OdbcConnection DbConnection2 = new OdbcConnection(con1);
        OdbcCommand DbCommand2 = DbConnection2.CreateCommand();
        try
        {
            DbConnection2.Open();
            DbCommand2.CommandText = "update tbl_trip_login set username='" +                    txtUser.Text.ToUpper() + "',password='" + txtPwd.Text + "',name='" + txtname.Text + "',department='" + txtDept.Text + "',designation='" + txtDesg.Text + "',DOJ='" + txtDOJ.Text + "',DOB='" + txtDOJ.Text + "',phone='" + txtPh.Text + "',Email='" + txtEmail.Text + "',Gender='" + rdbGender.SelectedItem.Text + "',Active='" + rdbActive.SelectedItem.Text + "' where upper(username)=upper('" + txtUser.Text + "')";
            TextBox1.Text = DbCommand2.CommandText.ToString();
            int t2 = DbCommand2.ExecuteNonQuery();
            if (t2 == 1)
            {

                lblError.Visible = true;
                lblError.Text = "Successfully Saved";
            }
            else
            {
                lblError.Text = "Not Saved.. Try again later";
                lblError.Visible = true;
            }
            DbConnection2.Close();
        }
        catch (Exception e1)
        {
            lblError.Text = e1.Message;
            lblError.Visible = true;
            DbConnection2.Close();
        }
    }
    else //if user doesn't exists,create new user
    {
        DateTime dd = DateTime.Now; //current date and time
        string custom = dd.ToString("dd-MMM-yyyy,hh:mm,tt"); // to convert in the form : 14-APR-2013 01:35 PM
        string ss = "to_date('" + custom + "','dd-mon-yyyy HH:MI AM')"; // oracle does not take only time by default, so use to_date(), and pass this string variable directly in the query

        OdbcConnection DbConnection1 = new OdbcConnection(con1);
        OdbcCommand DbCommand1 = DbConnection1.CreateCommand();
        try
        {
            DbConnection1.Open();
            DbCommand1.CommandText = "insert  into tbl_trip_login(username,password,name,department,designation,DOJ,DOB,phone,Email,Gender,Active,Entered_by,Entered_time)values('" + txtUser.Text + "','" + txtPwd.Text + "','" + txtname.Text + "','" + txtDept.Text + "','" + txtDesg.Text + "','" + txtDOJ.Text + "','" + txtDOB.Text + "','" + txtPh.Text + "','" + txtEmail.Text + "','" + rdbGender.SelectedItem.Text + "','" + rdbActive.SelectedItem.Text + "','" + Session["UserAuthentication"].ToString() + "','" + ss + "')";
            TextBox1.Text = DbCommand1.CommandText.ToString();
            int t1 = DbCommand1.ExecuteNonQuery();
            if (t1 == 1)
            {

                lblError.Visible = true;
                lblError.Text = "Successfully Saved";
            }
            else
            {
                lblError.Text = "Not Saved.. Try again later";
                lblError.Visible = true;
            }
            DbConnection1.Close();
        }
        catch (Exception e1)
        {
            lblError.Text = e1.Message;
            lblError.Visible = true;
            DbConnection1.Close();
        }
        txtUser.Focus();
        txtUser.Text = "";
        txtPwd.Text = "green";
    }
}

そして Text Changed イベント:

protected void txtUser_TextChanged(object sender, EventArgs e)
{
    var targetID = Request.Form["__EVENTTARGET"];
    if (targetID != null && targetID != string.Empty)
    {
        btnSave.Enabled = true;
        exists = "no";
        try
        {
            OdbcConnection myOdbcConnection = new OdbcConnection(con1);
            OdbcCommand myOdbcCommand = myOdbcConnection.CreateCommand();

            string sSQL = "select username,password,name,department,designation,doj,dob,phone,email,gender,active from tbl_trip_login where upper(username)=upper('" + txtUser.Text + "')";
            myOdbcCommand.CommandText = sSQL;

            myOdbcConnection.Open();
            OdbcDataReader myOdbcDataReader = myOdbcCommand.ExecuteReader();
            if (myOdbcDataReader.HasRows)
            {
                exists = "yes";
                txtPwd.Text = myOdbcDataReader[1].ToString();
                txtname.Text = myOdbcDataReader[2].ToString();
                txtDept.Text = myOdbcDataReader[3].ToString();
                txtDesg.Text = myOdbcDataReader[4].ToString();
                txtDOJ.Text = myOdbcDataReader[5].ToString();
                txtDOB.Text = myOdbcDataReader[6].ToString();
                txtPh.Text = myOdbcDataReader[7].ToString();
                txtEmail.Text = myOdbcDataReader[8].ToString();
                rdbGender.Text = myOdbcDataReader[9].ToString();
                rdbActive.Text = myOdbcDataReader[10].ToString();

            }
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
            lblError.Visible = true;
        }
    }
}
4

0 に答える 0