0

我有一个为报告而设计的 .aspx 页面,上面有一个 gridview 加上两个按钮和两个用于日期值的文本框。我将 Of one button 后面的代码包装在 try catch 块中,这意味着如果单击按钮后发生任何错误,则应处理错误,它工作正常,但是当我将 sum 值输入文本框并单击按钮时,会发生以下错误:

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 

因为它显示代码没有进入 try catch 块,我真的不知道发生了什么,任何帮助将不胜感激。

这是页面加载代码:

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"); }
}

这是按钮单击事件代码:

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

这是完整的 .aspx 代码

    <%@ 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>
4

1 回答 1

2

您在处理程序中捕获的异常很可能没有任何内部异常,并且当您尝试访问内部异常的消息时 - NullReference 被抛出。您应该首先检查 null :

catch (Exception ex)
{
    if (ex.InnerException != null)
    {
        lblMessage.Text = "Error : " + ex.InnerException.Message;
    }
    else
    {
        lblMessage.Text = "Error : " + ex.Message;
    }
}
于 2013-07-08T07:29:41.590 回答