Solved

ASP.net path error

Posted on 2010-11-22
15
414 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
Comment Utility
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
Comment Utility
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
Comment Utility

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
 

Author Comment

by:murbro
Comment Utility
Hi sammySeltzer:
Where exactly do I put that code
0
 
LVL 5

Expert Comment

by:buraksarica
Comment Utility
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
Comment Utility
excatly, see if that works.

if not, there is another way
0
 

Author Comment

by:murbro
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 28

Expert Comment

by:sammySeltzer
Comment Utility
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
Comment Utility
the important thing is that you specify the full physical path of your database location.
0
 

Author Comment

by:murbro
Comment Utility
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
Comment Utility
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
Comment Utility
by the way, i don't know what   <clear /> is doing on your code.

You need to remove it
0
 

Author Comment

by:murbro
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks very much
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now