Dear readers
I used to have experience with VB6 and for a while i am making my steps into vb2010...
I have a database in MS access with several tables... and i want to show one of its tables in a datagridview. I know how to set a connection to the database and know what a database is and what it can do.
The problem is in this code that it is without code-errors ) but does not display the table into my datagridview....
the code in public class form1 is
Code:
Private DataGridViewBoogieProjectgegevens As New DataGridView()
Private BindingSourceDataGridViewBoogieProjectgegevens As New BindingSource()
and in the same form but in a private sub
Code:
Private Sub BoogieDatabaseInleesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BoogieDatabaseToolStripMenuItem.Click
Dim conBoogieDatabase As New OleDbConnection
Dim OpenDatabaseDialog As New OpenFileDialog
Dim strBoogiePadFile As String
Try
'----------------------------------------------------------------------------------
strBoogiePadFile = ""
'Select the Boogie database
If strBoogiePadFile = "" Then
OpenDatabaseDialog.DefaultExt = "mdb"
OpenDatabaseDialog.Filter = "Databasefiles (*.mdb)|*.mdb"
If Not strBoogiePadFile = vbNullString Then
OpenDatabaseDialog.InitialDirectory = strBoogiePadFile
End If
OpenDatabaseDialog.ShowDialog()
strBoogiePadFile = OpenDatabaseDialog.FileName
frmBoogie.txtPadFileBoogie.Text = strBoogiePadFile
End If
'----------------------------------------------------------------------------------
'Make the connection to the database
conBoogieDatabase = SetDatabaseConnectie(strBoogiePadFile)
'Open the database if its not opened already
If conBoogieDatabase.State = ConnectionState.Closed Then conBoogieDatabase.Open()
'Fill a new dataset-tabel and bind it to BindingSource
Me.BindingSourceDataGridViewBoogieProjectgegevens.DataSource = GetDataAdapter("Projectgegevens", conBoogieDatabase)
Me.DataGridViewBoogieProjectgegevens.AutoResizeColumns()
frmBoogie.ShowDialog(Me)
Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.Critical, "Fout in BoogieDatabaseInleesToolStripMenuItem_Click")
End Try
End Sub
and 2 functions in a different module for the connectionpart and adapter
Code:
Public Function SetDatabaseConnectie(ByVal strDatabaseNaam As String) As OleDbConnection
Dim conDatabase As New OleDbConnection
'conDatabase.ConnectionString = "Driver={Microsoft Access Driver (*.mdb)};" & _
' "Dbq=" & strDatabaseNaam & ";" & _
' "Uid=admin;" & _
' "Pwd="
conDatabase.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strDatabaseNaam & ";Persist Security Info=False;User Id=admin;Password=;"
Return conDatabase
conDatabase = Nothing
End Function
Public Function GetDataAdapter(ByVal strTabel As String, ByVal pConnectie As OleDbConnection, Optional ByRef pAdapter As OleDbDataAdapter = Nothing) As DataSet
Dim strSQL As String = "Select * from " & strTabel
Dim pTabel As New DataSet
pAdapter = New OleDbDataAdapter(strSQL, pConnectie)
pAdapter.Fill(pTabel, strTabel)
Return pTabel
End Function