Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
i am trying to set label text which resides in gridview on RowDataBound event of gridview.The code for the same is as follows:


C#
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();
            }
        }

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 per as i understand,the new label is not being created.then how to assign values calculated in this event to gridviews label?Any help is greatly appriciated...
Regards
Posted

This code does not create a label, it just tries to find it by id, as you probably understand. The search fails. You need to check the result for null before dereferencing it by using any of its instance member, in this case, Text. Your code does not show why you don't find your label where you expect it.

Most likely, this is ASP.NET (always tag your application type or UI library you use in your question, when it comes to UI). Do a simple thing. Make this code runnable (comment out questionable parts), look at the page from the client side and look at your pure HTML page from the browser (View Page Source or something like that). Look at the source and find out what's wrong with the id or whatever else you have generated.

—SA
 
Share this answer
 
Object reference not set to an instance of an object

This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.


Based on what you have shared above, most probably lab is NULL. Make sure there is a control named 'd' in the row.
Also, it may be this error is for grid header where no label 'd' control is present.
If so, try:
C#
DataControlRowType rtype = e.Row.RowType;  
if (rtype == DataControlRowType.DataRow && rtype != DataControlRowType.Footer && rtype != DataControlRowType.Header)  
{
 //
} 

A simple use of breakpoint in the method in DEBUG mode will clear all the doubts and possible fix.
 
Share this answer
 
Comments
Sandeep Mewara 6-Mar-13 8:03am    
I see1 vote and a comment which is not there anymore!

Did I miss anything else?
Thanks7872 7-Mar-13 4:50am    
yes...Sandeep actualy i found the solution so i removed the comment...thanks for your support...
Sandeep Mewara 7-Mar-13 5:17am    
Ok. Please update the question with resolution if any of the answers did not help.
Thanks7872 7-Mar-13 23:03pm    
there was a problem with gridview's markup not with the code,so its not possible to show the resolution here as the above code works fine with the updated markup....
Sandeep Mewara 8-Mar-13 0:04am    
Ok.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900