0

I searched around stackoverflow on how to make a relative file and have tried various things and it hasn't worked, and i was hoping to see if you guys could help me out.

here is my connection sting in my web.config file:

 <add name="2007 Database  05-12-2013(Esfahanian's conflicted copy 2013-06-24)
  ConnectionString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data 
  Source=" providerName="System.Data.OleDb"/>

and here relative path in my aspx file:

<script runat="server">
    string connectionString = ConfigurationManager
    .ConnectionStrings["2007 Database 
    05-12-2013(Esfahanian's conflicted copy 2013-06-24) ConnectionString"]
   .ConnectionString + Server.MapPath("..\..\Anderson\2007 
    Database 05-12-2011 (Esfahanian's conflicted copy 2013-06-24).mdb");
</script>

And I get this error: CS1009: Unrecognized escape sequence

So what exactly am I doing wrong

Atish Kumar Dipongkor
  • 10,220
  • 9
  • 49
  • 77
foobar2023
  • 67
  • 2
  • 11
  • 1
    I wouldn't use a `'` in a connection string. And I wouldn't use an apostrophe on a directory name even though is supported just because many libraries don't handle this quite well. Try removing it and see if that helps. – Icarus Jul 18 '13 at 16:23
  • I would start by choosing a simple and short name value, which does not contain any special characters. – David Tansey Jul 18 '13 at 16:23
  • I get this error now CS1012: Too many characters in character literal – foobar2023 Jul 18 '13 at 16:25

2 Answers2

1

You aren't escaping the "\" character in your path so it is causing an error in the MapPath() method.

Change this:

Server.MapPath("..\..\Anderson\2007 Database 05-12-2011 (Esfahanian's conflicted copy 2013-06-24).mdb"

to this:

Server.MapPath(@"..\..\Anderson\2007 Database 05-12-2011 (Esfahanian's conflicted copy 2013-06-24).mdb"

or this:

Server.MapPath("..\\..\\Anderson\\2007 Database 05-12-2011 (Esfahanian's conflicted copy 2013-06-24).mdb"

Brian Maupin
  • 745
  • 4
  • 17
  • When I change it to the first method I get this error: Object reference not set to an instance of an object. – foobar2023 Jul 18 '13 at 16:45
  • Are you sure that `ConfigurationManager .ConnectionStrings["2007 Database 05-12-2013(Esfahanian's conflicted copy 2013-06-24) ConnectionString"] .ConnectionString` is returning a valid value? – Brian Maupin Jul 18 '13 at 16:55
  • Remove the part in bold and see if you still get the Object reference error: string connectionString = ConfigurationManager .ConnectionStrings["2007 Database 05-12-2013(Esfahanian's conflicted copy 2013-06-24) ConnectionString"] .ConnectionString **+ Server.MapPath("..\..\Anderson\2007 Database 05-12-2011 (Esfahanian's conflicted copy 2013-06-24).mdb");** – Brian Maupin Jul 18 '13 at 16:59
  • Ya I still get the same error, so what exactly does this mean? – foobar2023 Jul 18 '13 at 17:03
  • It means that your call to `ConfigurationManager.ConnectionStrings[...]` is not returning anything. You need to check the name of the connection string in your config file to call it correctly. If your connection string looks something like ` ` then you would access is like this `ConfigurationManager.ConnectionStrings["Target"]` – Brian Maupin Jul 18 '13 at 17:09
  • @Brian Ok so I fixed that, and now it says: Cannot use a leading .. to exit above the top directory. – foobar2023 Jul 18 '13 at 17:17
  • Now I get DB_SEC_E_AUTH_FAILED(0x80040E4D). – foobar2023 Jul 18 '13 at 17:33
  • Sounds like you've got some other deeper problems going on. You will have to submit a new question for that to be handled, but please mark this answer as correct if it solved the problem for which this question was asked. – Brian Maupin Jul 18 '13 at 17:49
0

You are using \ in your path string but thats the symbol for literal. So any char following the \ will be inturpreted as a literal like \. is not a valid char. You actualy want the literal \ the sequence for which is \\ Also @ infront of a string tells that you dont want literals.

So mappath(" needs to be mappath(@" or every \ needs to become \\

madnut
  • 124
  • 5
  • could you please explain that better – foobar2023 Jul 18 '13 at 16:28
  • When I change it to this: string connectionString = ConfigurationManager.ConnectionStrings["2007 Database 05-12-2013(Esfahanian's conflicted copy 2013-06-24) ConnectionString"].ConnectionString + Server.MapPath(@"..\..\Anderson\2007 Database 05-12-2011 (Esfahanian's conflicted copy 2013-06-24).mdb"); I get this error: Object reference not set to an instance of an object. – foobar2023 Jul 18 '13 at 16:38