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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

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
Juan_BarreraCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tia_kamakshiAuthor Commented:
Thanks you are right.

It works great now.

Thanks again
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.