我有一个包含员工详细信息的gridview。我有一个“编辑”链接按钮。单击后,它将更新记录。但是,在这段代码中,不是更新,而是创建新记录。
但是,不是更新,而是添加了新记录。
protected void LinkButtonEdit_Click(object sender, EventArgs e)
{
using (GridViewRow row = (GridViewRow)((LinkButton)sender).Parent.Parent)
{
textBoxNameInput.Text = row.Cells[0].Text;
HiddenEmployeeName.Value= row.Cells[0].Text;
textBoxSalaryInput.Text = row.Cells[1].Text;
HiddenSalary.Value = row.Cells[1].Text;
textBoxDOJInput.Text = row.Cells[2].Text;
HiddenDOJ.Value = row.Cells[2].Text;
var textToFind = row.Cells[3].Text;
DropDownListDepartment.Items.FindByText(textToFind).Selected = true;
DropDownListDepartment.ClearSelection();
HiddenDepartmentName.Value = textToFind;
}
ModalPopupExtender1.Show();
}
protected void Update_Btn_Click(object sender, EventArgs e)
{
var deptID = getDeptId(HiddenDepartmentName.Value);
float Salary = float.Parse(HiddenSalary.Value);
// string DateOfJoining = HiddenDOJ.Value;
// DateTime dateVal = Convert.ToDateTime(DateOfJoining,
//System.Globalization.CultureInfo.GetCultureInfo("hi-IN").DateTimeFormat);
int EmployeeID = getEmployeeID(HiddenEmployeeName.Value, Salary, deptID);
using (SqlConnection con = new SqlConnection(conStr))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
string EmployeeName = textBoxNameInput.Text;
float SalaryUpadate = float.Parse(textBoxSalaryInput.Text);
string DepartmentName = DropDownListDepartment.Text;
int DepartmentID = getDeptId(DepartmentName);
// DateTime DateOfJoining = Convert.ToDateTime(DOJ_Text.Text);
// string DateOfJoiningUpdate = EditDOJ_Text.Text;
// DateTime dateValUpdate = Convert.ToDateTime(DateOfJoiningUpdate,
// System.Globalization.CultureInfo.GetCultureInfo("hi-IN").DateTimeFormat);
con.Open();
cmd.CommandText = "UPDATE EMPLOYEE SET EmployeeName = @EmployeeName, SALARY = @Salary, DEPARTMENTID = @DepartmentID WHERE EMPLOYEEID = @EmployeeID";
cmd.Parameters.AddWithValue("@EmployeeName", EmployeeName);
cmd.Parameters.AddWithValue("@DepartmentId", DepartmentID);
cmd.ExecuteNonQuery();
}
}
public int getEmployeeID(string updateEmpolyeeName, float updateSalary, int deptID)
{
int EmpId = 0;
using (SqlConnection con = new SqlConnection(conStr))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
con.Open();
SqlDataReader rd;
cmd.CommandText = "Select EmployeeId from Employee where EmployeeName='" + updateEmpolyeeName + "'and salary='" + updateSalary + "'and departmentId='" + deptID + "'";
rd = cmd.ExecuteReader();
while (rd.Read())
{
EmpId = Int32.Parse(rd.GetValue(0).ToString());
}
}
return EmpId;
}
答案 0 :(得分:0)
我注意到的一件事是,这里没有@EmployeeID的参数
con.Open();
cmd.CommandText = "UPDATE EMPLOYEE SET EmployeeName = @EmployeeName, SALARY = @Salary, DEPARTMENTID = @DepartmentID WHERE EMPLOYEEID = @EmployeeID";
cmd.Parameters.AddWithValue("@EmployeeName", EmployeeName);
cmd.Parameters.AddWithValue("@DepartmentId", DepartmentID);
cmd.ExecuteNonQuery();
我要指出的另一件事是,在您的函数getEmployeeID()中...根据名称,工资,部门获得的EmployeeID ...无法保证这些字段将是唯一的。 将实际的EmployeeID放入网格中,并将其用作键。
为什么要插入记录而不是更新? 我看不到Update_Btn_Click()会插入记录的任何可能方式。 我将在此处放置一个断点,并确保正在执行的代码。
您可以这样隐藏EmployeeID:
<asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" InsertVisible="False" ReadOnly="True">
<HeaderStyle CssClass="hide" Width="0px" HorizontalAlign="Center" />
<ItemStyle CssClass="hide" Width="0px" HorizontalAlign="Center" />
</asp:BoundField>