Results 1 to 6 of 6

Thread: connecting dataset to a blank crystal report in vb 2005

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    15

    connecting dataset to a blank crystal report in vb 2005

    I have created a blank crystal report through visual basic 2005. Inserted a crystal viewer control on a new form. Now i want these to connect to a data set programmatically. How?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: connecting dataset to a blank crystal report in vb 2005

    Do you have the dataset created?

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    15

    Re: connecting dataset to a blank crystal report in vb 2005

    Yes I have created a dataset say ds through visual basic 2005. My database is in SQL Server 2005. Now how to connect this programmeticlly to a the form.
    While creating a I had chosen a new blank crystal report.

  4. #4
    Member
    Join Date
    Jun 2007
    Location
    Lomé - TOGO
    Posts
    38

    Re: connecting dataset to a blank crystal report in vb 2005

    Hi mp_minhas,
    Am facing the same issue right now. Curious to know if u finally found a way to get this. I've done couple of research but still can't get it.
    Do share your solution with me if u've got any.

    WaZda

  5. #5
    Hyperactive Member vincentg's Avatar
    Join Date
    Jun 2005
    Location
    Chicago IL, USA
    Posts
    261

    Re: connecting dataset to a blank crystal report in vb 2005

    Hello,

    So you have your DataSet with Data.

    I got some simple VB.Net Code for you and hopefully it help


    Code:
    Dim oCrystal As New rptYourReportObject
    
    '-- This is optional, depends if you set the connection right away from your CR
    oCrystal.SetDatabaseLogon(YourServer, YourPassword, YourServer, YourDatabase)
    oCrystal.SummaryInfo.ReportTitle = "Your Report Title"
    oCrystal.SummaryInfo.ReportComments = " "
    oCrystal.SetDataSource(dt_YourDataSet.Tables(0))
    
    '-- Then attached it to your Crystal Report Viewer Object
    Me.CrystalReportViewer.ReportSource = Nothing
    Me.CrystalReportViewer.ReportSource = oCrystal

    Note:
    It should work if the Data Column/Names/Field at your CR would be the same to your DataSet
    This wont work at your Blank CR. If you have time, design it first.

    Good luck!
    - No Signature Yet -

  6. #6
    Member
    Join Date
    Jun 2007
    Location
    Lomé - TOGO
    Posts
    38

    Re: connecting dataset to a blank crystal report in vb 2005

    Hi,
    I've found a working solution. But like Vincentg said, u have to design your CR first. Here is how i did mine
    Create a Dataset and define the schema by drag and drop the database table from Server Explorer.
    If there are multiple tables then put all the tables within one dataset itself.
    Make sure the schema of your drag and drop on the Report is exactly the same schema of your query output.

    Here is a sample of my code
    Code:
    'So to start this, i've dragged and dropped "CatId" and "CatType" from the dataset in the server explorer onto the report
    
    	Dim frmReportView As New Form1 'Form1 is where the form on which I have the Crytal Report Viewer
            Dim myReport As New CrystalReport2 'CrystalReport2 is the report to generate
    
    	'Do the Connection to the Data base programmatically
            Dim connectionstring As String = "Data Source=POSTE-91;Initial Catalog=MandatMission;Integrated Security=True"
            Dim connection As New SqlClient.SqlConnection(connectionstring)
    
    	'Here is the Query String. On the Report I have only "CatId" and "CatType" so the query should be exactly the same therefore i select only "CatId" and "CatType"
            Dim strSelectStatement As String = "SELECT CatId, CatType FROM CategPro where CatId = @CatId "
            Dim SelectCommand As New SqlClient.SqlCommand(strSelectStatement, connection)
    
    	'Adding the parameter stated in the query. If u don't have any parameter in your query u should add the line below
            SelectCommand.Parameters.Add(New Data.SqlClient.SqlParameter("@CatId", Trim$(txtCode1.Text)))
    
            connection.Open()
    
    	'Create the Data Adapter
            Dim da As New SqlClient.SqlDataAdapter(SelectCommand)
    
    	'Create the DataSet
            Dim ds As DataSet = New DataSet
    
    	'Fill the DataSet with the Table from Which u are doing the Query
            da.Fill(ds, "CategPro") 'Note here that the Table name is VERY IMPORTANT. It doesn't work with me when i don't add the "CategPro" as per what I have in the query string above named "strSelectStatement"
    
    	'Bind the DataSet to the Report
            myReport.SetDataSource(ds)
    
    	'Load the Report in the Crystal Report Viewer
            frmReportView.CrystalReportViewer1.ReportSource = myReport
    
    	'Close the Connection
            connection.Close()
    
    	'Display the Report
            frmReportView.ShowDialog(Me)
    
    	'Dispose the Report after the user has closed it
            frmReportView.Dispose()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width