Web.config file you should write connection string - ADO.NET
Q. In which file you should write the connection string, so you can access it in all the web page for same application?- Published on 16 Jun 15
a. In App_Data folder.
b. In Web.config file.
c. In MasterPage file.
d. None of the above.
ANSWER: In Web.config file.
Following are the steps to create the connection string in Web.config file and access it in code behind file. In connection string data source and database name you have to provide according to your sqlserver instance and their database name.
Step 1: Create the connection string in Web.config file as follows.
< add name="con" connectionString="Data Source =ServerName; initial catalog= DataBaseName; Integrated Security=true"/>
Step 2: In code behind file write the following code.
ConfigurationManager class is available in System.Configuration namespace;
string conStr= ConfigurationManager.ConnectionStrings["con"].ConnectionString;
SqlConnection conObj = new SqlConnection(conStr);
// Write code according to the requirement.