You must Sign In to post a response.
  • Category: .NET

    Application config file in C#.net

    How to use Application configuration file in the C#.net Application?
  • #648383
    Hi,
    this is application configuration file in which connection string is present:



    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <configSections>
    </configSections>
    <connectionStrings>
    </connectionStrings>
    <appSettings>
    <add key="connectionString" value="server = XYZ\SQLEXPRESS; Integrated Security = SSPI; Database = ABC"/>
    </appSettings>
    </configuration>



    in below code app.config file is used to connect the databse.



    connectionString = ConfigurationManager.AppSettings["connectionString"];
    sqlConnection = new SqlConnection(connectionString);
    try()
    {

    sqlConnection.Open();

    }
    Catch(Exception ex)
    {

    }


    With Regards
    Kushal

  • #648418
    You can store the Connection String in App.Config file.

    Please follow these steps:

    1) Right Click on the Windows Application -> Add -> New Item... -> Select Application Configuration File -> Ok

    2) In the newly add App.Config file, add the following entry within configuration element:


    <appSettings>
    <add key="sqlcon" value="server=.;integrated security=true;database=databasename;"/>
    </appSettings>


    3) In the form you can use the following code to read the Connection String:


    string ConnectionString = ConfigurationSettings.AppSettings["sqlcon"].ToString();


    Thanks & Regards
    Paritosh Mohapatra
    Microsoft MVP (ASP.Net/IIS)
    DotNetSpider MVM

  • #648542
    Configuration file is used to add setting related to whole application like:
    For adding connection string you can use:

    <appSettings>
    <add key="MyConnectionString" value="server = dell;integrated security=true;database=Employee;"/>
    </appSettings>


    For disabling viewstate we can use:

    <configuration>
    <system.web>
    <pages enableviewstate="false"/>
    </system.web>
    </configuration>

    Regards,
    Sugandha
    Microsoft Certified Technology Specialist
    MY Blog..

  • #648593
    connectionStrings in web.config file like this:

    <connectionStrings>
    <add name="dbconnection" connectionString="Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MyDB" providerName="System.Data.SqlClient"/>
    </connectionStrings >


    Please check below code:


    using System;
    using System.Data.SqlClient;
    using System.Configuration;
    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    //Get connection from web.config file
    string conn = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
    SqlConnection con = new SqlConnection(conn);
    con.Open();
    }
    }

    Regards
    Mr.Rajesh
    Software Engg.


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.