Crystal Report not showing data from SQL Server 17.5 - sql-server

After creating a table and putting data in it in the previous form, this form tries to display the report, but I am getting a blank report. What am I doing wrong?
Here is my VB 2017 code for form load event:
Imports CrystalDecisions.CrystalReports.Engine
Public Class Form5
Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim cryRpt As New ReportDocument
cryRpt.Load("C:\Users\Administrator\source\repos\WindowsApp3\WindowsApp3\CrystalReport4.rpt")
CrystalReportViewer1.ReportSource = cryRpt
CrystalReportViewer1.Refresh()
End Sub
End Class

I solved my problem in the following way
Here is my modified VB 2017 code for form load event:
Dim query As String = "SELECT * FROM monAtt"
Dim cmd As New SqlCommand(query, conn1)
cmd.CommandType = CommandType.Text
conn1.Open()
Dim MyDA As New SqlClient.SqlDataAdapter()
MyDA.SelectCommand = cmd
Dim myDS As New TestDataSet1()
MyDA.Fill(myDS, "monAtt")
Dim oRpt As New CrystalDecisions.CrystalReports.Engine.ReportDocument()
oRpt.Load("C:\Users\Administrator\source\repos\WindowsApp3\WindowsApp3\CrystalReport4.rpt")
oRpt.SetDataSource(myDS.Tables("monAtt"))
CrystalReportViewer1.ReportSource = oRpt
CrystalReportViewer1.RefreshReport()
CrystalReportViewer1.Visible = True
conn1.Close()
cmd.Dispose()

Related

vb.net crystal report is asking for database login and password but logon failed

I'm new in making crystal reports and I'm done creating my crystal report but when running my program it asks for my logon information but after I log in, it says that
logon failed
I tried to look in other questions here in the site but I was not able to find an answer. My Visual Studio is 2013 Ultimate and my sql is sql-server 2014 express.
Here's my code:
Imports System.Data.SqlClient
Imports System.Data.Sql
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim show As String = String.Empty
show &= "select * from fruit_stock "
show &= "where date_received=#daterec"
Using conn As New SqlConnection("server=WIN10;database=fruit_storage;user=fruit_admin;password=admin;")
Using cmd As New SqlCommand
With cmd
.Connection = conn
.CommandType = CommandType.Text
.CommandText = show
.Parameters.AddWithValue("#daterec", TextBox1.Text)
End With
Try
conn.Open()
Dim da As New SqlDataAdapter
Dim ds As New DataSet
da.SelectCommand = cmd
da.Fill(ds, "fruit_stock")
Dim report As New CrystalReport1
CrystalReportViewer1.ReportSource = report
CrystalReportViewer1.Refresh()
conn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Using
End Using
End Sub
End Class
Dim report As New CrystalDecisions.CrystalReports.Engine.ReportDocument
report.Load("<physical filename of your report>")
report.SetDataSource(ds.Tables("fruit_stock"))
<your_crystal_report_viewer_in_your_form>.ReportSource = report
if you are using an embedded report
Dim report As New <Name of your embedded crystal report>
report.SetDataSource(ds.Tables("fruit_stock"))
<your_crystal_report_viewer_in_your_form>.ReportSource = report

How to display data from SQL Server on a DataGridView

I want to load or show the data from SQL Server on a DataGridView.
The build succeeds and there's no error when I run it. There's nothing wrong in form modul_koneksi (I think) because it works on my other form (form_login)
However, nothing shows up in my DataGridView. How can I fix this?
Code:
Imports System.Data.SqlClient
Public Class FormProduk
Private Sub FormProduk_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim str As String = "Data Source=Fahriy;Initial Catalog=DBLogin;Integrated Security=True"
Dim connection As New SqlConnection(str)
Dim com As String = "Select * From tbl_user"
Dim dataadapter As New SqlDataAdapter(com, connection)
Dim dataset As New DataSet()
dataadapter.Fill(dataset, "tbl_user")
DataGridView1.DataSource = dataset.Tables()
End Sub
End Class
You should define which table to load in the DataGridView:
DataGridView1.DataSource = dataset.Tables("tbl_user")
You cannot set the .DataSource property with dataset.Tables. Instead you need to set it with a DataTable located in your DataSet:
DataGridView1.DataSource = dataset.Tables("tbl_user")
However I think you could simplify the code a little by getting rid of the SqlDataAdapter and loading straight into a DataTable
Dim dt As New DataTable
dt.Load(com.ExecuteReader())
DataGridView1.DataSource = dt
I would also consider implementing Using:
Sometimes your code requires an unmanaged resource, such as a file handle, a COM wrapper, or a SQL connection. A Using block guarantees the disposal of one or more such resources when your code is finished with them. This makes them available for other code to use.
With the changes your code would look something like this:
Dim dt As New DataTable
Using con As New SqlConnection("Data Source=Fahriy;Initial Catalog=DBLogin;Integrated Security=True"),
cmd As New SqlCommand("SELECT * FROM tbl_user", con)
con.Open()
dt.Load(cmd.ExecuteReader())
End Using
DataGridView1.DataSource = dt
Not enough info to tell, are you sure your connection string str is correct. You can try change to this code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim str As String = "Data Source=Fahriy;Initial Catalog=DBLogin;Integrated Security=True"
Dim conn As New SqlConnection(str)
Dim cmd As String = "Select * From tbl_user"
Dim adapter As New SqlDataAdapter(cmd, conn)
Dim tabeluser As New DataSet
adapter.Fill(tabeluser)
DataGridView1.DataSource = tabeluser.Tables
End Sub

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
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))

Datagridview vb won't work

I'm doing an A Level in computing (I'm terrible at programming so thats why im here) and I followed a tutorial to get a datagridview to load up a table in the database I have linked to the project and nothing comes up in the debug, still greyed out table.
Here is my code:
Imports System.Data.OleDb
Public Class Cards
Dim con As New OleDbConnection
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= |DataDirectory|\Cards.accdb"
con.Open()
datagridShow()
End Sub
Private Sub datagridShow()
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter
da = New OleDbDataAdapter("SELECT * FROM cards", con)
da.Fill(dt)
DataGridView1.DataSource = dt.DefaultView
con.Close()
End Sub
End Class
'If there is no data in the Grid There are No Cells to click so your code won't trigger a DataGridView1.CellContentClick So you will never hit your DataGridView1_CellContentClick method
'I just ran your code and it worked. I did put my connection string in so check your connection 'string.
'I am not sure why you are loading the grid on the cell contact click.
'Try this and call loadData on pageload or a button click event this should work for you.
Private Sub loadData()
con.ConnectionString = "Your Connection String"
con.Open()
datagridShow()
End Sub
Private Sub datagridShow()
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter
da = New OleDbDataAdapter("SELECT * FROM cards", con)
da.Fill(dt)
DataGridView1.DataSource = dt.DefaultView
con.Close()
End Sub

Unable to display SQL database data on Visual basic form

I am facing a problem displaying the records of my table on the visual basic form I have created.
This is my code :
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
myconnection = New SqlConnection("server=HOME-PC\SQLEXPRESS;uid=sa;pwd=123;database=college")
myconnection.Open()
mycommand = New SqlCommand("SELECT * from demo3)", myconnection)
Dim mySqlDataAdapter As New SqlDataAdapter(mycommand)
Dim mydsStudent As New DataSet()
mySqlDataAdapter.Fill(mydsStudent, "Student")
ra = mycommand.ExecuteNonQuery()
MessageBox.Show("Data Displayed" & ra)
myconnection.Close()
End Sub
End Class
Note: my database name is "college" , table name is "demo3" . Table contains 2 columns namely name and roll no. How to display the data in those columns on the visual basic form that I have created ?
You don't need to call execute non query. You can bind the dataset to a DataGridView. Like this
Dim DataGridView1 as new DataGridView()
DataGridView1.DataSource = mydsStudent
'Your table goes here, not sure about the exact propety name, hope it works.
DataGridView1.DisplayMember = "demo3"
Me.Controls.Add(DataGridView1)

Resources