|
-
Mar 14th, 2008, 10:39 PM
#1
Thread Starter
New Member
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?
-
Mar 18th, 2008, 12:53 PM
#2
Re: connecting dataset to a blank crystal report in vb 2005
Do you have the dataset created?
-
Mar 20th, 2008, 11:54 PM
#3
Thread Starter
New Member
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.
-
May 14th, 2008, 04:44 AM
#4
Member
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
-
May 14th, 2008, 10:05 AM
#5
Hyperactive Member
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!
-
May 14th, 2008, 10:32 AM
#6
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|