[Solved] ASP.Net MVC RDLC Report Export Error: An exception of type System.NullReferenceException occurred

on Mar 23, 2021 11:33 PM


I am using rdlc report first time when application load it is running perfectly but when calling second time or if changing parameter and clicking on report button, showing error on following line.

 Error Line

Dim fs As FileStream = New FileStream(HttpContext.Current.Server.MapPath("output.pdf"), FileMode.Create)


An exception of type 'System.NullReferenceException' occurred in App_Web_ewdvs_zp.dll but was not handled in user code


Public Sub LoadReportRDLC()
    lblMessage.Text = ""
        Dim objReport As New clsmembers
        Dim dsReport As New DataSet
        objReport.CompanyID = Convert.ToInt32(cboCompany.SelectedValue)
        objReport.Status = Convert.ToString(cboType.SelectedValue)
        objReport.FamilyID = Convert.ToInt32(cboFamily.SelectedValue)
        objReport.BloodGroupID = Convert.ToInt32(cboBloodGroup.SelectedValue)
        objReport.DughterOfJamat = Convert.ToInt32(cboDaughter.SelectedValue)
        objReport.DuesPosition = "" ' cboDues.SelectedValue
        objReport.MartialStatus = Convert.ToString(cboMartialStatus.SelectedValue)
        objReport.TransType = "21"
        If objReport.LoadMembers(dsReport) = False Then
            lblMessage.Text = "<div class=""alert alert-danger"">" & objReport.ErrMessage & "</div>"
            ReportViewer1.Visible = False
        End If
        If dsReport.Tables(0).Rows.Count > 0 Then
            ReportViewer1.Visible = True
            ReportViewer1.LocalReport.ReportPath = Server.MapPath("Reports/rptMemberList.rdlc")
            Dim parameters As ReportParameter() = New ReportParameter(5) {}
            parameters(0) = New ReportParameter("prmCompanyName", Convert.ToString(cboCompany.SelectedItem.Text))
            parameters(1) = New ReportParameter("prmFamilyName", Convert.ToString(cboFamily.SelectedItem.Text))
            parameters(2) = New ReportParameter("prmSex", Convert.ToString(cboMartialStatus.SelectedItem.Text))
            parameters(3) = New ReportParameter("prmDaughterOfJamat", Convert.ToString(cboDaughter.SelectedItem.Text))
            parameters(4) = New ReportParameter("prmActiveInActive", Convert.ToString(cboType.SelectedItem.Text))
            parameters(5) = New ReportParameter("prmBloodGroup", Convert.ToString(cboBloodGroup.SelectedItem.Text))
            ReportViewer1.LocalReport.EnableExternalImages = True
            ReportViewer1.LocalReport.DataSources.Add(New ReportDataSource("dsMemberList", dsReport.Tables(0)))
            ReportViewer1.Visible = False
            lblMessage.Text = "<div class=""alert alert-danger"">" & " Data not found" & "</div>"
        End If
    Catch ex As Exception
        lblMessage.Text = "<div class=""alert alert-danger"">" & ex.InnerException.Message & "</div>"
    End Try
End Sub
Protected Sub CreatePrint()
        Dim warnings As Warning() = Nothing
        Dim streamids As String() = Nothing
        Dim mimeType As String = Nothing
        Dim encoding As String = Nothing
        Dim extension As String = Nothing
        Dim bytes As Byte() = ReportViewer1.LocalReport.Render("PDF", Nothing, mimeType, encoding, extension, streamids, warnings)
        Dim fs As FileStream = New FileStream(HttpContext.Current.Server.MapPath("output.pdf"), FileMode.Create)
        fs.Write(bytes, 0, bytes.Length)
        Dim document As Document = New Document(PageSize.A4)
        Dim reader As PdfReader = New PdfReader(HttpContext.Current.Server.MapPath("output.pdf"))
        Dim writer As PdfWriter = PdfWriter.GetInstance(document, New FileStream(HttpContext.Current.Server.MapPath("Print.pdf"), FileMode.Create))
        Dim cb As PdfContentByte = writer.DirectContent
        ScriptManager.RegisterStartupScript(Page, GetType(Page), "OpenWindow", "window.open('output.pdf" & "');", True)
    Catch ex As Exception
        lblMessage.Text = "<div class=""alert alert-danger"">" & ex.InnerException.Message & "</div>"
    End Try
End Sub


Download FREE API for Word, Excel and PDF in ASP.Net: Download
on Mar 31, 2021 07:53 AM