我正在尝试更新 gridview 行,但我不断收到重复的异常错误。我正在使用 c#。我正在使用 3 层架构。我可以插入记录,但无法更新记录。请帮忙谢谢
<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True" OnRowEditing="EditRecord" OnRowUpdating="UpdateRecord" DataKeyName = "payClaimId">
<Columns>
<asp:TemplateField HeaderText="Duty ID">
<ItemTemplate><%# DataBinder.Eval(Container.DataItem, "dutyId")%></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pay Claim ID">
<ItemTemplate><%# DataBinder.Eval(Container.DataItem, "payClaimId")%></ItemTemplate>
<EditItemTemplate><asp:TextBox ID="txtpayclaimid" runat="Server" ReadOnly ="true" Text ='<%# Eval("payClaimId") %>'></asp:TextBox></EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date Submitted">
<ItemTemplate><%# DataBinder.Eval(Container.DataItem, "printeddate", "{0:d}")%></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date Worked">
<ItemTemplate><%# DataBinder.Eval(Container.DataItem, "dateWorked", "{0:d}")%></ItemTemplate>
<EditItemTemplate><asp:TextBox ID="txtdateworked" runat="Server" Text ='<%# Eval("dateWorked", "{0:d}") %>'></asp:TextBox></EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Hours">
<ItemTemplate><%# DataBinder.Eval(Container.DataItem, "totalHours")%></ItemTemplate>
<EditItemTemplate><asp:TextBox ID="txttotalhours" runat="Server" Text ='<%# Eval("totalHours") %>'></asp:TextBox></EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate><%# DataBinder.Eval(Container.DataItem, "status")%></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
private void loaddata()
{
DataTable dt = new DataTable();
Timesheet ts = new Timesheet();
dt = ts.gettimesheetrecords(Convert.ToInt32(txtboxeid.Text));
GridView3.DataSource = dt;
GridView3.DataBind();
}
protected void EditRecord(object sender, GridViewEditEventArgs e)
{
GridView3.EditIndex = e.NewEditIndex;
loaddata();
}
protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView3.Rows[e.RowIndex];
TextBox pcid = (TextBox)row.FindControl("txtpayclaimid");
TextBox dworked = (TextBox)row.FindControl("txtdateworked");
TextBox tHours = (TextBox)row.FindControl("txttotalhours");
int payClaimId = int.Parse(pcid.Text);
string dateWorked = Convert.ToString(dworked.Text);
double totalHours = double.Parse(tHours.Text);
int res = 0;
PayClaim pc = new PayClaim();
try
{
res = pc.updatepayclaim(payClaimId, dateWorked, totalHours);
if (res > 0)
{
statuslabel.Text = "Record saved";
GridView3.EditIndex = -1;
loaddata();
}
else
{
statuslabel.Text = "Duplicates";
}
}
catch (Exception ex)
{
statuslabel.Text = "Error";
}
finally
{
pc = null;
}
}
我的插入赔付方法
public int updatepayclaim(int payClaimId, string dateWorked, double totalHours)
{
dbCon = new OleDbConnection(sConnection);
dbCon.Open();
OleDbCommand com = dbCon.CreateCommand();
com.CommandText = "Update PayClaim Set totalHours=?, dateWorked=? where payClaimId=?";
try
{
com.Parameters.AddWithValue("payClaimId", payClaimId);
com.Parameters.AddWithValue("dateWorked", dateWorked);
com.Parameters.AddWithValue("totalHours", totalHours);
return com.ExecuteNonQuery();
}
catch
{
throw;
}
finally
{
com.Dispose();
dbCon.Close();
dbCon.Dispose();
}
}