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 :

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 ran 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 found few ways online:
1st Example

Public Function App_Path() As String
        Return System.AppDomain.CurrentDomain.BaseDirectory()
    End Function

2nd Example

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 Function

I did bring them into my coding but I don't know how to refer to my application path when I type

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:D

Regards

J

Hi
Try Application.StartupPath, It Gets the path for the executable file that started the application, not including the executable name.

Your Code might be

MyConn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & Application.StartupPath & "\Lensesdb.mdb;"

Morning selvaganapathy,

I have tried your method however it didn't work. When I install the application and run it on a client machine it still comes up with the error message saying that the file cannot be found under C:\Lensesdb.mdb. However, When I put the file there it works absolutely fine.

I am using Visual Studio 2005 built in Setup Wizard to create setup file and yes I have added the mdb file while adding other files for the application. This though has not made any difference.

any other suggestion will be welcomed.

Many thanks

Regards

J

Hi J,
Application.StartupPath will work fine. Problem may be

> You might created One or More connection (using c:\Lensesdb.mdb) and some of them are replaced as (Application.StartupPath & "\Lensesdb.mdb"), remaining not replaced.

> Database Connection using Wizard

These are my Guesses.

Morning selvaganapathy,

The only place I can see it is declared as C:\lensesdb.mdb is under the config file and under settings.designer.vb Here is the sample code of the config file and the settings.designer.vb.

app.config
------------

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="clLabApp.My.MySettings.LensesdbConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Lensesdb.mdb"
            providerName="System.Data.OleDb" />
    </connectionStrings>
    <system.diagnostics>
        <sources>
            <!-- This section defines the logging configuration for My.Application.Log -->
            <source name="DefaultSource" switchName="DefaultSwitch">
                <listeners>
                    <add name="FileLog"/>
                    <!-- Uncomment the below section to write to the Application Event Log -->
                    <!--<add name="EventLog"/>-->
                </listeners>
            </source>
        </sources>
        <switches>
            <add name="DefaultSwitch" value="Information" />
        </switches>
        <sharedListeners>
            <add name="FileLog"
                 type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
                 initializeData="FileLogWriter"/>
            <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
            <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
        </sharedListeners>
    </system.diagnostics>
</configuration>

Settings.designer.vb
-----------------------

<Global.System.Configuration.ApplicationScopedSettingAttribute(),  _
         Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
         Global.System.Configuration.SpecialSettingAttribute(Global.System.Configuration.SpecialSetting.ConnectionString),  _
         Global.System.Configuration.DefaultSettingValueAttribute("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Lensesdb.mdb")>  _
        Public ReadOnly Property LensesdbConnectionString() As String
            Get
                Return CType(Me("LensesdbConnectionString"),String)
            End Get
        End Property
    End Class
End Namespace

what should I do to that line where it says C:\Lensesdb.mdb? Shall I delete it? or shall i change it?

I have tried changing it to " &application......... " but it didn't work, though when I typed ..........Datasource=/Lensesdb.mdb" it didnt give me any errors.

whats your thoughts on that?

I'd like to thank you for your ongoing help however, if you help me little bit more I am sure I can achieve my goal:D

many thanks

Regards

J

I think you have added the mdb file in the Setup wizard from C:\. First add the mdb file to your folder and hen you can use the Application.Startup properly as stated above. Then use the Setup Wizard to create the Setup files. This should solve the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.