I am beginning to redesign an ASP application into ASP.NET and since I'm new, I was wondering what is the difference between storing my database connection strings in the "web.config" file or just storing them in the normal session variables?
For example in Web.config it would be:
<AppSettings>
<add key="namehere" value="SERVER=SERVER1;User ID=userid;PASSWORD=password;DATABASE=DatabaseName" />
</AppSettings
and stored inside a session variable it would be:
"DRIVER=SQL Server;SERVER=SERVER1;User ID=userid;PASSWORD=password;DATABASE=DatabaseName"
Now lets say a user's session times out, normally this would mean the user would lose the connection, but does the key value in the web.config file ever expire? If anyone can help me it would be appreciated.
Thanks in advance.
NickWeb.config settings are static, and are the same for all sessions of the webapplication. A session variable is kept in the session memory. It can differ for every session, can be modified, but is not persistent (if session is lost, value is gone)
So what you are saying is that it would be best to place the connection string in the web.config file so that it will never be lost throughout a user using my application?
Also, I am getting an error whenever I try to reference this key I placed in the AppSettings of my Web.Config file below:
-----
<AppSettings>
<add key="dataconn" value="SERVER=SERVER1;User ID=userid;PASSWORD=password;DATABASE=DatabaseName" />
</AppSettings>
-----
Do I need the part of the value to have "DRIVER=Sql Server" or not? That's the only thing I can see that would be causing this error, unless anyone can give me a clue. I am referencing this key in my code-behind file as followed:
------
Dim MyConnection As New System.Data.SqlClient.SqlConnection(ConfigurationSettings.AppSettings("dataconn"))
------
Thanks for your quick reply earlier.
Nick
Hi,
connectionstrings can be foundhere.
Grz, Kris.
I appreciate the help; however, I am still having problems referencing my web.config file. In the web.config file I have the following code:
----------
<configuration>
<AppSettings>
<add key="dataconn" value="SERVER=SERVER;User ID=UserID;PASSWORD=password;DATABASE=Database"/>
</AppSettings
<system.web>
----------
When I run this code, I get an error immediately that will not even let me load my project. I am confused on why this is NOT working due to the fact that I am after the configuration tag, yet I have placed the AppSettings before the system.web tag? Any thoughts?
If I comment out the enter AppSettings tag, I am able to load my page with no problem.
My goal in all of this is to reference the key I create in the AppSettings tag as followed:
----------
'Dim MyConnection As New System.Data.SqlClient.SqlConnection(ConfigurationSettings.AppSettings("dataconn"))
----------
However, I am left with hardcoding the connectionstring in there for now as followed:
----------
Dim MyConnection As New System.Data.SqlClient.SqlConnection("SERVER=SERVER;User ID=UserID;PASSWORD=password;DATABASE=Database")
----------
Any ideas? I am attempting to connect to a SQL Server 2000 database which SQL authentication which has been verified. Any help would be greatly appreciated.
Thanks in advance.
Nick
Your <AppSettings> node is using the incorrect case. It should be <appSettings> instead.
Dave,
Thanks a million. I'd of never figured that out. I appreciate your help. Have a good one!
Nick
0 comments:
Post a Comment