2012-06-30 57 views
0

我想更新gridview行,但我不断得到重复的异常错误。我正在使用C#。我正在使用3层架构。我能够插入记录,但无法更新记录。 请帮助谢谢asp净更新gridview行

<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; 
     } 
    } 

我插入payclaim方法

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(); 
     } 
    } 
+0

你得到一个错误或'statuslabel.Text =“重复”;'执行? – Louis

+0

我在运行时得到statuslabel.text = duplicateates异常。 – gogogo

回答

0

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

从你的描述我假设你得到一个异常,而不仅仅是一个大于0(res> 0)的返回码和设置一个标签重复 - 因为如果你通过显示你的状态标签的代码得到一个异常是'错误'。

请说明。

+0

我得到statuslabel.text =运行时重复异常,我更新了我的payclaim方法,当我设置断点,我可以看到通过的值,但我不知道为什么数据未更新数据库中....感谢您的帮助 – gogogo

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一样,以确保后者)。

+0

谢谢,但我遵循你的建议,但仍无法更新。我改变了int res = -2 ...或者有更好的方法从后面的代码中调用方法吗?还是我错误地声明了我的方法?谢谢 – gogogo

+0

当你调试时,res的价值是多少?我将更新代码以将参数包含在引号中,也许这就是问题所在。 – Louis