Ok, I had posted this-

http://www.daniweb.com/forums/thread128689.html

and got this piece of code from a link (http://vb.net-informations.com/crystal-report/dynamic_crystal_report_from_sql_query_string.htm)

Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.Data
Public Class Form1
    Dim objRpt As New CrystalReport1
    Private Sub Button1_Click(ByVal sender As System.Object, 
	ByVal e As System.EventArgs) Handles Button1.Click
        Dim cnn As SqlConnection
        Dim connectionString As String
        Dim sql As String

        connectionString = "data source=SERVERNAME; _
		initial catalog=crystaldb;user id=sa;password=PASSWORD;"
        cnn = New SqlConnection(connectionString)
        cnn.Open()
        sql = procesSQL()
        Dim dscmd As New SqlDataAdapter(sql, cnn)
        Dim ds As New DataSet1
        dscmd.Fill(ds, "Product")
        objRpt.SetDataSource(ds.Tables(1))
        CrystalReportViewer1.ReportSource = objRpt
        CrystalReportViewer1.Refresh()
    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 TextObject

        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(objRpt.ReportDefinition.ReportObjects("Text"  _
				& i + 1), TextObject)
                MyText.Text = fields(i).ToString()
            Else
                firstPart = firstPart & fields(i).ToString() & _
				"  AS COLUMN" & i + 1
                MyText = CType(objRpt.ReportDefinition.ReportObjects("Text" & _
				 i + 1), TextObject)
                MyText.Text = fields(i).ToString()
            End If
        Next
        sql = "SELECT " & firstPart & " " & lastPart
        Return sql
    End Function

End Class

The error is reported on the line- Dim objRpt As New CrystalReport1 What should have been the correct Syntax? Or does it require me to have an API to handle Crystal Reports (if it does not come native with VS 2008 Pro)

Recommended Answers

All 4 Replies

Maybe it is because there is no report (=class) "CrystalReport1" in your project.

Does Importing some API make the CrystalReport class available?

Sorry, I do not understand what you mean with "Importing some API"

The RT must be able to instantiate a class named "CrystalReport1", else the line Dim objRpt As New CrystalReport1 cannot work.

Normally you would create such a class by adding a crystal report named "CrystalReport1.rpt" into your project. For VS 2003:

  • Rightclick the project in the solution explorer
  • Select "Add New Item"
  • Select "Crystal Report" in the "Add New Item" Dialog, give it the name "CrystalReport1.rpt", finish with "Open".
  • Follow the instructions in the Crystal Report "Wizzard" Gallery.

After this there is a report (=class) CrystalReport1 in your project that can be instantiated.

commented: Thanks +1
commented: agreed +14

I did as you said and its working now.

Thankie!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.