System.NullReferenceException was unhandled by user code

Hi,

I am working on ASP.net2.0 and C#

I have created a delegate method
public event CommandEventHandler SaveReportImageButtonClicked;


But when I write

SaveReportImageButtonClicked(this, new CommandEventArgs("ExportImageTo", strExportTo));

It throws an error


System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."
  Source="App_Web_luvuxx3t"
  StackTrace:
       at Reports_ReportsMaster.imgExportReportToExcel_Click(Object sender, ImageClickEventArgs e) in d:\Dinesh\Projects\IssueManager_Latest\IssueManager_Latest\IssueManager\Reports\ReportsMaster.master.cs:line 111
       at System.Web.UI.WebControls.ImageButton.OnClick(ImageClickEventArgs e)
       at System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


But My delegate public event CommandEventHandler MonthsButtonClicked;
works fine

Can anyone help me in this.

Note: In CommandEventHandler MonthsButtonClicked, button is clicked

and in public event CommandEventHandler SaveReportImageButtonClicked;

is a Image button clicked

Below is my code

Please guide
public event CommandEventHandler MonthsButtonClicked;
public event CommandEventHandler SaveReportImageButtonClicked;
 
 
protected void btnViewReport_Click(object sender, EventArgs e)
{        
	if (txtMonths.Text != string.Empty && Convert.ToInt32(txtMonths.Text) > 0)
	    MonthsButtonClicked(this, new CommandEventArgs(ddlProjects.SelectedValue, txtMonths.Text));
}
 
 
protected void imgExportReportToPDF_Click(object sender, ImageClickEventArgs e)
{
    string strExportTo = "PDF";
    SaveReportImageButtonClicked(this, new CommandEventArgs("ExportImageTo", strExportTo));
}
 
 
 
IN ASPX MASTER PAGE IT is written
 
<td style="text-align: left" id="tdCommonControl" runat="server">
<asp:Panel ID="pnlCommonControl" runat="server">
 
    <asp:Label ID="lblMonths" runat="server" Text="Last Months: " Style="font-weight: 700"></asp:Label>&nbsp;
    <asp:TextBox
	ID="txtMonths" runat="server" Width="50px"></asp:TextBox>&nbsp;<asp:Button ID="btnViewReport"
	    runat="server" Text="View Report" CssClass="leftMenuHeader" OnClick="btnViewReport_Click" />
</asp:Panel>
 
</td>
<td>
<asp:Label ID="Label1" runat="server" Style="font-weight: 700" Text="Export Report: "></asp:Label><br />
<asp:ImageButton ID="imgExportReportToExcel" runat="server" CommandArgument="ExportToExcel"
    CommandName="ExportToExcel" ImageUrl="~/images/XLS_true.gif" OnClick="imgExportReportToExcel_Click" />&nbsp;
    
    <asp:ImageButton
	ID="imgExportReportToPDF" runat="server" CommandArgument="ExportToPDF" CommandName="ExportToPDF"
	ImageUrl="~/images/PDF_true.gif" OnClick="imgExportReportToPDF_Click" />
	
 
</td>

Open in new window

tia_kamakshiAsked:
Who is Participating?
 
Juan_BarreraConnect With a Mentor Commented:
Mmm.....

Is the Master object not null here?:

protected void Page_Init(object sender, EventArgs e)
{        
        Master.ProjectIdChanged += new CommandEventHandler(Master_ProjectIdChanged);
        Master.SaveReportImageButtonClicked += new CommandEventHandler(Master_ExportReport);  
}

Can you please check that?

0
 
Ivo StoykovCommented:
Hi 
you must  initialize it so as to knows that such a delegate exists
in you 
Me.your_object_here.Click += new System.EventHandler(Me.SaveReportImageButtonClicked);
0
 
tia_kamakshiAuthor Commented:

Hi,

Thanks for your reply,

This bit I have already written in ASPX file

and the above code is written in Master File



It works good with

Master.ProjectIdChanged += new CommandEventHandler(Master_ProjectIdChanged);

But now with
Master.SaveReportImageButtonClicked += new CommandEventHandler(Master_ExportReport);  

May ne the difference is that

SaveReportImageButtonClicked is using Image button and
Master_ProjectIdChanged is using ASPX button

Please guide
Below is my code in ASPX file
 
 
protected void Page_Init(object sender, EventArgs e)
{        
	Master.ProjectIdChanged += new CommandEventHandler(Master_ProjectIdChanged);
	Master.SaveReportImageButtonClicked += new CommandEventHandler(Master_ExportReport);  
}
 
protected void Master_ProjectIdChanged(object sender, CommandEventArgs e)
{
	int intProjectId = 0;
	string strProjectName = e.CommandName;
 
	intProjectId = Convert.ToInt32(e.CommandArgument.ToString());
	LoadReportsData(intProjectId);
}
 
 
protected void Master_ExportReport(object sender, CommandEventArgs e)
{
	string strExportReportTo = e.CommandName;
	SaveReports(strExportReportTo);
}

Open in new window

0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Juan_BarreraCommented:
Hi there,

The "e" argument for the ImageButton should be of type ImageClickEventArgs:

SaveReportImageButtonClicked(this, new ImageClickEventArgs(xPosition, yPosition));

Open in new window

0
 
tia_kamakshiAuthor Commented:
But in command argument I wish to pass string

and in aspx file I will take this string from command argument

and in

new ImageClickEventArgs(xPosition, yPosition))

xPosition and yPosition are the int values, where user has clicked on the image

Which does not meet my requirement

Please look into this
0
 
Juan_BarreraCommented:
Then you need to change this:

OnClick="imgExportReportToExcel_Click"

with this:

OnCommand="imgExportReportToExcel_Command"

And the same with the other ImageButton.
0
 
tia_kamakshiAuthor Commented:
Now, at below line I get an exception

SaveReportImageButtonClicked(this, new CommandEventArgs(imgExportReportToExcel.CommandName, imgExportReportToExcel.CommandArgument));

I get an exception

System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."
  Source="App_Web_aa3nwd0_"
  StackTrace:
       at Reports_ReportsMaster.imgExportReportToExcel_Command(Object sender, CommandEventArgs e) in d:\Dinesh\Projects\IssueManager_Latest\IssueManager_Latest\IssueManager\Reports\ReportsMaster.master.cs:line 137
       at System.Web.UI.WebControls.ImageButton.OnCommand(CommandEventArgs e)
       at System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
       
       
       
AND WHEN I WRITE



protected void imgExportReportToExcel_Command(object sender, CommandEventArgs e)
{
        SaveReportImageButtonClicked(this, new CommandEventArgs("Excel"));
}


Then I get an Exception      
       
Argument '1': cannot convert from 'string' to 'System.Web.UI.WebControls.CommandEventArgs'


How can I pass the string here

Please guide

Many Thanks
0
 
Juan_BarreraCommented:
The code below is almost fine, you just need to set the second parameter:

protected void imgExportReportToExcel_Command(object sender, CommandEventArgs e)
{
        SaveReportImageButtonClicked(this, new CommandEventArgs("Excel",String.Empty ));
}

Open in new window

0
 
tia_kamakshiAuthor Commented:
protected void imgExportReportToExcel_Command(object sender, CommandEventArgs e)
{
      SaveReportImageButtonClicked(this, new CommandEventArgs("Excel", String.Empty));
}


Still At line
SaveReportImageButtonClicked(this, new CommandEventArgs("Excel", String.Empty));

I get an error


System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."
  Source="App_Web_abrdv_io"
  StackTrace:
       at Reports_ReportsMaster.imgExportReportToExcel_Command(Object sender, CommandEventArgs e) in d:\Dinesh\Projects\IssueManager_Latest\IssueManager_Latest\IssueManager\Reports\ReportsMaster.master.cs:line 138
       at System.Web.UI.WebControls.ImageButton.OnCommand(CommandEventArgs e)
       at System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


(:
0
 
tia_kamakshiAuthor Commented:
Thanks you are right.

It works great now.

Thanks again
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.