13 Replies - 444 Views - Last Post: 05 March 2018 - 03:28 AM Rate Topic: -----

#1 alobi   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 180
  • Joined: 21-January 13

Could not find file 'C:\Vb.Net Secondary\SecSchool\bin\Debug\Secon

Posted 02 March 2018 - 12:23 PM

I could not figure out why I am getting this error message. When run the code it will crash at the line
".connection.cn
Additional information: Could not find file 'C:\Vb.Net Secondary\SecSchool\bin\Debug\SecondaryDb.accdb'.


 Private ReadOnly _builder As New OleDbConnectionStringBuilder With
            {
                .Provider = "Microsoft.ACE.OLEDB.12.0",
                .DataSource = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SecondaryDb.accdb")
            }
        Public Function GetCourses() As List(Of Course)

            Dim courses As New List(Of Course)

            Using cn As New OleDbConnection With {.ConnectionString = _builder.ConnectionString}
                Using cmd As New OleDbCommand With
                    {
                        .Connection = cn,
                        .CommandText = "SELECT Id, CourseName FROM Courses ORDER BY CourseName"
                    }


Is This A Good Question/Topic? 0
  • +

Replies To: Could not find file 'C:\Vb.Net Secondary\SecSchool\bin\Debug\Secon

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14101
  • View blog
  • Posts: 56,522
  • Joined: 12-June 08

Re: Could not find file 'C:\Vb.Net Secondary\SecSchool\bin\Debug\Secon

Posted 02 March 2018 - 12:27 PM

What happens when you put a breakpoint on that line, and see if the connection object is fully filled out and open?

13                       .Connection = cn,

Was This Post Helpful? 0
  • +
  • -

#3 alobi   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 180
  • Joined: 21-January 13

Re: Could not find file 'C:\Vb.Net Secondary\SecSchool\bin\Debug\Secon

Posted 02 March 2018 - 02:37 PM

For some reason vb is not let me put a break point at "connection=cn"
But here is another thing the very connecection object works
 Dim connstr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\VB.Net Secondary\SecondaryDb.accdb;Persist Security Info=False"

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14101
  • View blog
  • Posts: 56,522
  • Joined: 12-June 08

Re: Could not find file 'C:\Vb.Net Secondary\SecSchool\bin\Debug\Secon

Posted 02 March 2018 - 02:38 PM

Then put a breakpoint above and step through the code until you get there.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • RequestedRangeNotSatisfiable
  • member icon

Reputation: 6554
  • View blog
  • Posts: 26,569
  • Joined: 12-December 12

Re: Could not find file 'C:\Vb.Net Secondary\SecSchool\bin\Debug\Secon

Posted 03 March 2018 - 01:44 AM

You have two different file locations there, which one is the correct one? Is the file found at that location? Do they both fail? Are you trying to obtain a distributable, embedded, database?

With your number of posts you should be able to, by this time, provide a clear description and question.
Was This Post Helpful? 0
  • +
  • -

#6 alobi   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 180
  • Joined: 21-January 13

Re: Could not find file 'C:\Vb.Net Secondary\SecSchool\bin\Debug\Secon

Posted 03 March 2018 - 10:35 AM

The file only fails at the location below, I am trying to move away from the other file location.
For your last question Are you trying to obtain a distributable, embedded, database? I am not sure what that means at first but I read up on it and I think the answer is yes. But if you can explain that more it will be appreciated. I have commented out that file location and it still fails at the same spot.I have used F8 with the breakpoint it does not show anything. Thanks for your help
Public Function GetCourses() As List(Of Course)

	         Dim courses As New List(Of Course)

	           Using cn As New OleDbConnection With {.ConnectionString = _builder.ConnectionString}
	               Using cmd As New OleDbCommand With
	                   {
	                       .Connection = cn,
	                       .CommandText = "SELECT Id, CourseName FROM Courses ORDER BY CourseName"
	                   }

Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • RequestedRangeNotSatisfiable
  • member icon

Reputation: 6554
  • View blog
  • Posts: 26,569
  • Joined: 12-December 12

Re: Could not find file 'C:\Vb.Net Secondary\SecSchool\bin\Debug\Secon

Posted 03 March 2018 - 02:15 PM

Quote

The file only fails at the location below,

There is no location mentioned in the referenced code, so this aspect remains unclear.

I cannot recall the steps and restrictions for attempting to embed an Access database, you can search ".net embed access database" or similar. It is a poor option though, SQL Sever Compact Edition or SQLite are better options.



Anyway, just concerning your current error, and just to get past it, I would stick the database in the Documents folder and provide a fixed file path to it. "Could not find file" should be fairly easy to resolve (generally). Then I would consider the alternative database options.
Was This Post Helpful? 0
  • +
  • -

#8 alobi   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 180
  • Joined: 21-January 13

Re: Could not find file 'C:\Vb.Net Secondary\SecSchool\bin\Debug\Secon

Posted 03 March 2018 - 02:44 PM

I have an update.I Created another form frmtesting2, the program is now failing with same message but this time at the line where it says Cn.Open()
I am also able to put a break point at cn.open() and when I place a mouse over Cn, it says "cn system.Data.oledb.oledbconnection" I will go and try your last suggestion.
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw   User is offline

  • RequestedRangeNotSatisfiable
  • member icon

Reputation: 6554
  • View blog
  • Posts: 26,569
  • Joined: 12-December 12

Re: Could not find file 'C:\Vb.Net Secondary\SecSchool\bin\Debug\Secon

Posted 03 March 2018 - 02:51 PM

Note that it is a little distracting when the code stops on cn.Open because it is at this point that all of the connection details are resolved to attempt the connection. Before this, the connection string is just some inert text.
Was This Post Helpful? 0
  • +
  • -

#10 alobi   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 180
  • Joined: 21-January 13

Re: Could not find file 'C:\Vb.Net Secondary\SecSchool\bin\Debug\Secon

Posted 03 March 2018 - 11:53 PM

Here is more information on this error, the db file is actually here "C:\Vb.Net Secondary\SecondaryDb.accdb"
  Public Shared Function getConnection() As String
        Return ConfigurationManager.ConnectionStrings("SecondaryDbConnectionString").ConnectionString
    End Function

All calls to the file location works fine. I only started having this problem when I tried to use the one below
            Private ReadOnly _builder As New OleDbConnectionStringBuilder With
            {
                .Provider = "Microsoft.ACE.OLEDB.12.0",
                .DataSource = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SecondaryDb.accdb")
            }
        Public Function GetCourses() As List(Of Course)

            Dim courses As New List(Of Course)

            Using cn As New OleDbConnection With {.ConnectionString = _builder.ConnectionString}
                Using cmd As New OleDbCommand With
                    {
                        .Connection = cn,
                        .CommandText = "SELECT Id, CourseName FROM Courses ORDER BY CourseName"
                    }

                    cn.Open()

Was This Post Helpful? 0
  • +
  • -

#11 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1503
  • View blog
  • Posts: 3,802
  • Joined: 01-February 13

Re: Could not find file 'C:\Vb.Net Secondary\SecSchool\bin\Debug\Secon

Posted 04 March 2018 - 01:25 PM

Are you placing the database file in the same folder that your application's exe file is located in?

Where did you come up with using the below code to get the path to the file?
.DataSource = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SecondaryDb.accdb")


Have you actually tested this path by showing it in a messagebox so you can see the path it creates?

You should do that because, I think you may not be understanding what path the code will give you by using the "AppDomain.CurrentDomain.BaseDirectory". Check it and see if that is actually where you have the "SecondaryDb.accdb" file. Place this in your application and test it.
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim test As String = IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SecondaryDb.accdb")
        MessageBox.Show(test)
    End Sub


Was This Post Helpful? 1
  • +
  • -

#12 alobi   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 180
  • Joined: 21-January 13

Re: Could not find file 'C:\Vb.Net Secondary\SecSchool\bin\Debug\Secon

Posted 05 March 2018 - 02:21 AM

I think you may not be understanding what path the code will give you by using the "AppDomain.CurrentDomain.BaseDirectory". You are correct and I suspected that. I got the code online, it is my first time using it. I tried to read up on it but did not get much.

How do I solve it, the db file is at this location "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\VB.Net. Secondary\SecondaryDb.accdb and I will prefer in the absence of any compelling reason. I have over twenty form opening it from this location. When I ran your test the is what I get " C:\Vb.Net Secondary\SecSchool\bin\Debug\Secondary.accdb"
Al
Was This Post Helpful? 0
  • +
  • -

#13 andrewsw   User is offline

  • RequestedRangeNotSatisfiable
  • member icon

Reputation: 6554
  • View blog
  • Posts: 26,569
  • Joined: 12-December 12

Re: Could not find file 'C:\Vb.Net Secondary\SecSchool\bin\Debug\Secon

Posted 05 March 2018 - 03:15 AM

You cannot use AppDomain.CurrentDomain.BaseDirectory because that references a location that does not contain the file. Nor can you use an alias for a known folder, such as Documents, as the file is at C:\VB.Net. Secondary\SecondaryDb.accdb. Just use this fixed (absolute) location with your connection string.

Quote

You are correct and I suspected that. I got the code online, it is my first time using it. I tried to read up on it but did not get much.

It is obviously a bad idea to use, and attempt to modify, code that you do not understand. You need to continue to research the code until it makes some sense to you, or perhaps start with some simpler code that you do understand.
Was This Post Helpful? 1
  • +
  • -

#14 andrewsw   User is offline

  • RequestedRangeNotSatisfiable
  • member icon

Reputation: 6554
  • View blog
  • Posts: 26,569
  • Joined: 12-December 12

Re: Could not find file 'C:\Vb.Net Secondary\SecSchool\bin\Debug\Secon

Posted 05 March 2018 - 03:28 AM

(You should also avoid putting dots in a folder name.)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1