gwt
gwt

Reputation: 2423

strange issue on posting page and click event

I have a .aspx page wich I designed for reporting , It has a gridview on It plus two buttons and two textboxes for date value . I wrapped the code behind Of one button in a try catch block , means if any error occurs after clicking the button the error should be handled , It works fine but when I enter sum values into textboxes and click the button the following error occurs :

Server Error in '/' Application.
--------------------------------------------------------------------------------


Object reference not set to an instance of an object. 
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

 Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


 An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 



[NullReferenceException: Object reference not set to an instance of an object.]
   Pages_Admin_Report_New._btnReport_Click(Object sender, EventArgs e) +33120
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +154
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3803




--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18045 

as It's showne the code does not go into try catch block , I really have no Idea what is happening , any helps would be appreciated .

Here Is Page load Code :

protected void Page_Load(object sender, EventArgs e)
{
    if (Session["UserId"] == null)
    {
        Response.Redirect("../../Default.aspx");
    }
    try
    {
        string currentPageFileName = new FileInfo(this.Request.Url.LocalPath).Name;
        if (!userIsAuthorised(Guid.Parse(Session["UserId"].ToString()), currentPageFileName))
        {
            Response.Redirect("../../Default.aspx");
        }
    }
    catch { Response.Redirect("../../Default.aspx"); }
}

here is button click event code :

protected void _btnReport_Click(object sender, EventArgs e)
{
    try
    {
     //My Code here
    }
    catch (Exception ex)
    {
        lblMessage.Text = "Error : " + ex.InnerException.Message;
    }
}

here is the full .aspx code

    <%@ Page Title="" Language="C#" MasterPageFile="~/Pages/Admin/SportMasterPage.master" AutoEventWireup="true" CodeFile="Report_New.aspx.cs" Inherits="Pages_Admin_Report_New" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <link href="../../Content/Style/StyleSheet.css" rel="stylesheet" type="text/css" />
    <script src="../../Content/Js/jquery.js" type="text/javascript"></script>
    <script src="../../Content/Js/jquery.maskedinput-1.3.js" type="text/javascript"></script>
    <script type='text/javascript'>
        jQuery(function ($) {
            $("#txtFromDate").mask("9999/99/99");
            $("#txtToDate").mask("9999/99/99");
        });
    </script>
    <script type="text/javascript">

        function PrintPanel() {

            var panel = document.getElementById("<%=pnlPrint.ClientID %>");

            var printWindow = window.open('', '', 'height=400,width=800');

            printWindow.document.write('<html><head><link href="../../Content/Style/StyleSheet.css" rel="stylesheet" type="text/css" /><title>DIV Contents</title>');

            printWindow.document.write('</head><body dir="rtl" >');

            printWindow.document.write(panel.innerHTML);

            printWindow.document.write('</body></html>');

            printWindow.document.close();

            setTimeout(function () {

                printWindow.print();

            }, 500);

            return false;

        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <div id="frmData" class="MainPageData">
        <table>
            <tr>
                <td>
                    <div id="frmtitle">
                        آمار کلی ثبت نام</div>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblMessage" runat="server"></asp:Label>
                </td>
            </tr>
        </table>
        <hr class="frmborder" />
        <div class="space">
        </div>
        <table style="width: 100%">
            <tr style="width: 100%">
                <td style="width: 100%">
                    <table style="width: 100%">
                        <tr>
                            <td style="padding-right: 15px">
                                از :
                            </td>
                            <td>
                                <asp:TextBox ID="txtFromDate" runat="server" MaxLength="10" dir="ltr" Width="100px"
                                    class="validate[required] text-input" ClientIDMode="Static"></asp:TextBox>
                            </td>
                            <td style="padding-right: 15px">
                                تا :
                            </td>
                            <td>
                                <asp:TextBox ID="txtToDate" runat="server" MaxLength="10" dir="ltr" Width="100px"
                                    class="validate[required] text-input" ClientIDMode="Static"></asp:TextBox>
                            </td>
                            <td>
                                &nbsp;&nbsp;
                            </td>
                            <td style="padding-right: 15px">
                                &nbsp;
                            </td>
                            <td style="padding-right: 15px">
                                <asp:Button ID="_btnReport" runat="server" Text="تهییه گزارش" 
                                    onclick="_btnReport_Click"  />
                            </td>
                            <td>
                                <asp:Button ID="_btnReportToday" runat="server" Text="گزارش امروز" 
                                    onclick="_btnReportToday_Click"  />
                            </td>
                            <td>
                                <asp:ImageButton ID="btnPrint" runat="server" OnClientClick="PrintPanel(); return false;"
                                    ImageAlign="AbsMiddle" ImageUrl="~/Content/Image/print.png" ToolTip="چاپ" />
                            </td>
                        </tr>
                        <tr>
                            <td style="padding-right: 15px">
                                &nbsp;
                            </td>
                            <td>
                                &nbsp;
                            </td>
                            <td style="padding-right: 15px">
                                &nbsp;
                            </td>
                            <td>
                                &nbsp;
                            </td>
                            <td>
                                &nbsp;
                            </td>
                            <td style="padding-right: 15px">
                                &nbsp;
                            </td>
                            <td style="padding-right: 15px">
                                &nbsp;
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr style="width: 100%">
                <td style="width: 100%">
                    <asp:Panel ID="pnlPrint" runat="server">
                        <table style="width: 100%">
                            <tr style="width: 100%">
                                <td style="width: 100px" align="right" >
                                <img src="../../Content/Image/48x48.jpg"  />
                                </td>
                                <td   colspan="3"  align="center">
                                    <asp:Label ID="lblTitle" runat="server" Font-Names="Tahoma" Font-Size="16px"></asp:Label>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="4">
                                    <br />
                                    <br />
                                </td>
                            </tr>
                            <tr style="width: 100%" dir="rtl">
                                <td style="text-align: center; width: 100px">
                                </td>
                                <td style="text-align: center; width: 100px">
                                    عضویتــ
                                </td>
                                <td style="text-align: center; width: 100px">
                                    پارکینگـ
                                </td>
                                <td style="text-align: center; width: 155px">
                                    خدمات
                                </td>
                            </tr>
                            <tr style="width: 100%">
                                <td style="width: 100%" colspan="4">
                                    <asp:GridView ID="_GVTotalRegistration" runat="server" CssClass="mGrid" AutoGenerateColumns="False">
                                        <Columns>
                                            <asp:TemplateField>
                                                <ItemTemplate>
                                                    <asp:HyperLink ID="linkReport" runat="server" Text='<%#Eval("RegisterType")%>' Target="_blank"
                                                        NavigateUrl='<%#Eval("Url")%>' Style="text-decoration: none"></asp:HyperLink>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:BoundField DataField="Total_Ozviat" HeaderText="کل" />
                                            <asp:BoundField DataField="Total_Ozviat_New" HeaderText="جدید" />
                                            <asp:BoundField DataField="Total_Ozviat_Old" HeaderText="قدیم" />
                                            <asp:BoundField DataField="Total_Ozviat_Men" HeaderText="مرد" />
                                            <asp:BoundField DataField="Total_Ozviat_Women" HeaderText="زن" />
                                            <asp:BoundField DataField="Total_Parking" HeaderText="کل" />
                                            <asp:BoundField DataField="Total_Parking_Men" HeaderText="مرد" />
                                            <asp:BoundField DataField="Total_Parking_Women" HeaderText="زن" />
                                            <asp:BoundField DataField="Total_Card_New" HeaderText="کارت جدید" />
                                            <asp:BoundField DataField="Total_Card_Replace" HeaderText="تعویض کارت" />
                                            <asp:BoundField DataField="Total_Card_Parking" HeaderText="پارکینگ" />
                                        </Columns>
                                        <RowStyle HorizontalAlign="Center" />
                                    </asp:GridView>
                                </td>
                            </tr>
                        </table>
                    </asp:Panel>
                </td>
            </tr>
        </table>
    </div>
</asp:Content>

Upvotes: 0

Views: 1638

Answers (1)

Andrei
Andrei

Reputation: 56716

Most likely the exception you are catching in the handler does not have any inner exception, and when you try to access inner exception's message - the NullReference is thrown. You should check for null first:

catch (Exception ex)
{
    if (ex.InnerException != null)
    {
        lblMessage.Text = "Error : " + ex.InnerException.Message;
    }
    else
    {
        lblMessage.Text = "Error : " + ex.Message;
    }
}

Upvotes: 2

Related Questions