Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET Database and Reporting > access vb.net app path


Reply
 
Thread Tools Display Modes
  #1  
Old 06-23-2008, 07:29 AM
jehanzeb jehanzeb is offline
Newcomer
 
Join Date: Jun 2008
Posts: 9
Default access vb.net app path


Dear all, as always I am back with yet again a simple question.

I have developed an application in vb.net which has VB interface as front end and access 2003 as backend. The application path which I setup initially is :


Code:
Dim MyConn As ADODB.Connection
            MyConn = New ADODB.Connection
            MyConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= C:\Lensesdb.mdb;"
MyConn.open()
here the access file is refering to C drive however, I want it to be within Application folder so that when I create the application, the file should be installed and run directly from the application directory and not from C drive.

I have added the file into the compilation and it created the application (exe) fine however when I install the program and run it, it asks for the C:\Lensesdb.mdb file (says its missing) whereas the application file is within application folder. I have to copy the file from the application folder to C drive to make it work.

I have found few ways online:

1st Example

Code:
    Public Function App_Path() As String
        Return System.AppDomain.CurrentDomain.BaseDirectory()
    End Function    Public Function App_Path() As String
        Return System.AppDomain.CurrentDomain.BaseDirectory()
    End Function
2nd Example
Code:
Private Function GetAppPath() As String
Dim i As Integer
Dim strAppPath As String
strAppPath = System.Reflection.Assembly.GetExecutingAssembly.Location()
i = strAppPath.Length - 1
Do Until strAppPath.Substring(i, 1) = "\"
i = i - 1
Loop
strAppPath = strAppPath.Substring(0, i)
Return strAppPath
End FunctionPrivate Function GetAppPath() As String
Dim i As Integer
Dim strAppPath As String
strAppPath = System.Reflection.Assembly.GetExecutingAssembly.Location()
i = strAppPath.Length - 1
Do Until strAppPath.Substring(i, 1) = "\"
i = i - 1
Loop
strAppPath = strAppPath.Substring(0, i)
Return strAppPath
End Function
I did bring them into my coding but I don't know how to refer to my application path when I type
Code:
MyConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= C:\Lensesdb.mdb;"MyConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= C:\Lensesdb.mdb;"
how can I declare the the path so that I don't have to manually put it into the C drive of destination pc each time I install the application.

I hope I made some sense here

Regards

Jehanzeb
Reply With Quote
  #2  
Old 06-23-2008, 02:59 PM
lebb's Avatar
lebb lebb is offline
Disillusioned Code Poet

Retired Moderator
* Guru *
 
Join Date: Apr 2002
Location: Tennessee, USA
Posts: 12,808
Default

Just concatenate the path into the string:
MyConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & System.IO.Path.Combine(GetAppPath(), "Lensesdb.mdb") & ";"

Incidentally, all your code samples seem to be pasted twice. I'm guessing they aren't actually that way in your code.
__________________
Laura

Ita erat quando hic adveni.
Reply With Quote
  #3  
Old 06-24-2008, 04:51 AM
jehanzeb jehanzeb is offline
Newcomer
 
Join Date: Jun 2008
Posts: 9
Default

many thanks for your kind reply Laura. however, this didn't work. [no i dont have code twice in my application but for somehow it was added twice in this post]

I have even tried
Code:
MyConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & Application.StartupPath & "\Lensesdb.mdb;"
but this didnt work either.

One thing which I noticed under my Project (under Settings) It says

Name: LensesdbConnectionString
Type: Connection String
Scope: Application
Value: Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Lensesdb.mdb


does this make any difference when I built a setup file? Should this be any different?

I do add the mdb file when building an application (using VS2005 wizard) and it does appear under the compiled folder. In addition to that when I install the application on a client machine the mdb file appears on the installed application folder however, when i run the application it comes with error asking me to put the file under C:\ drive.

When i do that it works fine.

Regards

Jehanzeb

Last edited by jehanzeb; 06-24-2008 at 04:58 AM.
Reply With Quote
  #4  
Old 06-24-2008, 06:26 AM
lebb's Avatar
lebb lebb is offline
Disillusioned Code Poet

Retired Moderator
* Guru *
 
Join Date: Apr 2002
Location: Tennessee, USA
Posts: 12,808
Default

Sounds like you've got something still using the hard-coded string instead of the one you're generating.

Incidentally, why are you still using legacy ADO instead of ADO.NET, in a .NET application?
__________________
Laura

Ita erat quando hic adveni.
Reply With Quote
  #5  
Old 06-24-2008, 07:59 AM
jehanzeb jehanzeb is offline
Newcomer
 
Join Date: Jun 2008
Posts: 9
Default

Hello Laura,

that is the only method I know since vb6 I use the same ado method. How do you use ado.net method?

I have checked the coding again and I dont have any ado code in my codes - only three places.

1: When to add the record into access database
2: when to delete a record from access database
3: when to delete all records from access database

these are the three places I have used the ado. Rest is all dataset and datagrid. I tried dataset option but I don't know how to keep the record within dataset and then show it onto the datagrid.
The above 3 options I need to do with my application. So when a user open the application all the records stored should be shown and also when a user deletes a record it needs to be deleted.

I don't have any particular reason to use access database, if it works with dataset alone I'll use it however, when I tried with dataset it shows the current entries which are done in the dataset but when an application is closed the information is lost and it starts from the fresh dataset. i want it to keep the data until user deletes it manually.

Can I achieve this with dataset? what code would I need to make it work?

Many thanks

Regards

Jehanzeb

Last edited by jehanzeb; 06-24-2008 at 08:08 AM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->