Search code examples
sql-servervb.netcrystal-reportsdatasetvb.net-2010

No data display in crystal report from vb.net, dataset with SQL


I am using Vb.Net 2010 to develop project that has crystal reports CRforVS_13_0. I connect to sqlserver and fill my dataset using the following steps.

Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports CrystalDecisions.ReportSource
Imports System.Data

Public Class frmPrinTest

   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
     Dim MyCommand As New SqlCommand
     Dim myDA As New SqlDataAdapter

    connectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=stock;Persist Security Info=True;User ID=sa;Password=1234[enter image description here][1];"

    cnn = New SqlConnection(connectionString)
    sql = "select Icode,Iname from stockInvt"

    MyCommand.Connection = cnn
    MyCommand.CommandText = sql
    MyCommand.CommandType = CommandType.Text
    myDA.SelectCommand = MyCommand
    Dim ds As New DataSet1
    myDA.Fill(ds, "stockInvt")

    Dim objRpt As New CrystalReport1
    objRpt.SetDataSource(ds)
    CrystalReportViewer1.ReportSource = objRpt
    'CrystalReportViewer1.Refresh()
   End Sub
End Class

But I can't display the data in my dataset or datatable in crystal report. I follow this link also. CrystalReport_ADO_Dataset PDF Please help me.

My CrystalReportViewer


Solution

  • You have to assign what table crystal report will used. So just change your code from this objRpt.SetDataSource(ds) to this objRpt.SetDataSource(ds.tables("stockInvt")) OR objRpt.SetDataSource(ds.tables(0))