0

我正在尝试更新 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();
        }
    }
4

2 回答 2

0

我认为为了回答您的问题,您需要显示 PayClaim updatepayclaim 方法的代码。

根据您的描述,我假设您收到异常,而不仅仅是大于 0(res > 0)的返回码并将标签设置为 Duplicates - 因为如果您通过显示的代码获得异常,您的状态标签将是“错误”。 '

请说清楚。

于 2012-06-30T12:41:34.817 回答
0

我不认为这是正确的语法。我会尝试这样的事情:

com.CommandText = "Update PayClaim Set totalHours='@pTotalHours', dateWorked='@pDateWorked' where payClaimId='@pPayClaimId'";
try
{
    com.Parameters.AddWithValue("@pPayClaimId", payClaimId);
    com.Parameters.AddWithValue("@pDateWorked", dateWorked);
    com.Parameters.AddWithValue("@pTotalHours", totalHours);
    return com.ExecuteNonQuery();
}

我也很想知道 的值res,如果它是 -1 表示错误或 0 表示没有更新行(你可以将它初始化为非零,如 -2,以确保后者)。

于 2012-07-01T23:04:19.167 回答