Web.config file write connection string to access all web pages for same application - ADO.NET

Q.  Which file you should write for the connection string, so that you can access it in all the web pages for the same application?
- Published on 24 Jul 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
 

    Discussion

  • Digvijay   -Posted on 20 Oct 15
    If you write connection string in web.config file, then you can access it in all the web pages for the same application.
    I have written my connection string in web.config file as follows. You can edit the connection string according to your computer and server setting.
    <connectionStrings>

    <add name="connString" connectionString="data source=(local);initial
    catalog=Employee;integrated security=true"/>

    </connectionStrings>

    You can access this connection string in cs file as follows.
    string con= ConfigurationManager.ConnectionStrings["connString"].ConnectionString;

    SqlConnection conObj = new SqlConnection(con);
    conObj.Open();

    --------
    --------
    conObj.Close();

Post your comment / Share knowledge


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)