In this article I will explain with an example, how to solve the following error (exception) that occurs when using the ASP.Net SqlDataSource control.
The connection name 'constr' was not found in the applications configuration or the connection string is empty.
 
 
Error
The following error (exception) occurs when using the ASP.Net SqlDataSource control.
The connection name 'constr' was not found in the applications configuration or the connection string is empty.
The connection name 'constr' was not found in the applications configuration or the connection string is empty
 
 
Cause
The above error occurs when the name of the connection string setting assigned to the SqlDataSource control is either missing or the setting name specified does match with the one specified in the ConnectionStrings section of the Web.Config file.
 
 
Solution
The solution to this problem is fairly simple i.e. you need to make sure that the name of connection string setting assigned to the SqlDataSource control is present in the ConnectionStrings section of the Web.Config file and also it is spelled correctly.
Example
The following connection string has been set in the ConnectionStrings section of the Web.Config file.
<connectionStrings>
    <add name="constr" connectionString="Data Source=.\SQL2005;Initial Catalog=Northwind;Integrated Security=true" />
</connectionStrings>
 
The name of the Connection String setting is assigned to the SqlDataSource ConnectionString property.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:constr %>" SelectCommand="SELECT TOP 10 * FROM Customers"></asp:SqlDataSource>