We help IT Professionals succeed at work.

ASP.net fill GridView with table data from Access table (VB.net)

Murray Brown
Murray Brown asked
on
Medium Priority
666 Views
Last Modified: 2012-05-12
Hi

I am trying to use the following code to fill a GridView on my ASP.net
WebForm from a table in an Access database but nothing is coming through.
Dim oFullPath As String = Environ("userprofile") & "\Desktop\EMS_2007.accdb"
    Dim ConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & oFullPath & ";"

    Dim dbadp As OleDbDataAdapter
    Dim dTable As New DataTable

 Sub Fill_GridView()
        Try
            Dim sSQL As String

            sSQL = "SELECT * FROM [AANew]"
            Dim connection As New OleDbConnection(ConnectionString)
            dbadp = New OleDbDataAdapter(sSQL, connection)

            dbadp.Fill(dTable)
            Me.GridView1.DataSource = dTable
            'Me.DataGridView1.Columns(0).Width = 0
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

Open in new window

Comment
Watch Question

Web Developer
CERTIFIED EXPERT
Commented:
As I am having little knowledge of VB so I could suggest you ideas.

First problem I suppose is that you connection string is been not set properly. So you can follow steps:

1. Place your database file in App_Data folder of your website.

2. Declare connection string in web.config as:

1. For .mdb only
<add name="conString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|EMS_2007.accdb" providerName="System.Data.OleDb"/>

2. For .accdb and .mdb
<add name="disString" connectionString="Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=|DataDirectory|EMS_2007.accdb;MultipleActiveResultSets=True"/>

Open in new window


3. Then code for accessing data from database.

Second is You have not opened connection to database. That seems to be problem too.
Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
thanks for the help

Explore More ContentExplore courses, solutions, and other research materials related to this topic.