Click here to Skip to main content
15,890,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I have a problem to create single crystal report to generate data of multiple SQL queries dynamically. I have googled lot but I could not find the right solution to solve my problem.

My requiement is to create the single crystal report that should load the contents of the SQL Query passed on that time dynamically not to create the report using of drag and drop wizard.

For Example, I have a text field to enter the SQL query and Button field to generate Crysrtal Report. if I hit the generate button after providing the sql query, the CR should be generate the SQL query result inside the crystal report.

This is to avoid to generate multiple Crystal report for multiple queries.

What I have tried:

This is the code I have tried so far, I 'm stuck to find out the right solution.

VB Code:
Private Sub loadlabe2()
Dim cryRpt As New ReportDocument
Dim cnn As SqlConnection
Dim connectionString As String
Dim sql As String

connectionString = "Data source=MYDATA\MYAPP21;initial catalog=XYZ_FrontOffice;user id=XYZ;password=test"
cnn = New SqlConnection(connectionString)
cnn.Open()
sql = procesSQL()
Dim dscmd As New SqlDataAdapter(sql, cnn)
Dim ds As New DataSet4
dscmd.Fill(ds, "DataTable1")
cryRpt.SetDataSource(ds.Tables(1))
CrystLabl.ReportSource = "CrystalReport4.rpt"
CrystLabl.RefreshReport()
End Sub
Public Function procesSQL() As String
Dim sql As String
Dim inSql As String
Dim firstPart As String
Dim lastPart As String
Dim selectStart As Integer
Dim fromStart As Integer
Dim fields As String()
Dim i As Integer
Dim mytext As CrystalDecisions.CrystalReports.Engine.TextObject
Dim discreteVal1 As New CrystalDecisions.Shared.ParameterDiscreteValue()

inSql = TextBox1.Text
inSql = inSql.ToUpper

selectStart = inSql.IndexOf("SELECT")
fromStart = inSql.IndexOf("FROM")
selectStart = selectStart + 6
firstPart = inSql.Substring(selectStart, (fromStart - selectStart))
lastPart = inSql.Substring(fromStart, inSql.Length - fromStart)

fields = firstPart.Split(",")
firstPart = ""
For i = 0 To fields.Length - 1
If i > 0 Then
firstPart = firstPart & " , " & fields(i).ToString() & " AS COLUMN" & i + 1
mytext = CType(CrystLabl.ReportSource.ReportObjects("Text" & i + 1), TextObject)
mytext.Text = fields(i).ToString()
Else
firstPart = firstPart & fields(i).ToString() & " AS COLUMN" & i + 1
mytext = CType(CrystLabl.ReportSource.ReportObjects("Text" & i + 1), TextObject)
mytext.Text = fields(i).ToString()
End If
Next
sql = "SELECT " & firstPart & " " & lastPart
Return sql
End Function
Posted
Updated 30-Dec-16 18:31pm
v3
Comments
an0ther1 2-Jan-17 20:32pm    
Personally I wouldn't do this. You will experience performance issues at some point and you are basically letting the user query any table they want to which is probably bad.
You will also need to handle any query that the user writes - remember that crystal uses the query result to determine the fields in the report - you need to go through every variation and create an appropriate display.
A search of the net found the following through;
http://stackoverflow.com/questions/5729434/how-can-i-pass-sql-query-to-crystal-report
The page the solution is from contains a lot of examples but the links don't go anywhere, you may be able to find the examples with a bit of effort.

Re-think your design & requirements. It would be easier to have the report run from a stored procedure that takes some parameters as input and outputs in a standard way that you then display in your report.

Good luck though

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900