VB.NET - how to load sub report - Asked By madhavi on 15-Feb-12 01:42 AM

Hi,

Iam using vs2005 and sql server 2005.


how to load sub report?
because iam using 2 stored procedres. 1 for main report another for sub report.
but my main report showing data ok but sub report didnt show the data.whats my mistake i dont know.


Please any one help me.its very urgent.i tried so many ways. thanks in advance..

my code is below..



Imports Toms.Common
Imports Toms.Data
Imports Toms.Entity
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared




Partial Public Class AdditionalSalesReport
    Inherits System.Web.UI.Page




    'Dim sFuncType As String
    Dim oDataview As DataView
    Dim oRpt As New ReportDocument
    Dim rpt As New CrNewSalesListing
    Dim oDsListing As New DataSet
    Dim oDsListingsub As New DataSet
    Dim oDsListing1 As New DataSet
    Dim oDsListing2 As New DataSet


    Dim ods As New DataSet
    Dim resp As New ResponseMessage
    Dim resp1 As New ResponseMessage
    Dim resp2 As New ResponseMessage
    Dim fromdt As String
    Dim todt, flag As String
    Dim BatchNo As String
    Dim Logon As CrystalDecisions.Shared.TableLogOnInfo
    Dim mySubReportObject As CrystalDecisions.CrystalReports.Engine.SubreportObject
    'Sub report document of crystal report.
    Dim mySubRepDoc As New CrystalDecisions.CrystalReports.Engine.ReportDocument


    Private Sub AdditionalSalesReport_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        oRpt = New CrystalDecisions.CrystalReports.Engine.ReportDocument


        oRpt.Load(System.Configuration.ConfigurationManager.AppSettings("ReportPath") & "Reports\\CrAdditionalSalesListing.rpt")


    End Sub
    Private Sub crViewer1_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles crViewer1.Unload
        oRpt.Close()
        oRpt.Dispose()
    End Sub


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        BatchNo = Request.QueryString("BatchNo")
        BindReport()


    End Sub


    'Private Sub BindReport()
    '    Dim oConVar As New ConnectionVariable


    '    Dim unsDb As New dbUNS8


    '    resp = unsDb.GetAdditionalSalesListingReport(Trim(BatchNo))


    '    If resp.Success Then


    '        oDsListing = resp.RespObject


    '        oRpt.Database.Tables("command").SetDataSource(oDsListing.Tables(0))


    '        Dim Table As CrystalDecisions.CrystalReports.Engine.Table
    '        For Each Table In oRpt.Database.Tables
    '            Dim Logon As CrystalDecisions.Shared.TableLogOnInfo
    '            oConVar.ExtractConnectionString()
    '            Logon = Table.LogOnInfo


    '            Logon.ConnectionInfo.ServerName = ConnectionVariable.sDBServer
    '            Logon.ConnectionInfo.DatabaseName = ConnectionVariable.sDBNm
    '            Logon.ConnectionInfo.Password = ConnectionVariable.sDBPwd
    '            Logon.ConnectionInfo.UserID = ConnectionVariable.sDBUsrID


    '            Table.ApplyLogOnInfo(Logon)
    '        Next


    '        oRpt.PrintOptions.PaperOrientation = PaperOrientation.Portrait


    '        crViewer1.Visible = True
    '        crViewer1.ReportSource = oRpt
    '        Me.btnGetPdf.Visible = True
    '        GoPDF()
    '    Else
    '        crViewer1.Visible = False
    '        Me.btnGetPdf.Visible = False
    '        Me.lblMessage.Text = "No Record Found !"
    '    End If




    'End Sub
    Private Sub GoPDF()
        Dim oStream As New System.IO.MemoryStream
        oStream = oRpt.ExportToStream(ExportFormatType.PortableDocFormat)
        Response.ClearContent()
        Response.Buffer = True
        Response.ContentType = "application/pdf"
        Response.BinaryWrite(oStream.ToArray())
        Response.End()
    End Sub
    Private Sub btnGetPdf_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetPdf.Click
        Dim oStream As New System.IO.MemoryStream
        oStream = oRpt.ExportToStream(ExportFormatType.PortableDocFormat)
        Response.ClearContent()
        Response.Buffer = True
        Response.ContentType = "application/pdf"
        Response.BinaryWrite(oStream.ToArray())
        Response.End()
    End Sub
    Private Sub BindReport()
        Dim oConVar As New ConnectionVariable
        Dim index As Integer
        Dim intCounter As Integer
        Dim intCounter1 As Integer


        Dim unsDb As New dbUNS8


        resp = unsDb.GetAdditionalSalesListingReport(Trim(BatchNo))
        resp1 = unsDb.GetAdditionalSalesListingSubReport(Trim(BatchNo))


        If resp.Success And resp1.Success Then
            oDsListing = resp.RespObject
            oDsListingsub = resp1.RespObject
            ' oRpt.Database.Tables("command").SetDataSource(oDsListing.Tables(0))




            Dim Table As CrystalDecisions.CrystalReports.Engine.Table
            For Each Table In oRpt.Database.Tables
                oConVar.ExtractConnectionString()
                Logon = Table.LogOnInfo




                Logon.ConnectionInfo.ServerName = ConnectionVariable.sDBServer
                Logon.ConnectionInfo.DatabaseName = ConnectionVariable.sDBNm
                Logon.ConnectionInfo.Password = ConnectionVariable.sDBPwd
                Logon.ConnectionInfo.UserID = ConnectionVariable.sDBUsrID


                Table.ApplyLogOnInfo(Logon)
            Next


            For index = 0 To oRpt.ReportDefinition.Sections.Count - 1
                For intCounter = 0 To oRpt.ReportDefinition.Sections(index).ReportObjects.Count - 1
                    With oRpt.ReportDefinition.Sections(index)
                        If .ReportObjects(intCounter).Kind = CrystalDecisions.Shared.ReportObjectKind.SubreportObject Then
                            mySubReportObject = CType(.ReportObjects(intCounter), CrystalDecisions.CrystalReports.Engine.SubreportObject)
                            mySubRepDoc = mySubReportObject.OpenSubreport(mySubReportObject.SubreportName)
                            For intCounter1 = 0 To mySubRepDoc.Database.Tables.Count - 1
                                mySubRepDoc.Database.Tables(intCounter1).ApplyLogOnInfo(Logon)
                                mySubRepDoc.Database.Tables(intCounter1).ApplyLogOnInfo(Logon)
                                mySubRepDoc.Database.Tables("command").SetDataSource(oDsListingsub.Tables(0))
                            Next
                        End If
                    End With
                Next
            Next


            oRpt.PrintOptions.PaperOrientation = PaperOrientation.Portrait


            crViewer1.Visible = True
            crViewer1.ReportSource = oRpt
            Me.btnGetPdf.Visible = True
            GoPDF()
        Else
            crViewer1.Visible = False
            Me.btnGetPdf.Visible = False
            Me.lblMessage.Text = "No Record Found !"
        End If
    End Sub
End Class
Chintan Vaghela replied to madhavi on 15-Feb-12 01:48 AM

Hello,

 

  ReportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(localReport_SubreportProcessing);

            ReportViewer1.LocalReport.Refresh();

 

void localReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)

      {

        e.DataSources.Add(new ReportDataSource("DataSet1_rpt_SubReport", ObjectDataSource1));

 

      }

 

Hope this is helpful !

Thanks

 

 

 

 

 

 

 

madhavi replied to Chintan Vaghela on 15-Feb-12 02:00 AM
Hi,

Thanks for your quick reply.

Iam using vS2005 vb.net, sql server 2005.

sorry to say  i couldn't understand what u sent.
can u please explain briefly.
Chintan Vaghela replied to madhavi on 15-Feb-12 03:34 AM
For load subreprort, then you need to added this report DataSource Dynemicay as per above code