• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 33671
  • Last Modified:

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
0
edlig
Asked:
edlig
1 Solution
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

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