-1

I am trying to set label text which resides in gridview on RowDataBound event of gridview.The code for the same is as follows:

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        int day = Convert.ToInt32(DropDownList3.SelectedValue);
        int days = System.DateTime.DaysInMonth(2013,day);
        Label lab = (Label)e.Row.FindControl("d");
        lab.Text = days.ToString();
    }
}

GRIDVIEW STRUCTURE:

<asp:GridView ID="GridView2" runat="server" OnRowDataBound="GridView2_RowDataBound" />
                <asp:BoundField DataField="name" HeaderText="Name" SortExpression="n" />
                <asp:BoundField DataField="dept" HeaderText="Department" SortExpression="d" />
                <asp:BoundField DataField="code" HeaderText="Employee Code" SortExpression="c" />
                <asp:TemplateField HeaderText="Total days" >
                <itemtemplate>
                    <asp:Label ID="d" runat="server" Text="" />
                </itemtemplate>
                </asp:TemplateField>
                </asp:GridView>

In the above code,'d' is the id of the label in the gridview.On the highlighted line i am getting error that OBJECT REFERENCE NOT SET TO INSTANCE OF AN OBJECT.As far as I understand, the new label is not being created.Then how to assign values calculated in this event to gridview's label?

  • 6
    Debug your code and check what you get with e.Row.FindControl("d") I guess that this is null. – Tomtom Mar 06 '13 at 06:44
  • 1
    Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Mar 06 '13 at 06:50
  • i know that it is null,but i dont know the way to solve it...can you please suggest me. –  Mar 06 '13 at 06:56
  • The documentation explains that the [`FindControl` method](http://msdn.microsoft.com/en-us/library/486wc64h.aspx) will return `null` if the specified control does not exist. – Jeppe Stig Nielsen Mar 06 '13 at 06:58
  • show the gridview markup – tariq Mar 06 '13 at 06:59
  • Please update your post and do not put the code in a comment – Tomtom Mar 06 '13 at 07:07
  • i just want that calculated value of 'days' should be displayed in gridview,thats it..for that i am trying to assign days to label and that label to 'd' that is label in gridview... –  Mar 06 '13 at 07:08
  • @tomtom...sorry...i am newbie,will take care now onwards.....please have a look at updated question. –  Mar 06 '13 at 07:09

1 Answers1

1
 <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView2_RowDataBound">
        <Columns>
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="n" />
            <asp:BoundField DataField="ID" HeaderText="Department" />
            <asp:BoundField DataField="Seats" HeaderText="Employee Code" />
            <asp:TemplateField HeaderText="Total days">
                <ItemTemplate>
                    <asp:Label ID="d" runat="server" Text=""></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

where is your columns tag, put in the columns tag and try

tariq
  • 2,193
  • 15
  • 26
  • still not working.Issue is as it is....can you suggest me any other way round to display days values in the gridview...? –  Mar 06 '13 at 07:17
  • i marked it as answer,but not able to upvote as i am new to stackoverflow.It requires 15 reputation for that.thanks once again. –  Mar 06 '13 at 07:31