dynamic crystal report with runtime generated dataset
Hi! I have to print a report of a runtime generated dataset. The user can select between 5 fields to report. E.g.: If he choose to print the price item, then the select query will have to onjoin the tbl_price. I need to know how to tell what field to print at runtime, and I just CAN'T FIND IT!!!!!! I googled all the web! msdn, newsgroup, yahoo, vbforums, i cant find it anywhere, canm anyone help me with this one, I'm desperate since the deadline is at the end of the week, and i been struggling with this for more than a week!!! I feel like I'm wastin my time, and i really don't have any to waste! anyhelp really appreciated!
Re: dynamic crystal report with runtime generated dataset
If you are using VB.Net then you may follow these steps
1. Add a Form in your application.
2. Create Data Adapter, Connection during runtime in that form
3. Generate a Dataset during design time from Data Adapter
4. Create the .rpt file from scratch and make it point to the Dataset that you created, remember not from the database direct.
5. Place a Crystal report viewer control on that form.
Your next move is to add reference to System.XML.dll
Depending on the database you are using you must import the required namespaces. Here I'll show you with MS-Access
In the first line of your Form
VB Code:
Imports System.Data.OleDb
Next step Runtime data population
VB Code:
private sub GetDataForReport()
dim myCon as new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=[your database file location] ")
dim myCMD as New OleDbCommand()
myCMD.Connection=myCon
myCMD.CommandType=CommandType.Text
myCMD.CommandText="Select Statement"
dim myDA as OleDbDataAdapter()
myDA.SelectCommand=myCMD
dim myDS as New [Your Datasetname in the Form]
myDA.Fill(myDS,"TableName")
dim myRPT as new [You .rpt Name]
myRPT.SetDataSource(myDS)
[Your Crystal report viewer].ReportSource=myRPT
And you are done. Enjoy... :thumb:
In the name of Jesus Christ, peace be with you.....God loves you.
If this work please rate it.
Re: dynamic crystal report with runtime generated dataset
Hi Suan, thank you for your help! I still have some problem. I did what you said, but I have a blank report. Obviously, I must be missing something!
Quote:
Originally Posted by Suan Ngaihte
4. Create the .rpt file from scratch and make it point to the Dataset that you created, remember not from the database direct.
- Does the dataset I point the report to have to be populated, or just a empty dataset??
- Do I have to drag the field I want on the report? Because the field printed will depend of the User's choice (he can choose whether to print these fields—'latin name', 'family', 'format', price', 'guarantee'— he can print them all or just 1 or them.)
here is the code:
VB Code:
Private Sub populateItemListing()
Dim cnnListing As New SqlConnection(STR_SQL_CONNECTION_STRING)
Dim cmmListing As New SqlCommand
Dim dadListing As New SqlDataAdapter
Dim dstListing As New DataSet1
Dim rptListing As New rptPlant
Try
cmmListing.Connection = cnnListing
cmmListing.CommandType = CommandType.Text
cmmListing.CommandText = "SELECT latin_name FROM tbl_items WHERE hard_good = 0"
dadListing.SelectCommand = cmmListing
dadListing.Fill(dstListing, "item")
rptListing.SetDataSource(dstListing)
list.reportList.ReportSource = rptListing
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
list.Show()
list.MdiParent = Me
End Try
Thank you! Any help appreciated!
Re: dynamic crystal report with runtime generated dataset
1. Do you create the report using dataset fields? You must...
2. Do you call the "populateItemListing()" method in the form load event? You must do....
3. Do your connection string point to the correct path? It must.
Check out....man.