2014-04-18 257 views
1

enter image description here我正在嘗試更新我的數據庫表ExpenseManagement。但它沒有更新。如何更新表格

using System; 

using System.Collections.Generic; 

using System.Linq; 

using System.Web; 

using System.Web.UI; 

using System.Web.UI.WebControls; 

using System.Configuration; 

using System.Data.SqlClient; 

using System.Data; 


public partial class UserProfile : System.Web.UI.Page 
{ 

    protected void Page_Load(object sender, EventArgs e) 
    { 

      txtUserId.Text = Request.Cookies["txtUserName"].Value; 

      string con_string = @"data Source= 10.10.10.5; initial catalog= test; user= xx; password= xxxxxxxxx;"; 

      SqlConnection con = new SqlConnection(con_string); 

      SqlCommand cmd = new SqlCommand("select FirstName, LastName, Password, EmailId, MobileNumber from ExpenseManagement where UserId ='"+txtUserId.Text+"'", con); 

      cmd.Parameters.AddWithValue("@UserId", txtUserId.Text); 

      con.Open(); 

      DataTable dt = new DataTable(); 

      SqlDataAdapter da = new SqlDataAdapter(cmd); 

      da.Fill(dt); 

      txtFirstName.Text = dt.Rows[0]["FirstName"].ToString(); 

      txtLastName.Text = dt.Rows[0]["LastName"].ToString(); 

      txtPassword.Text= dt.Rows[0]["Password"].ToString(); 

      txtEmailId.Text = dt.Rows[0]["EmailId"].ToString(); 

      txtMobileNumber.Text = dt.Rows[0]["MobileNumber"].ToString(); 

      con.Close(); 

      txtUserId.Enabled = false; 

      txtFirstName.Enabled=false; 

      txtLastName.Enabled=false; 

      txtPassword.Enabled = false; 

      txtEmailId.Enabled = false; 

      txtMobileNumber.Enabled = false; 

      btnUpdate.Visible = false; 
    } 

    protected void Button1_Click1(object sender, EventArgs e) 
    { 

     txtUserId.Enabled = true; 

     txtUserId.ReadOnly = true; 

     txtFirstName.Enabled = true; 

     txtLastName.Enabled = true; 

     txtPassword.Enabled = true; 

     txtMobileNumber.Enabled = true; 

     txtEmailId.Enabled = true; 

     btnUpdate.Visible = true; 

     btnEdit.Visible = false; 
    } 

    protected void btnUpdate_Click(object sender, EventArgs e) 
    { 

     string con_string = @"data Source= 10.10.10.5; initial catalog= test; user= xx; password= xxxxxxxxx;"; 

     SqlConnection con = new SqlConnection(con_string); 

     string qryUpdate = "Update ExpenseManagement set FirstName= @FirstName, [email protected], [email protected], [email protected],[email protected] where UserId= @UserId"; 

     SqlCommand cmd = new SqlCommand(qryUpdate, con); 

     cmd.Parameters.AddWithValue("@UserId", txtUserId.Text); 

     cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text); 

     cmd.Parameters.AddWithValue("@LastName", txtLastName.Text); 

     cmd.Parameters.AddWithValue("@Password", txtPassword.Text); 

     cmd.Parameters.AddWithValue("@EmailId", txtEmailId.Text); 

     cmd.Parameters.AddWithValue("@MobileNumber", txtMobileNumber.Text); 

     con.Open(); 

     if (Page.IsValid) 
     { 

      cmd.ExecuteNonQuery(); 

      btnEdit.Visible = true; 
     } 

     con.Close(); 
    } 
} 

我有一個數據庫字段:

UserId, FirstName, LastName, Password, EmailId, MobileNumber. 
+0

得到任何錯誤? – Shell

+0

@Nimesh獲取沒有錯誤。 –

+0

使用連接字符串作爲 'Server = 10.10.10.5; User id = sa; password = multiverse @ 1; Initial Catalog = test' –

回答

2

缺少對Page_Load事件的Page.IsPostBack檢查。

在ASP.NET中,當您在服務器端控件上引發事件時,Page_Load事件總是在控件事件中的代碼之前執行。

就你而言,你的用戶改變了文本框,然後按下Update按鈕。這會引發Page_Load事件,然後是btnUpdate_Click事件。如果沒有檢查屬性IsPostBack,Page_Load事件會使用原始值重新加載數據庫中的文本框,從而有效地銷燬用戶鍵入的數據,然後調用按鈕事件代碼。但是在這一點上,文本框中的值是原始值,所以您的代碼正確運行,但不會改變任何內容。

更改Page_Load事件添加

protected void Page_Load(object sender, EventArgs e) 
{ 
     if(!IsPostBack) 
     { 
     txtUserId.Text = Request.Cookies["txtUserName"].Value; 
     string con_string = @"data Source= 10.10.10.5; initial catalog= test; user= xx; password= xxxxxxxxx;"; 
     SqlConnection con = new SqlConnection(con_string); 
     SqlCommand cmd = new SqlCommand(@"select FirstName, LastName, Password, 
              EmailId, MobileNumber 
              from ExpenseManagement 
              where UserId [email protected]", con); 
     cmd.Parameters.AddWithValue("@UserId", txtUserId.Text); 
     con.Open(); 
     DataTable dt = new DataTable(); 
     SqlDataAdapter da = new SqlDataAdapter(cmd); 
     da.Fill(dt); 
     ...... 
     btnUpdate.Visible = false; 
    } 
} 
+0

是的..現在它正在工作..非常感謝@Mr。史蒂夫 –

0

我認爲這個問題是使用cmd.Parameters.AddWithValue("@UserId", txtUserId.Text);因爲它添加的(因爲txtUserId.Text是字符串),而不是intlongstring類型的參數,所以將其更改爲cmd.Parameters.AddWithValue("@UserId", int.parse(txtUserId.Text));或使用cmd.Parameters.Add(),這需要type作爲參數。

+0

我的userId是A1001,A1002等等。它不工作。 –

+0

顯示異常 - 「輸入的字符串格式不正確」 –

+0

如果它們是字符串,那麼您從此視圖中的代碼可以執行 – RezaRahmati

1

我可以推薦這個命令是不是用「+」號,如下連接字符串更可讀的用戶@:

protected void btnUpdate_Click(object sender, EventArgs e) 
     { 
      string con_string = @"data Source= 10.10.10.5;initial catalog= test; user= xx; password= xxxxxxxxx;"; 
      SqlConnection con = new SqlConnection(con_string); 
      string qryUpdate = @"Update ExpenseManagement 
           set FirstName= @FirstName, 
            [email protected], 
            [email protected], 
            [email protected], 
            [email protected] 
            where UserId= @UserId"; 
      SqlCommand cmd = new SqlCommand(qryUpdate, con); 
      cmd.Parameters.AddWithValue("@UserId", Convert.ToInt32(txtUserId.Text)); 
      cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text); 
      cmd.Parameters.AddWithValue("@LastName", txtLastName.Text); 
      cmd.Parameters.AddWithValue("@Password", txtPassword.Text); 
      cmd.Parameters.AddWithValue("@EmailId", txtEmailId.Text); 
      cmd.Parameters.AddWithValue("@MobileNumber", txtMobileNumber.Text); 
      con.Open(); 

      if (Page.IsValid) 
      { 
       cmd.ExecuteNonQuery(); 
       btnEdit.Visible = true; 
      } 
      con.Close(); 
     } 

而且我RezaRahmati同意用戶id轉換等參數進行修正您在數據庫中定義的類型,表格列。