Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何更新表_C#_Asp.net_Sql Server - Fatal编程技术网

C# 如何更新表

C# 如何更新表,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我正在尝试更新我的数据库表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 Sy

我正在尝试更新我的数据库表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, 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.

我认为问题在于使用
cmd.Parameters.AddWithValue(“@UserId”,txtserid.Text)
因为它添加了类型为
string
(因为txtserid.Text是string)的参数,而不是
int
long
,所以将其更改为
cmd.Parameters.AddWithValue(@UserId),int.parse(txtserid.Text))
或使用
cmd.Parameters.Add()
,它以
type
作为参数。

我建议用户@更易于阅读该命令,而不是像下面那样将字符串与“+”符号连接起来:

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将用户ID和其他参数转换为您在数据库、表列中定义的正确类型。

缺少页面加载事件的检查

在ASP.NET中,当在服务器端控件上引发事件时,页面加载事件始终在控件事件中的代码之前执行

在您的情况下,用户更改文本框,然后按下更新按钮。这将引发
Page\u Load
事件,然后引发
btnUpdate\u Click
事件。如果不检查属性
IsPostBack
,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;
   }
}

@Nimesh没有收到错误。使用连接字符串作为
Server=10.10.10.5;用户id=sa;密码=multiverse@1;初始目录=测试
数据库中是否存在“UserId=@UserId”?您在@userid中传递的内容?检查
txtoserid。文本
不应在值之后或之前包含空格。将
断点
放置在
if(Page.IsValid)
处,并检查控件是否在其中。我的userid是A1001、A1002,依此类推。。它不起作用。显示异常-“输入字符串格式不正确”您能否运行
sql探查器
并告诉我们sql server中运行的语句是什么?