如何更新表格

时间:2014-04-18 07:10:27

标签: c# asp.net sql-server

enter image description here我正在尝试更新我的数据库表费用管理。但它没有更新。

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, LastName=@LastName, Password=@Password, EmailId=@EmailId,MobileNumber=@MobileNumber 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.

3 个答案:

答案 0 :(得分:2)

缺少对Page_Load事件的Page.IsPostBack检查。

在ASP.NET中,当您在服务器端控件上引发事件时,Page_Load事件始终在控件事件中的代码之前执行。

在您的情况下,您的用户更改文本框,然后按“更新”按钮。这会引发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 =@usedId", con);
        cmd.Parameters.AddWithValue("@UserId", txtUserId.Text);
        con.Open();
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        ......
        btnUpdate.Visible = false;
   }
}

答案 1 :(得分: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, 
                                    LastName=@LastName, 
                                    Password=@Password,
                                    EmailId=@EmailId,
                                    MobileNumber=@MobileNumber 
                                    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将userId和其他参数转换为您在数据库中定义的正确类型,表格列。

答案 2 :(得分:0)

我认为问题是使用cmd.Parameters.AddWithValue("@UserId", txtUserId.Text);,因为它添加了string类型的参数(因为txtUserId.Text是字符串)而不是intlong,所以更改它以cmd.Parameters.AddWithValue("@UserId", int.parse(txtUserId.Text));或使用cmd.Parameters.Add()type作为参数。