connection string in web config

Hi
I need to find out how to write my connection sting in my web config file.
I use OleDb for MS Access and C#.
I have been running my application locally, so my connection string has the Data Source:
Data Source=""C:\Inetpub\wwwroot\Phone\Database\directory.mdb"";
and it is in the code behind file.

Now how do I write the connection string for the database when the application sits on the web server?
And how do I use the settings from the web config file in the code behind?

Help is much appreciated  :-)

Regards
KH
edligAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mmarinovCommented:
Hi edlig,

try this
<add key="AccessConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=XXX;"  />
in your code behind use this

OleDbConnection con = null;

con = new OleDbConnection(_ConfigurationSettings.AppSettings["AccessConnectionString"].Replace("XXX", Server.MapPath("..\\Database\\directory.mdb")));

Regards!
B..M
mmarinov
0
edligAuthor Commented:
Thanks, but I get the error message:  Not a valide file name. So I guess my path is wrong..?
Would it be possible in some way to set the path in the web config file though, or do I have to add that in the code behind?

Is the Server.MapPath etc a general string or will that work only on the server?

Sorry if I am asking really blond questions...

Regards
KH
0
mmarinovCommented:
it will work on every machine :) it is a part of the .NET

you can debug your program and see what Server.MapPath returns

B..M
mmarinov
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

edligAuthor Commented:
When I debug, this is the error:

Server Error: Unable to load database!System.Data.OleDb.OleDbException: Not a valid file name. at System.Data.OleDb.OleDbConnection.ProcessResults(Int32 hr) at System.Data.OleDb.OleDbConnection.InitializeProvider() at System.Data.OleDb.OleDbConnection.Open() at Phone.Room.fillOfficeDropDown() in c:\inetpub\wwwroot\phone\room.aspx.cs:line 110

line 110: connection.Open();

Kh
0
edligAuthor Commented:
ups... a bit more awake this morning...

it worked when I put \\ instead of \

But can I set the connection string in the web config file? so I don't have to change it in all the different aspx.cs files?

Thanks for you help :-)

KH
0
mmarinovCommented:
Of course you can set it
then you will have
con = new OleDbConnection(ConfigurationSettings.AppSettings["AccessConnectionString"]);
and in the web config replace
XXX with the path to the file

B..M
mmarinov
0
edligAuthor Commented:
Tried
<appSettings>
<add key="connectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Inetpub\wwwroot\Phone\Database\directory.mdb;" />
</appSettings>

and it works... but

How do I use Server.MapPath in the web config file though?

KH
0
mmarinovCommented:
you don't have to use it. i suggested to use this method if you want to make your application to search a certain file within the working folder and no other subfolder
so because the using directly path in the web.config - i think your problem is resolved

B..M
mmarinov
0
edligAuthor Commented:
yes, but i would like to use it :-) if possible
because it does not work sitting on the server if I have the direct path..
sorry to keep bothering...
0
mmarinovCommented:
sorry, but i didn't understand your last post - waht actually you want to achieve ?

B..M
mmarinov
0
edligAuthor Commented:
well, I woudl like to set the whole connection path in the web config file.
something like this:
<appSettings>
<add key="connectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Server.MapPath("Database\\directory.mdb") />
</appSettings>

this path works here
con = new OleDbConnection(_ConfigurationSettings.AppSettings["AccessConnectionString"].Replace("XXX", Server.MapPath("Database\\directory.mdb")));

but how do I set it in web config?
0
mmarinovCommented:
you can not do that programatically so easy
so i would not suggest to do that

B..M
mmarinov
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
edligAuthor Commented:
Ok :-)

Thank you for all your help!!

KH
0
cluster1Commented:
how to set server.mappath in web config
0
vdjoshiCommented:
Please Use this in the Web.Config Application Settings file and Add a Key in that.



<add key="ConStr"
value=" User ID= ;password= ;Server=;Port=;" />

Pls try it and It would definitely work.

0
wtconwayCommented:
I'm only familiar with VB.NET in ASP.NET but I've done this very thing in several files. You can just set the variable in the codebehind file. Have you done any reading on that or considered that at all?
0
sohamrajCommented:
Hi,

<appSettings>
       <add key="connection_string" value="Database=dbname;Data Source=localhost;User Id=root;Password=sa;"></add>
  </appSettings>

But you can't edit string at rumtime, provide a such interface if any change in string you can update.


0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.