尝试从数据库 GridView 特定用户进行更新 (OleDbCommand)
Trying To Make Update From Database GridView Specific User (OleDbCommand)
大家好,我正在处理用户 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";
大家好,我正在处理用户 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()
:
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";