VB.NET-INFORMATIONS.COM




     VB.NET Crystal Reports Tutorials

*  

  Crystal Reports from SQL Query String

*  

*  

*  

*  

*  

*  

*  

 

Crystal Reports from SQL Query String

In usual practice , Crystal Reports we are getting from pre defined columns. But we can make Crystal Reports from Dynamic column . Here we are going to do the dynamic Crystal Reports from SQL statements . That is we enter SQL in textbox and get the Crystal Reports according to the SQL statement.

vb.net_crystal_report_dynamic_column_0.GIF

All Crystal Reports programming samples in this tutorials is based on the following database (crystaldb) . Please take a look at the database structure before you start this tutorial - Click here to see Database Structure .

Create a new VB.NET project and add a Strongly Typed Dataset . Before creating a Strongly Typed take a look at the detailed tutorial of create a strongly typed datset and add five column in the Datatable. Here we are limiting as five column , but you can add any number of column according to your requirements.

vb.net_crystal_report_dynamic_column_1.GIF

Next step is to create a Crystal Reports design from the Strongly Typed dataset.

vb.net_crystal_report_dynamic_column_2.GIF

Select all the column from dataset.

vb.net_crystal_report_dynamic_column_3.GIF

Select the default form(Form1.vb) and add a TextBox , Button and Crystal Reports Viewer .

Here we are going to pass the SQl statements to Crystal Reports at runtime . For that we parsing the SQL statement before we passing it to Crystal Reports. So we create a function for parsing SQL statements.

Public Function procesSQL() As String

Put the following vb.net source code in your form and run the program .

VB.NET Source Code

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

NOTE :

You have to provide the necessary databse information to Connection String.

VB.NET Crystal Reports Related Contents
*     Sample Database and tables for Crystal Reports tutorials
*     Step by Step help for creating a simple Crystal Reports in VB.NET
*     VB.NET Crystal Reports from multiple tables
*     VB.NET Crystal Reports String parameter
*     VB.NET Crystal Reports Integer parameter
*     VB.NET Crystal Reports Date parameter
*     VB.NET Crystal Report Load Dynamically
*     VB.NET Crystal Reports Formula Fields
*     VB.NET Crystal Reports Summary Fields
*     VB.NET Crystal Reports Export to PDF
*     VB.NET Crystal Reports Export to Excel
*     Email a Crystal Reports from VB.NET
*     Crystal Reports Without Database
*     Crystal Report from SQL Query String
*     Crystal Reports from XML File
*     Create a Subreport in Crystal Reports
*     Create a Subreport in Crystal Reports with Link
*     How to deploy Crystal Reports on Clinet Machine
*     How to create Crystal Reports installer using Merge Modules

VB.Net Related Tpoics
*     Microsoft .Net Framework Tutorials
*     VB.NET Language Basics Tutorials
*     VB.NET Program Flow Control Tutorials
*     VB.NET Collections Tutorials
*     VB.NET String Tutorials
*     VB.NET Files Tutorials
*     VB.NET Excel 2007 Tutorials
*     VB.NET Crystal Reports Tutorials
*     VB.NET Communications Tutorial
*     VB.NET ADO.NET Tutorial with source code
*     ADO.NET Data Providers help and Tutorial
*     VB.NET ADO.NET Dataset Tutorial
*     ADO.NET DataAdapter and Dataset
*     VB.NET ADO.NET DataView Tutorial
*     VB.NET Remoting Tutorial
*     VB.NET XML Tutorial

VB.NET

*  

*  

*  

*  

*  

*  

*  

 

VB.Net Tutorial Summary

Search here for more Vb.Net Source Code :


   Categories

    HOME
    VB.NET
    CSHARP

 
 

   













  |  Home   |  SiteMap   |  About   |
net-informations.com (C) 2009 All Rights Reserved