Google News
logo
ASP.NET - Quiz(MCQ)
In which file you should write the connection string, so you can access it in all the web page for same application?
A)
In Web.config file.
B)
In MasterPage file.
C)
In App_Data folder.
D)
None of the Above.

Correct Answer :   In Web.config file.


Explanation :

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.
< connectionStrings>
< 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);
conObj.Open();

// Write code according to the requirement.

conObj.Close();

Advertisement