Is it possible to insert a connection string into a web.config file at runtime, if it isn't there? Would this work?
The connection string is for a ASP.NET user login and create account database.
How do you mean "insert?" If you mean can you manually edit web.config: yes, you can. You'd add an entry for connectionStrings (if there wasn't one) an then a child node for your specific connectionString.
If you mean can you do it in code: theoretically yes. However, it is normally bad practice, and a pain the rear.
Yes it is very possible:
<connectionStrings>
<add name="yourNameHere" connectionString="connectiongStringHere" providerName="nameHere"/>
</connectionStrings>
inside ConfigSections
Put a default connectionstring in your web.config and replace the username/password and database name at runtime as desired.
Related
I use ASP.NET for web and Oracle for the database. For communicating between ASP.NET and Oracle I use ODP.NET. I have specified the connection string in web.config file as such:
<connectionStrings>
<add name="main" connectionString="Data Source=AGAPUSTEST; User Id=agapus;Password=pswd;"/>
</connectionStrings>
When I try to establish connection using this connection string I get "Integrated Security is an invalid connection string attribute" error. As you can see I have not specified this attribute in the configuration file. So at some point this attribute probably gets added automatically.
I used to have the connection string hard coded and I didn't have any problems at all. Do you have any ideas how to solve this?
Luckily I have found the solution. I'd provided the web config content but I had not given you the code I use to access the connection string from code behind. Here's the code:
string conString = ConfigurationManager.ConnectionStrings[0].ConnectionString;
As I was suspecting Integrated Security attribute being added automatically I was almost sure that some other connection string was retrieved. I tired changing ConnectionStrings[0] to ConnectionStrings["main"] and voila, it worked. So although there's only one connection string in the configuration file, the 0th one is not the one I needed. When I displayed the 0th connection string look what I got:
data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
I'm sure you guessed where it comes from, from the machine.config file. So to make everything clean and safe I've once more been convinced that accessing by name is most of the time a better idea.
Looks like they are talking about this problem here:
https://community.oracle.com/thread/585813?tstart=405
Seems the solution could be to use User Id=/ in the connection string and see if that works
Also make sure to have your sqlnet.ora have "SQLNET.AUTHENTICATION_SERVICES = (NTS)"
I am trying to read some image data from my sql database, i have it writing; all i need to do is get it reading. But i am getting the error stated in the question on line 18? Can somone please help me.
Here is the code:
http://codepad.org/d3Yd702T
Do you have a connection string named RevisionConnectionString in your web.config file?
In your web.config file, add the following in the <configuration> section:
<connectionStrings>
<add name="RevisionConnectionString" connectionString="whatever it should be for you;" />
</connectionStrings>
Property ConfigurationManager.ConnectionStrings gets the ConnectionStringsSection data for the current application's default configuration.
A ConnectionStringsSection object contains the contents of the configuration file's connectionStrings section.
And configuration file is your web.config, which means that in it you should have section where is stated conn string as a relation to data source.
So, first check is there a connection string, and if is, check is name written properly.
Line 18 shows this:
string strConnection = ConfigurationManager.ConnectionStrings["RevisionConnectionString"].ToString();
Which can only mean that you don't have a ConnectionString with key RevisionConnectionString
We can store Connection String in Web.config file in two ways One is
<connectionStrings>
<clear/>
<add name="LocalSqlServer"
connectionString="Data Source=(local);Initial Catalog=aspnetdb;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Other One is
<appSettings>
<add key="ConnectionString"
value="server=localhost;database=Northwind;uid=sa;password=secret;" />
</appSettings>
Now I want to know
What is difference between these two approach?
Which one is better way?
What are their limitations?
UPDATE:Can you explain that <connectionString> has any significant advantage over <appSetting>?
The connectionStrings section is dedicated to connection strings and was only introduced in .NET 2.0.
appSettings is more general and is to be used for other application settings.
You should use the connectionStrings section, as it can also be encrypted separately from any other settings.
The first approach can be accessed directly by some data controls like SQLDataSource.
Using the connectionStrings element would be the most appropriate way to handle connection strings. The appSettings element is how connection strings used to be handled before .NET 2.0. You can use either approach but it is probably easier to work with multiple connection strings if you use the connectionString element. With multiple connection strings stored in appSettings you would have to parse each name (or value) to work out if it is a connection string before you could use it. This leads to maintenance issues. It's easier to just check if all of the connectionString items are present.
If you add your connection strings to the appSettings section, you need to manually retrieve them using the ConfigurationManager.AppSettings.Get(key) method.
By adding your connection strings instead to the connectionStrings element, .NET can automatically find these by name when you create your connection object.
connection-string section is declare the connection for system.I mean that you application know that you string is connectionString. If you will use appSettings for application it is just any string value.
Besides the benefits mentioned in the other answers, connectionStrings elements have a providerName attribute that appSettings elements do not. This is particularly useful if your data source is not SQL Server.
I am trying to publish a website using ASP.NET MVC3 EF and CODEFIRST with a SQL Server 2008 backend. On my local machine I was using a sql express db for development, but now that I am pushing live, I want to use my hosted production database. The problem is that when I try to run the application, it is still using my local db connection string. I have completely removed the old connection string from my web.config file and am using the <clear /> tag before creating the new connection string. I have also cleaned the solution and rebuilt, but somehow it is still connecting to the old db. What am I missing?
This is the new connection string:
<connectionStrings>
<clear />
<add name="CellularAutomataDBContext"
connectionString=" Server=XXX;
Database=CellularAutomata; User ID=XXX; Password=XXX; Trusted_Connection=False"
providerName="System.Data.SqlClient" />
</connectionStrings>
UPDATE
When I debug and look at the DBCONTEXT object, this is what is showing up for its connection:
Data Source=.\\SQLEXPRESS;Initial Catalog=CellularAutomata.Models.D1K2N3CARuleDBContext;Integrated Security=True;MultipleActiveResultSets=True"
I am unsure why this is happening because I cannot find it being set to this anywhere. Also, under configuration it says LazyLoadingEnabled = true, I assume this may be part of the problem, maybe it is not loading the new connection string. Where do I change these parameters?
UPDATE 2
EFCodeFirst is using a default connection string, I can't figure out how to get it to accept the connection string that I specify in the web.config file.
So, When using EF CodeFirst, there is a default connection string that it uses. If you want to be able to use a custom connection string, there are a few parameters guidelines that you must follow.
name ="this must match the name of your database context class"
connectionString="Server=yourserverurl; Database=yourdatabasename; User ID=youruserid;
Password=yourpassword; Initial Catalog=the name of the database to use;
Trusted_Connection=False"
providerName="System.Data.SqlClient"
So far this is working for me.
The connectionString you show is not an EF connection string. The EF won't use it. So you're changing the wrong thing.
An EF connectionString will include providerName="System.Data.EntityClient"
It will look for the same name as your context and depending on what else you
are using other names as well. I usually use the following for controlling
specific features with either the same or specific connection strings
(I keep app services in a different db for example so EFCF can drop tables as needed):
<connectionStrings>
<add name="MyAppContext" .../>
<add name="ApplicationServices" .../>
<add name="DefaultConnection" .../>
</connectionStrings>
I have set up a small SQL Server database for users to login and also create accounts. There is a problem with the connection string though. Whenever I use the connection string below in the web.config file I get a server error page and cannot view the website. However when I take out this connection string I am able to view the website albeit the database doesn't work. Any ideas would be greatly appreciated.
<configuration>
<appSettings/>
<connectionStrings>
<addname="ConnectionString" connectionString="Server=.\SQLEXPRESS;Database=medicale_Members;User ID=user_admin;Password=medicalmembers;"/>
<connectionStrings/>
<system.web>
Unsure if it's a typo in your question, but you need to ensure your connection string element looks like:
<add name
rather than
<addname
Ensure you close your element with
</connectionStrings>
Change it to:
</connectionStrings>
Also as another person mentioned. Change the add to:
<add name ... />