-1

I can't understand why my Access database won't load into a DataGridView for me. I have no errors within my code so I'm unsure as to what is going on. The only error I'm getting is the configuration element is not declared.

Imports System.Data.OleDb

Public Class Form1
Dim conn As New OleDbConnection

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim objConn As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source = Students.accdb"
    conn.Open()

    dataGridShow()

End Sub

Private Sub dataGridShow()
    Dim objDS As New DataSet
    Dim objDT As New DataTable
    objDS.Tables.Add(objDT)

    Dim objDA As New OleDb.OleDbDataAdapter
    objDA = New OleDbDataAdapter("SELECT * FROM Students", conn)
    objDA.Fill(objDT)

    DataGridView1.DataSource = objDT.DefaultView

    conn.Close()

End Sub

End Class

My app config looks like this. My database is located in bin/Debug

<?xml version="1.0" encoding="utf-8"?
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <connectionStrings>
  </connectionStrings>
</configuration>
djv
  • 15,168
  • 7
  • 48
  • 72
D.Emerson
  • 1
  • 6
  • Don't use the load event. Put that code in the constructor. Don't leave your connections open like that. Put disposable objects in a using-endusing block. Your database connection has no path, so it can only assume the same path as the executable. – LarsTech Mar 26 '18 at 17:26
  • "I have no errors within my code so im so unsure as to what is going on." - Just because it compiles doe not mean that there are no errors. You define `objConn`, but never use it. – TnTinMn Mar 26 '18 at 17:38
  • @TnTinMn ok.. so how would i solve this problem? – D.Emerson Mar 26 '18 at 17:49
  • I noticed as I was editing your question that your app.config is missing a closing bracket at the end of ` – djv Mar 26 '18 at 17:57
  • See [VS2010 does not show unhandled exception message in a WinForms Application on a 64-bit version of Windows](https://stackoverflow.com/q/4933958/719186) – LarsTech Mar 26 '18 at 18:09

1 Answers1

0

You don't need to open and close your connection. The DataAdapter does that for you. Your connection will never work without a valid connection string so I moved your connection string to the dataGridShow procedure and set the ConnectionString property of your conn object.

Private conn As New OleDbConnection

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        dataGridShow()
    End Sub

    Private Sub dataGridShow()
        Dim objDS As New DataSet
        Dim objDT As New DataTable
        objDS.Tables.Add(objDT)
        Dim objConn As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source = Students.accdb"
        conn.ConnectionString = objConn
        Dim objDA As New OleDb.OleDbDataAdapter("SELECT * FROM Students", conn)
        objDA.Fill(objDT)
        DataGridView1.DataSource = objDT.DefaultView
    End Sub
Mary
  • 14,926
  • 3
  • 18
  • 27