Reputation: 53
i am trying to update the gridview row but i keep getting the duplicate exception error. i am using c#. i am using 3-tier architecture. i am able to insert records, but unable to update record. please help thanks
<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;
}
}
my insert payclaim method
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();
}
}
Upvotes: 0
Views: 1532
Reputation: 3672
I don't think that's the correct syntax. I would try something like this:
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();
}
I would also be curious to know the value of res
, if it is -1 for error or 0 for no rows updated (you would have initialize it to a non-zero, like -2, to be sure about the latter).
Upvotes: 0
Reputation: 44
I think in order to answer your question you will need to show the code for the PayClaim updatepayclaim method.
From your description I assume you are getting an Exception and not just a return code greater than 0 (res > 0) and setting of a label to Duplicates - because if you get an exception by the code shown your status label would be 'Error.'
Please clarify.
Upvotes: 0