C# 如何更新表
我正在尝试更新我的数据库表ExpenseManagement。但它没有更新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
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中运行的语句是什么?