2014-04-18 126 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转换等参数进行修正您在数据库中定义的类型,表格列。