how to write connection string in web.config file and read from it in asp.net?

How to: Read Connection Strings from the Web.config File?

Look at below connectionstring code:


<connectionstrings>
       <add name="Your Connection string name" connectionstring="data source=server name;
        Database=database name;uid=Database Username;pwd=password;
        multipleactiveresultsets=true;" providername="System.Data.SqlClient" />
</connectionstrings>

For call connection string write the below code:


SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings
["Connection string name"].ConnectionString.ToString());

Connection string name means your connection name:

About the Author

avatar