[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 440
  • Last Modified:

ASP.net path error

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
Murray Brown
Asked:
Murray Brown
1 Solution
 
sammySeltzerCommented:
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
 
jasonduanCommented:
try change data source path in connection string to:
... Data Source = |DataDirectory|\Contacts.accdb

and put the database file inn App_Data folder
0
 
sammySeltzerCommented:

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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi sammySeltzer:
Where exactly do I put that code
0
 
buraksaricaCommented:
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
 
sammySeltzerCommented:
excatly, see if that works.

if not, there is another way
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
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
 
sammySeltzerCommented:
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
 
sammySeltzerCommented:
the important thing is that you specify the full physical path of your database location.
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
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
 
sammySeltzerCommented:
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
 
sammySeltzerCommented:
by the way, i don't know what   <clear /> is doing on your code.

You need to remove it
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
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
 
sammySeltzerCommented:
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
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now