Programmatically connect msaccess table to datagridview

Hi,
How can I connect msaccess table to dataGridView object?
there are an easy sample code?
I want to connect it programmatically in Vb.net.
Thanks
bobdylan75Asked:
Who is Participating?
 
DhaestCommented:
If you want to use an ms access 2007 database, you need to alter the connectionstring

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccess2007file.accdb;Persist Security Info=False;

Source: http://connectionstrings.com/access-2007
0
 
DhaestCommented:
You can use the following code
(you need to set the connection and sql-string correctly to your project)
Dim StrConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Mydb.mdb"
Dim oConn As New OleDbConnection(StrConn)
Dim da As OleDbDataAdapter
Dim oDS As New DataSet
'
Dim sSQL As String
'
sSQL = "Select * From MyTable"
da = New OleDbDataAdapter(sSQL, oConn)
da.Fill(oDS, "Table1")
DataGridView1.DataSource = oDS.Tables("Table1").DefaultView
DataGridView1.Refresh()

Open in new window

0
 
bobdylan75Author Commented:
Thanks, but using that code It gives me a not define error "E_FAIL" on the FILL code line.
I'm using VS2005.

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
DhaestCommented:
Did you make the adjustments to your database ?
1. Data Source=C:\Mydb.mdb
2. select * from ....
0
 
bobdylan75Author Commented:
yes I do!
the path is correct and the SQL too..
if I use the "automatic objects" it works,
If I do it programmatically gives me an error..
I'm thinking to make a test in a standalone project..

I would use the "automatic objects" but in the file.config it creates
a string with an absolute path, I would like use a relative one..

But the programmatically solution is what I prefer..
0
 
DhaestCommented:
What is the exact error ?
0
 
bobdylan75Author Commented:
System.Data.OleDb.OleDbException was not handled
  ErrorCode=-2147467259
  Message="IErrorInfo.GetDescription failed with E_FAIL(0x80004005)."
  Source="System.Data"
...
0
 
bobdylan75Author Commented:
One moment..
I have change the database..
I've choosen another one,
and everything runs.
But I have to use the other database..
I have done it with MsAccess2007 comptible with access 2002/2003..
I'm thinking that the problem should be here..
0
 
bobdylan75Author Commented:
Ok it works fine,
and if I want to add a user/passowrd to the database?
0
 
DhaestCommented:
With database password
This is the connection string to use when you have an Access 2007 database protected with a password using the "Set Database Password" function in Access.

Quote:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccess2007file.accdb;Jet OLEDB: Database Password=MyDbPassword;  
0
 
DhaestCommented:
By the way. When you add a question, you can assign multiple zones for one question. If you do that, you'll probably reach more experts
This question could be also under the next zones: Visual Studio, VB.NET
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.