Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. access databse into datagridview

access databse into datagridview

Scheduled Pinned Locked Moved Visual Basic
helpdatabase
20 Posts 5 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R rajulama

    Hello everyone, I just wonder what's wrong with my code. Actually I want to use openfiledialog to locate an access database file and then display it in datagridview. I used following connection string:

    Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=;Data Source=" & fi.DirectoryName

    and following to call the table

    Dim objCmdSelect As New OleDbCommand("select * from table1" & fi.Name, objConn)

    But I get the famous error: "the microsoft jet database engine cannot open the file. it is already opened exclusively by another user or you need permission to view its data." But neither by database is open nor its password protected. Please help me resolving this problem

    C Offline
    C Offline
    Christian Graus
    wrote on last edited by
    #2

    I suggest you google for a tool which tells you what process has a file open. I know such a tool exists. It's plain the file is open, you need to work out why.

    Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

    R 1 Reply Last reply
    0
    • R rajulama

      Hello everyone, I just wonder what's wrong with my code. Actually I want to use openfiledialog to locate an access database file and then display it in datagridview. I used following connection string:

      Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=;Data Source=" & fi.DirectoryName

      and following to call the table

      Dim objCmdSelect As New OleDbCommand("select * from table1" & fi.Name, objConn)

      But I get the famous error: "the microsoft jet database engine cannot open the file. it is already opened exclusively by another user or you need permission to view its data." But neither by database is open nor its password protected. Please help me resolving this problem

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #3

      Are you viewing the contents of table1 in MS Access while debugging? If so, MS Access is the one that's locking the database :(

      I are troll :)

      R 1 Reply Last reply
      0
      • C Christian Graus

        I suggest you google for a tool which tells you what process has a file open. I know such a tool exists. It's plain the file is open, you need to work out why.

        Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

        R Offline
        R Offline
        rajulama
        wrote on last edited by
        #4

        thanks for your suggestions but I think you misunderstood my problem. Following is complete code and its not working:

        If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
        Dim fi As New FileInfo(OpenFileDialog1.FileName)
        Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=;Data Source=" & fi.DirectoryName
        Dim objConn As New OleDbConnection(sConnectionString)
        Dim objDataSet As DataSet = New DataSet()
        objConn.Open()
        Dim objCmdSelect As New OleDbCommand("select * from [Joint Restraint Assignments]" & fi.Name, objConn)
        Dim objAdapter1 As New OleDbDataAdapter
        objAdapter1.SelectCommand = objCmdSelect
        objAdapter1.Fill(objDataSet, "test")
        DataGridView1.DataSource = objDataSet.Tables(0).DefaultView
        objConn.Close()
        End If

        I just wonder what's wrong with this code, in particular when i want to read access database file.

        C 1 Reply Last reply
        0
        • L Lost User

          Are you viewing the contents of table1 in MS Access while debugging? If so, MS Access is the one that's locking the database :(

          I are troll :)

          R Offline
          R Offline
          rajulama
          wrote on last edited by
          #5

          thanks. no i m not. the access database as well as the table is not open.

          1 Reply Last reply
          0
          • R rajulama

            thanks for your suggestions but I think you misunderstood my problem. Following is complete code and its not working:

            If OpenFileDialog1.ShowDialog(Me) = DialogResult.OK Then
            Dim fi As New FileInfo(OpenFileDialog1.FileName)
            Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=;Data Source=" & fi.DirectoryName
            Dim objConn As New OleDbConnection(sConnectionString)
            Dim objDataSet As DataSet = New DataSet()
            objConn.Open()
            Dim objCmdSelect As New OleDbCommand("select * from [Joint Restraint Assignments]" & fi.Name, objConn)
            Dim objAdapter1 As New OleDbDataAdapter
            objAdapter1.SelectCommand = objCmdSelect
            objAdapter1.Fill(objDataSet, "test")
            DataGridView1.DataSource = objDataSet.Tables(0).DefaultView
            objConn.Close()
            End If

            I just wonder what's wrong with this code, in particular when i want to read access database file.

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #6

            Ok, why do you need a fileinfo object ? I suspect that's what's locking your file. I did not misunderstand, your file is locked, if you like it or not.

            Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

            R 1 Reply Last reply
            0
            • C Christian Graus

              Ok, why do you need a fileinfo object ? I suspect that's what's locking your file. I did not misunderstand, your file is locked, if you like it or not.

              Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

              R Offline
              R Offline
              rajulama
              wrote on last edited by
              #7

              thanks. what can be an alternative to fileInfo?

              C 1 Reply Last reply
              0
              • R rajulama

                thanks. what can be an alternative to fileInfo?

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #8

                you have the path already, use System.IO.Path.GetDirectoryName and Path.GetFileName to get the pieces of the file path that you seem to need.

                Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

                R 1 Reply Last reply
                0
                • C Christian Graus

                  you have the path already, use System.IO.Path.GetDirectoryName and Path.GetFileName to get the pieces of the file path that you seem to need.

                  Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

                  R Offline
                  R Offline
                  rajulama
                  wrote on last edited by
                  #9

                  sorry I didn't get it. Could you please give me an example?

                  C 1 Reply Last reply
                  0
                  • R rajulama

                    sorry I didn't get it. Could you please give me an example?

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #10

                    In the System.IO namespace: Path.GetDirectory("c:\documents\word.doc") will return c:\documents Path.GetFileName("c:\documents\word.doc") will return word.doc. The Path class is full of methods for manipulating and examining file paths

                    Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

                    R 1 Reply Last reply
                    0
                    • C Christian Graus

                      In the System.IO namespace: Path.GetDirectory("c:\documents\word.doc") will return c:\documents Path.GetFileName("c:\documents\word.doc") will return word.doc. The Path class is full of methods for manipulating and examining file paths

                      Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

                      R Offline
                      R Offline
                      rajulama
                      wrote on last edited by
                      #11

                      but my file location is not fixed. I want users to select the access database file and display a table from it into datagridview.

                      C 1 Reply Last reply
                      0
                      • R rajulama

                        but my file location is not fixed. I want users to select the access database file and display a table from it into datagridview.

                        C Offline
                        C Offline
                        Christian Graus
                        wrote on last edited by
                        #12

                        *sigh* Dim fi As New FileInfo(OpenFileDialog1.FileName) Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=;Data Source=" & fi.DirectoryName Dim objConn As New OleDbConnection(sConnectionString) Dim objDataSet As DataSet = New DataSet() objConn.Open() Dim objCmdSelect As New OleDbCommand("select * from [Joint Restraint Assignments]" & fi.Name, objConn) So, you take the filename from the openfiledialog and use the FileInfo object to extract the directory name and the filename. Instead, you can use the methods I gave you, to get those values from the filename property of the file dialog, directly. I am at a loss how you think what I have been saying takes away from your using a dynamic filename. If the filename was fixed, you'd type in the values in your code, not use methods to work them out, surely ?

                        Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

                        R 2 Replies Last reply
                        0
                        • C Christian Graus

                          *sigh* Dim fi As New FileInfo(OpenFileDialog1.FileName) Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=;Data Source=" & fi.DirectoryName Dim objConn As New OleDbConnection(sConnectionString) Dim objDataSet As DataSet = New DataSet() objConn.Open() Dim objCmdSelect As New OleDbCommand("select * from [Joint Restraint Assignments]" & fi.Name, objConn) So, you take the filename from the openfiledialog and use the FileInfo object to extract the directory name and the filename. Instead, you can use the methods I gave you, to get those values from the filename property of the file dialog, directly. I am at a loss how you think what I have been saying takes away from your using a dynamic filename. If the filename was fixed, you'd type in the values in your code, not use methods to work them out, surely ?

                          Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

                          R Offline
                          R Offline
                          rajulama
                          wrote on last edited by
                          #13

                          that means my no need to establish a connection. Do you mean to do something like: Dim fi As New FileInfo(OpenFileDialog1.FileName) path.GetFileName(&fi.name) Dim objCmdSelect As New OleDbCommand("select * from [Joint Restraint Assignments]" .....) ??? or, path.getfilename(openfiledialog1.filename) Dim objCmdSelect As New OleDbCommand("select * from [Joint Restraint Assignments]" .....)

                          1 Reply Last reply
                          0
                          • C Christian Graus

                            *sigh* Dim fi As New FileInfo(OpenFileDialog1.FileName) Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=;Data Source=" & fi.DirectoryName Dim objConn As New OleDbConnection(sConnectionString) Dim objDataSet As DataSet = New DataSet() objConn.Open() Dim objCmdSelect As New OleDbCommand("select * from [Joint Restraint Assignments]" & fi.Name, objConn) So, you take the filename from the openfiledialog and use the FileInfo object to extract the directory name and the filename. Instead, you can use the methods I gave you, to get those values from the filename property of the file dialog, directly. I am at a loss how you think what I have been saying takes away from your using a dynamic filename. If the filename was fixed, you'd type in the values in your code, not use methods to work them out, surely ?

                            Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

                            R Offline
                            R Offline
                            rajulama
                            wrote on last edited by
                            #14

                            i think i got it. But I am confused about the data source to be use in oledbconnection:

                            Path.GetFileName(OpenFileDialog1.FileName)
                            Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=???")

                            C 1 Reply Last reply
                            0
                            • R rajulama

                              i think i got it. But I am confused about the data source to be use in oledbconnection:

                              Path.GetFileName(OpenFileDialog1.FileName)
                              Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=???")

                              C Offline
                              C Offline
                              Christian Graus
                              wrote on last edited by
                              #15

                              Dim path as String =Path.GetFileName(OpenFileDialog1.FileName) will give you the path in a variable. Just calling GetFileName without storing the result, does nothing.

                              Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

                              R 3 Replies Last reply
                              0
                              • C Christian Graus

                                Dim path as String =Path.GetFileName(OpenFileDialog1.FileName) will give you the path in a variable. Just calling GetFileName without storing the result, does nothing.

                                Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

                                R Offline
                                R Offline
                                rajulama
                                wrote on last edited by
                                #16

                                thanks i got it. so what should be the data source in:

                                Dim path1 As String = Path.GetFileName(OpenFileDialog1.FileName)
                                Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=???")

                                1 Reply Last reply
                                0
                                • C Christian Graus

                                  Dim path as String =Path.GetFileName(OpenFileDialog1.FileName) will give you the path in a variable. Just calling GetFileName without storing the result, does nothing.

                                  Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

                                  R Offline
                                  R Offline
                                  rajulama
                                  wrote on last edited by
                                  #17

                                  Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=" & path1 & "")

                                  above code works. Thank you very very much

                                  1 Reply Last reply
                                  0
                                  • C Christian Graus

                                    Dim path as String =Path.GetFileName(OpenFileDialog1.FileName) will give you the path in a variable. Just calling GetFileName without storing the result, does nothing.

                                    Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

                                    R Offline
                                    R Offline
                                    rajulama
                                    wrote on last edited by
                                    #18

                                    :)

                                    1 Reply Last reply
                                    0
                                    • R rajulama

                                      Hello everyone, I just wonder what's wrong with my code. Actually I want to use openfiledialog to locate an access database file and then display it in datagridview. I used following connection string:

                                      Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=;Data Source=" & fi.DirectoryName

                                      and following to call the table

                                      Dim objCmdSelect As New OleDbCommand("select * from table1" & fi.Name, objConn)

                                      But I get the famous error: "the microsoft jet database engine cannot open the file. it is already opened exclusively by another user or you need permission to view its data." But neither by database is open nor its password protected. Please help me resolving this problem

                                      L Offline
                                      L Offline
                                      Luc Pattyn
                                      wrote on last edited by
                                      #19

                                      rajulama wrote:

                                      Dim sConnectionString As String = "..." & fi.DirectoryName

                                      Trying to open a directory as a database? :)

                                      Luc Pattyn [Forum Guidelines] [My Articles]


                                      Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


                                      1 Reply Last reply
                                      0
                                      • R rajulama

                                        Hello everyone, I just wonder what's wrong with my code. Actually I want to use openfiledialog to locate an access database file and then display it in datagridview. I used following connection string:

                                        Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=;Data Source=" & fi.DirectoryName

                                        and following to call the table

                                        Dim objCmdSelect As New OleDbCommand("select * from table1" & fi.Name, objConn)

                                        But I get the famous error: "the microsoft jet database engine cannot open the file. it is already opened exclusively by another user or you need permission to view its data." But neither by database is open nor its password protected. Please help me resolving this problem

                                        D Offline
                                        D Offline
                                        Dave Kreskowiak
                                        wrote on last edited by
                                        #20

                                        rajulama wrote:

                                        Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=;Data Source=" & fi.DirectoryName

                                        The names used in your code, you're trying to open a directory as a database. You have to give it the path to the .MDB file, not just it's parent directory.

                                        rajulama wrote:

                                        Dim objCmdSelect As New OleDbCommand("select * from table1" & fi.Name, objConn)

                                        On top of that, your SQL query to get the data in table1 (if it even exists in that database) is wrong. You appending the filename to the end of the SQL SELECT statement, which won't work. The filename goes in the Data Source if your connection string, not the SELECT statement.

                                        A guide to posting questions on CodeProject[^]
                                        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                             2006, 2007, 2008

                                        1 Reply Last reply
                                        0
                                        Reply
                                        • Reply as topic
                                        Log in to reply
                                        • Oldest to Newest
                                        • Newest to Oldest
                                        • Most Votes


                                        • Login

                                        • Don't have an account? Register

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • World
                                        • Users
                                        • Groups