System.NullReferenceException error ASP C#

G

Ga?tan Par?

I have this error when I try to launch my Asp.Net page :

System.NullReferenceException: Object reference not set to an instance
of an object. at calformation.CalForm.Calendar1_DayRender(Object
sender, DayRenderEventArgs e)

Source COde :
protected void Calendar1_DayRender(object sender, DayRenderEventArgs
e)
{
CalendarDay d = ((DayRenderEventArgs)e).Day;
TableCell c = ((DayRenderEventArgs)e).Cell;
DateTime d2;
string SDate;


if (d.IsOtherMonth)
{
c.Controls.Clear();
}
else
{
try
{

HERE IS THE ERROR while (MyReader.Read())
{
SDate = MyReader["Date_F"].ToString();
d2 = DateTime.Parse(SDate);
if (DateTime.Compare(d.Date,d2)== 0)
{
c.Controls.Add( new LiteralControl("<br>" +
MyReader["Sujet"].ToString()));
}

}


}
catch ( Exception exc)
{
Response.Write (exc.ToString());
}
}
}

End of Code

The Object MyReader is a OleDbDataReader initialised in Page_Load

Can you help me ?
 
G

Guest

Hi, first of all your code is a bit fuzzy. Why you cast the object e to the type which it is instance of? I talk about the lines like those:
CalendarDay d = ((DayRenderEventArgs)e).Day
TableCell c = ((DayRenderEventArgs)e).Cell
You can simply write
CalendarDay d = e.Day
TableCell c = e.Cell
or event better, just use them streight from the object e,it will be much more memory efficient

Second,try to initialize the MyReader object in the constructor of the page object
What could be the reason of your problem is that you don't have proper assignment of the event handler method for the Load event, which is done in the InitiliaizeComponent() method automatically by the development environment. Check this first
Check as well if the MyReader is really created,that there is no OleDbException thrown during fetching the data from the database
Svetosla
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top