gpt4 book ai didi

c# - 尝试从数据库 GridView 特定用户进行更新 (OleDbCommand)

转载 作者:搜寻专家 更新时间:2023-10-30 20:47:42 24 4
gpt4 key购买 nike

大家好,我正在处理用户 GridView 数据库更新,我的更新行有问题,它告诉我当我点击更新时,该行更新成功但它并没有真正更新它:(

代码:

1 ASP.net(OleDB)

 protected void AdminBook_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
using (OleDbConnection sqlCon = new OleDbConnection(connectionStr))
{
sqlCon.Open();
string query = "UPDATE Users SET FirstName=@FirstName,LastName=@LastName,UserPass=@UserPass,Gender=@Gender,Birthday=@Birthday,Email=@Email WHERE UserID=@id";
OleDbCommand sqlCmd = new OleDbCommand(query, sqlCon);
sqlCmd.Parameters.AddWithValue("@FirstName", (AdminBook.Rows[e.RowIndex].FindControl("txtFirstName") as TextBox).Text.Trim());
sqlCmd.Parameters.AddWithValue("@LastName", (AdminBook.Rows[e.RowIndex].FindControl("txtLastName") as TextBox).Text.Trim());
sqlCmd.Parameters.AddWithValue("@UserPass", (AdminBook.Rows[e.RowIndex].FindControl("txtUserPass") as TextBox).Text.Trim());
sqlCmd.Parameters.AddWithValue("@Gender", (AdminBook.Rows[e.RowIndex].FindControl("txtGender") as TextBox).Text.Trim());
sqlCmd.Parameters.AddWithValue("@Birthday", (AdminBook.Rows[e.RowIndex].FindControl("txtBirthday") as TextBox).Text.Trim());
sqlCmd.Parameters.AddWithValue("@Email", (AdminBook.Rows[e.RowIndex].FindControl("txtEmail") as TextBox).Text.Trim());
sqlCmd.Parameters.AddWithValue("@UserName", (AdminBook.Rows[e.RowIndex].FindControl("txtUserName") as TextBox).Text.Trim());
sqlCmd.Parameters.AddWithValue("@id", Convert.ToInt32(AdminBook.DataKeys[e.RowIndex].Value.ToString()));
sqlCmd.ExecuteNonQuery();
AdminBook.EditIndex = -1;
PopulateGridView();
lblSuccessMessage.Text = "עריכת הנתונים התבצעה בהצלחה";
lblErrorMessage.Text = "";
}
}
catch (Exception ex)
{
lblSuccessMessage.Text = "";
lblErrorMessage.Text = ex.Message;
}
}

2 HTML(非常短)

    <asp:GridView ID="AdminBook" runat="server" AutoGenerateColumns="False" 
ShowFooter="True" DataKeyNames="UserID"
ShowHeaderWhenEmpty="True"

OnRowCommand="AdminBook_RowCommand" OnRowEditing="AdminBook_RowEditing" OnRowCancelingEdit="AdminBook_RowCancelingEdit"
OnRowUpdating="AdminBook_RowUpdating" OnRowDeleting="AdminBook_RowDeleting"

BackColor="White" BorderColor="#999999" BorderStyle="Solid"
BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical">

你能看到的图片: Here you can see that it show the success message

请帮助我...因为它使它成功但没有真正在数据库中更新 :(

最佳答案

您查询的主要问题是此参数定义的存在:

sqlCmd.Parameters.AddWithValue("@UserName", (AdminBook.Rows[e.RowIndex].FindControl("txtUserName") as TextBox).Text.Trim());

查询字符串中不存在,它有 7 个参数,而不是 cmd.Parameters.Add() 定义的 8 个参数:

UPDATE Users SET FirstName=@FirstName,LastName=@LastName,UserPass=@UserPass,Gender=@Gender,Birthday=@Birthday,Email=@Email WHERE UserID=@id

注意 OLE DB 不识别命名参数,它只识别 positional parameters (查询参数按其定义顺序处理)。对于代码中的当前顺序,UserName 参数可能被错误地分配为 id,并且由于提供的值与存储在 UserID 中的任何值都不匹配列,则不会更新任何数据。

因此,您应该删除上面提到的行,以便查询参数与它们在查询字符串中的顺序完全匹配:

string query = "UPDATE Users SET FirstName=@FirstName,LastName=@LastName,UserPass=@UserPass,Gender=@Gender,Birthday=@Birthday,Email=@Email WHERE UserID=@id";

OleDbCommand sqlCmd = new OleDbCommand(query, sqlCon);

sqlCmd.Parameters.AddWithValue("@FirstName", (AdminBook.Rows[e.RowIndex].FindControl("txtFirstName") as TextBox).Text.Trim());
sqlCmd.Parameters.AddWithValue("@LastName", (AdminBook.Rows[e.RowIndex].FindControl("txtLastName") as TextBox).Text.Trim());
sqlCmd.Parameters.AddWithValue("@UserPass", (AdminBook.Rows[e.RowIndex].FindControl("txtUserPass") as TextBox).Text.Trim());
sqlCmd.Parameters.AddWithValue("@Gender", (AdminBook.Rows[e.RowIndex].FindControl("txtGender") as TextBox).Text.Trim());
sqlCmd.Parameters.AddWithValue("@Birthday", (AdminBook.Rows[e.RowIndex].FindControl("txtBirthday") as TextBox).Text.Trim());
sqlCmd.Parameters.AddWithValue("@Email", (AdminBook.Rows[e.RowIndex].FindControl("txtEmail") as TextBox).Text.Trim());
sqlCmd.Parameters.AddWithValue("@id", Convert.ToInt32(AdminBook.DataKeys[e.RowIndex].Value.ToString()));

或者在WHERE 子句之前添加UserName 列而不改变参数顺序:

string query = "UPDATE Users SET FirstName=@FirstName,LastName=@LastName,UserPass=@UserPass,Gender=@Gender,Birthday=@Birthday,Email=@Email,
UserName=@UserName WHERE UserID=@id";

关于c# - 尝试从数据库 GridView 特定用户进行更新 (OleDbCommand),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53577531/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com