[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

ASP.net path error

Posted on 2010-11-22
15
Medium Priority
?
435 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:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
15 Comments
 
LVL 29

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 29

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Murray Brown
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 29

Expert Comment

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

if not, there is another way
0
 

Author Comment

by:Murray Brown
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 29

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 29

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:Murray Brown
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 29

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 29

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:Murray Brown
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 29

Accepted Solution

by:
sammySeltzer earned 2000 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:Murray Brown
ID: 34199092
Thanks very much
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

650 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