?
Solved

Populating DataGrid from table or query in MS Access

Posted on 2006-06-11
5
Medium Priority
?
361 Views
Last Modified: 2008-02-01
Hello

I’m using a VB.Net front to an MS Access database.

I’d like to connect to the Access database, get all the data from a table or query in MS Access and display it in a DataGrid.

I have actually managed it, but it involves 2 connections to the database - which I’m pretty sure is not right.

Imports ADODB
Imports System.Data.OleDb

Dim Conn As Connection
Dim rs As ADODB.Recordset

Conn = New Connection
rs = New ADODB.Recordset

Dim connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
                        "Data Source=c:\Projects.mdb;" & _
                        "Persist Security Info=False")

        Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                        "Data Source=c:\Projects.mdb;" & _
                                "Persist Security Info=False"

        Conn.Open()

Dim adapter As New OleDbDataAdapter("QueryName", connection)

rs.Open("QueryName ", Conn)

Dim customersTable As New DataTable("Customers")

adapter.Fill(customersTable, rs)

DataGrid1.DataSource = customersTable

Conn.Close()


Thanks.
Iain
0
Comment
Question by:IainTheVBALearner
  • 3
  • 2
5 Comments
 
LVL 34

Expert Comment

by:Sancler
ID: 16882312
Iain

You're right, it's not necessary to do everything twice.  The OleDb dataadapter and the datatable REPLACE the ADOBD recordset.  You should use the former in VB.NET.  Your code should work like this

'''Imports ADODB
Imports System.Data.OleDb

'''Dim Conn As Connection
'''Dim rs As ADODB.Recordset

'''Conn = New Connection
'''rs = New ADODB.Recordset

Dim connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
                        "Data Source=c:\Projects.mdb;" & _
                        "Persist Security Info=False")

'''        Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                        "Data Source=c:\Projects.mdb;" & _
                                "Persist Security Info=False"

'''        Conn.Open()

Dim adapter As New OleDbDataAdapter("QueryName", connection)

'''rs.Open("QueryName ", Conn)

Dim customersTable As New DataTable("Customers")

adapter.Fill(customersTable) ''', rs)

DataGrid1.DataSource = customersTable

'''Conn.Close()

Roger

0
 
LVL 3

Author Comment

by:IainTheVBALearner
ID: 16887715
Hello Roger.  Thanks for the response.

If I put a simple SQL statement in "QueryName" then it works.  But If I put the name a query or table from the database it's connected it, then it doesn't work.  It fails on the adapter.Fill line with this error - no other details:

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll

So this works:
Dim adapter As New OleDbDataAdapter("SELECT * FROM [tbl Apps]", connection)

But this doesn't:
Dim adapter As New OleDbDataAdapter("qryProjectsBasicByTitle", connection)

qryProjectsBasicByTitle is a valid SELECT query in the database, with no parameters.

Do you know how to make it fill the DataGrid from a query?

Thanks
Iain
0
 
LVL 34

Accepted Solution

by:
Sancler earned 500 total points
ID: 16888222
If you want to use a dataadapter then you will need to use a SELECT statement, although you can do so with "SELECT * FROM MyAccessQuery".

If you want to call an Access Query directly, you will need to use a DataReader.  On these lines

Dim connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
                        "Data Source=c:\Projects.mdb;" & _
                        "Persist Security Info=False")

Dim customersTable As New DataTable("Customers")
'code needed to structure datatable

Dim cmd As New OleDbCommand("MyAccessQuery")
cmd.Connection = connection
cmd.CommandType = CommandType.StoredProcedure
connection.Open
Dim dr As OleDbDataReader = cmd.ExecuteReader()
While dr.Read
   'code needed to write values from single record returned into row of datatable
End While
Connection.Close()

Sorry, in my initial response, I hadn't picked up the point about using an MS Access query directly.

As you can see, it is far simpler to use a dataadapter - selecting from a Query, rather than from a Table, if necessary - because (a) it will create the datatable structure for you and (b) will bring over all the necessary data without you needing to code putting it in the datatable.  But the other route (which is closer to the ADOBD recordset approach, perhaps) is available if you want to use it.

Roger
0
 
LVL 3

Author Comment

by:IainTheVBALearner
ID: 16889030
mmm....okay.  Thanks.  I guess that answers the question, although I'm not sure how to populate the DataTable by moving thought the DataReader...

I'm really struggling with this...I've done loads of MS Access - recordsets & multicolumn listboxes and they work great.

I'm teaching myself VB.NET but I'm struggling to see what's better...it seems that a DataGrid is an quivalent to a multicolumn listbox, as there is no multicolumn in VB.NET, but populating a DataGrid is such a palava - and I've not even begun editing DataGraid data and updating the Access tables based on the new DataGrid data...

I'm pretty new to this though, and I'm sure I'll look back on this and realise it's only because there were a bunch of things I didn't know...

Thanks.
Iain
0
 
LVL 34

Expert Comment

by:Sancler
ID: 16890019
Iain

It will get easier, but there'll be a lot of swearing at it first ;-)

Thanks for the points

Roger

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses
Course of the Month16 days, 9 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question