Solved

ASP.net path error

Posted on 2010-11-22
15
419 Views
Last Modified: 2012-05-10
Hi

I am getting the error
'c:\windows\system32\inetsrv\Database\Contacts.accdb' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.

in the ASP.net code written below (code section)

I have saved my database to a folder called "Database" which resides in the wwwroot folder
Have I done something wring in the connection string I used below:

  <connectionStrings>
    <clear />
    <add name="WhatEverNameYouWant" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database\Contacts.accdb;"  />
 
  </connectionStrings>

Should I have the website name after "Source="
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click


        '// define a connection to the database
        Dim cn As New OleDbConnection(ConfigurationManager.ConnectionStrings("WhatEverNameYouWant").ConnectionString)

        '// define the sql statement to execute
        Dim cmd As New OleDbCommand("SELECT * FROM [Contacts]", cn)

        Try

            '// open the connection
            cn.Open()

            '// execute the sql statement
            Using reader As OleDbDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)

                While reader.Read()
                    '// this loops through all of the returned records
                    Response.Write("blah blah blah" & "<br />")

                End While

            End Using

        Catch ex As Exception
            Response.Write(ex.Message)
        Finally
            If cn.State <> ConnectionState.Closed Then
                cn.Close()
            End If
        End Try


    End Sub

Open in new window

0
Comment
Question by:murbro
15 Comments
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 34191160
That connection assumes your accessdb file is located in the
c:\windows\system32\inetsrv directory.

If you are using OleDbConnection connection, you need to specify a physical path.

You will need something like this:

DBConnection = New OledbConnection("Microsoft.ACE.OLEDB.12.0;Data Source=& Server.Mappath("Database\Contacts.accdb
") & ";"

0
 
LVL 11

Expert Comment

by:jasonduan
ID: 34191165
try change data source path in connection string to:
... Data Source = |DataDirectory|\Contacts.accdb

and put the database file inn App_Data folder
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 34191201

some quotes were missing with last post
DBConnection = New OledbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data 
Source=" & Server.Mappath("Database\Contacts.accdb") & ";"

Open in new window

0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:murbro
ID: 34191225
Hi sammySeltzer:
Where exactly do I put that code
0
 
LVL 5

Expert Comment

by:buraksarica
ID: 34191334
You should get the value of ConfigurationManager.ConnectionStrings("WhatEverNameYouWant").ConnectionString to a temporary variable, and change the "Data Source=folder\db.mdb" part with the full path using Server.MapPath

pseudo code :

Dim cs As new String = ConfigurationManager.ConnectionStrings("WhatEverNameYouWant").ConnectionString
cs = cs.Replace("Database\Contacts.accdb",Server.MapPath("Database\Contacts.accdb"))
Dim cn As New OleDbConnection(cs)
....
....

Open in new window

0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 34191459
excatly, see if that works.

if not, there is another way
0
 

Author Comment

by:murbro
ID: 34191498
Hi
If I insert the following code I get the error "End of statement expected"

      Dim cn As new String = ConfigurationManager.ConnectionStrings("WhatEverNameYouWant").ConnectionString
        cn = cn.Replace("Database\Contacts.accdb", Server.MapPath("Database\Contacts.accdb"))
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 34191553
Use this then:

Please change the bolded to suit your current setup.
 <connectionStrings>
    <clear />
    <add name="WhatEverNameYouWant" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=[b]c:\inetpub\wwwroot\FolderName\[/b]Database\Contacts.accdb;"  />
  
  </connectionStrings>

Open in new window

0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 34191566
the important thing is that you specify the full physical path of your database location.
0
 

Author Comment

by:murbro
ID: 34191609
Hi sammySeltzer:
Thanks for the help please could you how my code will change. Do I change the connection string in Web.config file too
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 34191638
This is your original web.config file:

  <connectionStrings>
    <clear />
    <add name="WhatEverNameYouWant" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Database\Contacts.accdb;"  />

Open in new window


So, instead of that, use this below:

  <connectionStrings>
    <clear />
    <add name="WhatEverNameYouWant" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\Database\Contacts.accdb;Persist Security Info=False;"  />
  
  </connectionStrings>

Open in new window


Again, depending on the location of your database file, you will need to change the path.

In other words, you can post the physical location of your file and dummy up whatever you wish to dummy up and we can put the in proper context for you.
0
 
LVL 28

Expert Comment

by:sammySeltzer
ID: 34191659
by the way, i don't know what   <clear /> is doing on your code.

You need to remove it
0
 

Author Comment

by:murbro
ID: 34193516
Hi. Thanks. If that is thew connection string then what exactly would I put in the button  click event.
I need an exact answer as I am brand new to this. Thanks
0
 
LVL 28

Accepted Solution

by:
sammySeltzer earned 500 total points
ID: 34193782
You can adding the connection directly inside the sub.


Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

       Dim cn As OleDbConnection
       Dim reader As OleDbDataReader
       Dim cmd As OleDbCommand

        Try

        '// define a connection to the database
        'Dim cn As New OleDbConnection(ConfigurationManager.ConnectionStrings("WhatEverNameYouWant").ConnectionString)

         cn=New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;data source=" & server.mappath("Database\Contacts.accdb")) & ";"
        
        '// open the connection
        cn.Open() 

        '// define the sql statement to execute
        cmd = New OleDbCommand("SELECT * FROM [Contacts]", cn)

            '// execute the sql statement
          reader = cmd.ExecuteReader

                While reader.Read()
                    '// this loops through all of the returned records
                    Response.Write("blah blah blah" & "<br />")

                End While

            End Using

        Catch ex As Exception
            Response.Write(ex.Message)
        Finally
            If cn.State <> ConnectionState.Closed Then
                cn.Close()
            End If
        End Try

    End Sub

Open in new window

0
 

Author Closing Comment

by:murbro
ID: 34199092
Thanks very much
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question