There are various ways of storing/retrieving connection details.
The connectionStrings element would be the most straightforward way to handle connection strings. The connectionStrings section is dedicated to connection strings and was introduced in .NET 2.0.
If the privacy of your connection credentials is of concern: you should use the connectionStrings section, as it can also be encrypted separately from any other settings.
In the Web.config file you can add:
<configuration>
...
<configSections>
<connectionStrings>
<add name="connectionStringName" connectionString="Data Source=(local);Initial Catalog=DBNAME;User Id=XXX;Password=XXX;" providerName="System.Data.SqlClient" />
</connectionStrings>
<configSections>
...
<configuration>
for more information on connection strings you can learn more here: https://www.connectionstrings.com/sql-server/
and in your code you can get the connection string like this:
string connectionString = System.Configuration.ConfigurationManager.
ConnectionStrings["connectionStringName"].ConnectionString;