2010-08-14 29 views
1

我写我的代码下按钮,如下错误的GridView

string actkey = string.Empty; 
    foreach (GridViewRow row in GridView1.Rows) 
    { 
     //Label lbInvoice = (Label)row.FindControl("lblInvoice"); 
     string strInvoice = GridView1.DataKeys[row.RowIndex].Values["lblInvoice"].ToString(); 
     objinvoice.Invoice = strInvoice; 
     RadioButton rdn = (RadioButton)row.FindControl("rdnRenew"); 
     if (rdn.Checked) 
     { 
      actkey = oCustomerDetails.CreateRandom(20); 
      objinvoice.activationcode = actkey; 
      objinvoice.editInvoice(actkey, strInvoice); 
     } 
    } 

但我在这个

string strInvoice = GridView1.DataKeys[row.RowIndex].Values["lblInvoice"].ToString(); 

得到一个错误,这有什么错在我所写的

我宣布我的项目模板如下

 <ItemTemplate> 
         <asp:Label ID="lblInvoice" runat="server" Text='<%# Eval("invoceNo") %>'></asp:Label> 
        </ItemTemplate> 
+0

你遇到了什么错误? – 2010-08-14 08:33:05

+0

处理了空引用异常 未将对象引用设置为对象 – Dotnet 2010-08-14 08:37:31

+0

因此,集合中没有键“lblInvoice”的元素。 – 2010-08-14 08:38:55

回答

0

这是一个工作

string strInvoice = GridView1.DataKeys[row.RowIndex].Values["invoceNo"].ToString(); 

,也为网格,我们必须分配Datakey值...

0

得到的答案忘记 GridView

2
<asp:Label ID="lblInvoice" runat="server" Text=<%# Eval("invoceNo") %> 

您正在收到此错误,因为可能是此行未设置任何值为label

string strInvoice = GridView1.DataKeys[row.RowIndex].Values["lblInvoice"].ToString(); 

如果值为null,则remove(.ToString())可能会工作

+0

不,实际上我调用了错误的值,而不是从数据库字段调用我称为标签字段我有一个错误 – Dotnet 2010-08-14 12:21:21