Datagridview vb won't work - database

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

Related

Crystal Report not showing data from SQL Server 17.5

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

Expiration Time out VB.Net

I'm turning an SQL Request in my application which is very big and need a lot of traitement. When I execute the request in SQL server, it takes time, but I have the result, but when I execute it in my application, after many seconds, it gives me an error of Time out Expiration.
Here is my VB.Net Button:
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Dim str As String = "Data Source=----;Initial Catalog=----;Persist Security Info=True;User ID=---;Password=---"
Dim con As New SqlConnection(str)
Dim com As String = " ma RequĂȘte"
Dim Adpt As New SqlDataAdapter(com, con)
Dim ds As New DataSet()
Adpt.Fill(ds, "Organisation")
DataGridView1.DataSource = ds.Tables(0)
End Sub
Can anyone help?

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

Tables not updating in MS Access through the visual basic login form?

So I'm doing a car rental project for my I.T assignment. Everything is working fine except the data isn't updating through the program, i.e. if I add a new User through the program, it doesn't save into the Access Database, therefore I can't use it to log in. And if I manually enter a username and password directly into the table on the Access Database, it still does not allow me to log in with it.
I went on 'Data > Add New Data Source > Database > Dataset > CarRentalSystem1ConnectionString > Tables (selected) > Finish'. The database seems to be connected yet the information isn't updating from either side. Is it something to do with my coding?
Thanks for your help.
Imports System.Data.OleDb
Public Class LogIn
Dim con As New OleDbConnection
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
If ask() = True Then
Main_Form.Show()
Me.Hide()
Else
MessageBox.Show("Invalid Username or Password!", "Access Denied", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
Me.Close()
End Sub
Private Sub LogIn_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Information Technology 11\Crown's Garage\FinalCarRentalSystem\CarRentalSystem1.mdb"
con.Open()
con.Close()
End Sub
Public Function ask()
Dim dt As New DataTable
Dim ds As New DataSet
ds.Tables.Add(dt)
con.Open()
Dim da As New OleDbDataAdapter("select * from Admin", con)
da.Fill(dt)
For Each datarow In dt.Rows
If UsernameTextBox.Text = datarow.item(1) And PasswordTextBox.Text = datarow(2) Then
con.Close()
Return True
End If
Next
con.Close()
Return False
End Function
End Class

Resources