无法在提交

时间:2017-12-01 18:58:45

标签: asp.net webforms

我有一个ASP.NET webform,它填充数据库中的文本框。然后用户改变表格中的值(即密码)并按下提交按钮以更新他的帐户信息。这里的问题是,当文本框中的数据发生变化时,下面的方法不会读取更改的值,而只会看到原始值。我不确定为什么Textbox_field.Text不会检索控件的当前值。

我在这里做错了什么?我尝试过使用IsPostback,但没有取得任何成功。

感谢。

protected void Button_Save_Click (object sender, EventArgs e)
{
        using (SqlConnection conn = new SqlConnection (_connectionString))
        {
            conn.Open ();

            string firstName = TextBox_FirstName.Text; 
            string lastName = TextBox_LastName.Text; 
            string email = TextBox_Email.Text;
            string login = TextBox_Username.Text;
            string password = TextBox_Password.Text;
            int userID = Convert.ToInt32 (Session ["UserID"]); 

            string sql = "update Auth_Users"                           
                       + $"    set FirstName='{firstName}', LastName='{lastName}', Email='{email}', Login='{login}', Password='{password}'"                            
                       + $" where ID={userID}"; 

            using (SqlCommand command = new SqlCommand (sql, conn))                 
            {                   
                 command.ExecuteNonQuery ();                
            } 

            conn.Close ();          
      }
}

1 个答案:

答案 0 :(得分:0)

string sql = "update Auth_Users set FirstName=@FirstNAme, LastName=@LastName"                            
                       + " where ID=@UserId"; 

using (SqlCommand command = new SqlCommand (sql, conn))                 
            { 
                 command.parameters.AddWithValue("@FirstName", FirstName) 
                 command.parameters.AddWithValue("@LastName", LastName) 
                 command.parameters.AddWithValue("@UserId", userID) 
                 command.ExecuteNonQuery ();                
            }