Question Changing Tablename in CrystalReport

acidflash

Active member
Joined
Oct 23, 2008
Messages
29
Programming Experience
1-3
Hello,

I have a database which has many tables in it, and also a VB GUI to it which creates databases on the fly, the layout for each database is the same, Column1, Column2, Column3, Column4, Column5 ; I have 1 crystal report called "ProjectReport" and inside this report I have the layout that i want and all is well except that its bound to a single table in the database. What I want to know is how do I go about programmatically editing the tablename so that I can make a reference to whatever table I want in the database, and the rest stay the same in the crystal report. Its very urgent and any help is appreciated. This is my first time using crystal reports and I spent a lot of time searching on the net, I came across some code that was about 3 pages long and Im certain there is a shorter way. Thanks in advance.

acidflash
 
I was able to solve it through some very intense search and slight modifications, here is the code for anyone who might have a similar scenario as me:

VB.NET:
Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
        Dim CrystalReportViewer1 As CrystalDecisions.Windows.Forms.CrystalReportViewer = New CrystalDecisions.Windows.Forms.CrystalReportViewer
        Dim Report As CrystalDecisions.CrystalReports.Engine.ReportDocument = New CrystalDecisions.CrystalReports.Engine.ReportDocument
        DetailedReport.CrystalReportViewer1.ActiveViewIndex = 0
        DetailedReport.CrystalReportViewer1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        DetailedReport.CrystalReportViewer1.DisplayGroupTree = False
        DetailedReport.CrystalReportViewer1.Dock = System.Windows.Forms.DockStyle.Fill
        DetailedReport.CrystalReportViewer1.Location = New System.Drawing.Point(0, 0)
        DetailedReport.CrystalReportViewer1.Name = "CrystalReportViewer1"

        Dim QueryString As String = "select العدد,التاريخ,الشرح,السعر,الاجمالي from " & TableName & " "
        Dim Connection As New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Mode=Share deny none;Data Source =" & My.Application.Info.DirectoryPath & "\Yassen.mdb") 'Your Database Connection Here
        Connection.Open()

        Dim Adapter As OleDbDataAdapter = New OleDbDataAdapter(QueryString, Connection) 
        Dim DataSet As DataSet = New DataSet()
        Adapter.Fill(DataSet)
        Dim DataTable As DataTable = New DataTable
        DataTable = DataSet.Tables(0)
        Report.Load(Application.StartupPath & "/ReportProject.rpt")
        Report.SetDataSource(DataTable)
        DetailedReport.CrystalReportViewer1.ReportSource = Report
        DetailedReport.Show()
    End Sub

Note: This is directly from my project, you can copy paste it and modify it to fit to your projects its pretty self-explanatory.

acidflash
 
Back
Top