SqlDataSource error - The connection name ' ' was not found in the applications configuration or the connection string is empty

← PrevNext →

In this article I am going to share a solution for a very common error that occurs while using SqlDataSource control in Asp.Net. It says... The connection name ' ' was not found in the applications configuration or the connection string is empty. See the below image. It happened to me once and I solved it and I'll show you how.

SqlDataSource error - The connection name ' ' was not found in the applications configuration

The error is associated with the SqlDataSource control and may occure for various reasons.

SqlDataSource control in Asp.Net, is used to connect with a remote database. You can later bind the SqlDataSource to other controls like a GridView to display data extracted from a database table etc.

While configuring "SqlDataSource", a connection string is created within the "web.config" file. The web.config is situated in the root directory of the application. However, if in any case the connection is missing or incorrectly defined in the "web.config" file, it will throw the above error.

The "web.config" file with a proper connection string should look like this. It will have the data source, the name of the database and user credentials.

<connectionStrings>
<add name="ENCODEDNAConnectionString3" connectionString="Data Source=dna_classified;Initial Catalog=ENCODEDNA;User ID=sa;Password=;Integrated Security=True"
    providerName="System.Data.SqlClient" />
</connectionStrings>

The "SqlDataSource" on you web page should have proper connection string defined like this.

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:encodednaConnectionString3 %>" 
                
    SelectCommand="SELECT [EmpID], [EmpName], [Country], [Email] FROM [EmployeeDetails]">
</asp:SqlDataSource>

Remember, you might not see this error while working on "localhost". But we often forget (or make mistake) to configure the "web.config" file properly when we run the application on a server (live application).

🚀 Here's a suggestion. Try to assign the connection string to an "SqlDataSource" control using code behind procedure. This will ensure that important informations like user credentials and the database name etc. is not compromised.

Setting a connection string to "SqlDataSource" using code behind procedure

Code behind refers to code that is written within a separate class file in Asp.Net.

Now let us assume, this is the "SqlDataSource" that you have defined and configured. Remove the connection string from the control and assign the string using a code.

<asp:SqlDataSource ID="SqlDataSource1" runat="server"                
    SelectCommand="SELECT [EmpID], [EmpName], [Country], [Email] FROM [EmployeeDetails]">
</asp:SqlDataSource>

Code behind (C#)

protected void form1_Load(object sender, EventArgs e)
{
    // set the connection string.
    string sCon = "Data Source=dna_clasified;Persist Security Info=False;Initial Catalog=encodedna;User Id=sa;Password=;Connect Timeout=500;";
    SqlDataSource1.ConnectionString = sCon;
}

Code behind (VB)

Protected Sub form1_Load(sender As Object, e As EventArgs) Handles form1.Load
    // set the connection string.
    Dim sCon As String = "Data Source=dna_clasified;Persist Security Info=False;Initial Catalog=encodedna;User Id=sa;Password=;Connect Timeout=500;"
    SqlDataSource1.ConnectionString = sCon
End Sub
    

← PreviousNext →