views:

35

answers:

3
+1  Q: 

Error in gridview

I have written my code under button as follows

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

But i am getting an error at this

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

Is there anything wrong in what i have written

I declared my item template as follows

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

Got the answer forgot to give datakeynames in gridview ...

Dorababu
+1  A: 
<asp:Label ID="lblInvoice" runat="server" Text=<%# Eval("invoceNo") %>

You are getting this error bcoz may be this line is not setting any value to label.

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

if value is null then remove(.ToString()) may be it will work

Pankaj Mishra
No no actually i called the wrong value in that instead of calling from database field i called label field so i got an error
Dorababu
A: 

This is the working one

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

and also for grid we have to assign Datakey values...

Dorababu