Server Error in '/' Application. The network name cannot be found

1

I am working on a Web Application in ASP.NET Framework 4.5. SQLSERVER Database is connected to the project. Everything was working fine, I have completed the project. Before publishing the project I changed the connection string to the online server database. The site was published successfully.

To do a few minor changes to the application, I changed the connection string back to the local database. Now when I run the application I get the following error:

Server Error in '/' Application.

The network name cannot be found.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ComponentModel.Win32Exception: The network name cannot be found

Source Error:

Line 49: 
Line 50:             DataTable dt = new DataTable();
Line 51:             conn.Open(); // Throws Exception Here
Line 52:             string query = "SELECT Event.StartDate, Event.title, Artist.[Stage Name] FROM Event INNER JOIN Artist ON Event.ArtistID=Artist.ID WHERE StartDate >= GETDATE() ORDER BY  CONVERT(DateTime, StartDate,101)  DESC";
Line 53:             SqlDataAdapter da = new SqlDataAdapter(query, conn);

Source File: c:\Users\Junaid\Desktop\FAST\PasoFino\Artist Management\ArtistManagementSystem\ArtistManagementSystem\Views\Portal\Dashboard.aspx.cs Line: 51

Stack Trace:

[Win32Exception (0x80004005): The network name cannot be found]

[SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)]
   System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling) +821
   System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +332
   System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) +38
   System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) +699
   System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) +89
   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) +426
   System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +78
   System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) +191
   System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +154
   System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +21
   System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) +90
   System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +217
   System.Data.SqlClient.SqlConnection.Open() +96
   ArtistManagementSystem.Views.Portal.Dashboard.showUpComingEvents() in c:\Users\Junaid\Desktop\FAST\PasoFino\Artist Management\ArtistManagementSystem\ArtistManagementSystem\Views\Portal\Dashboard.aspx.cs:51
   ArtistManagementSystem.Views.Portal.Dashboard.Page_Load(Object sender, EventArgs e) in c:\Users\Junaid\Desktop\FAST\PasoFino\Artist Management\ArtistManagementSystem\ArtistManagementSystem\Views\Portal\Dashboard.aspx.cs:20
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +95
   System.Web.UI.Control.LoadRecursive() +59
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +678

This is my Web.Config:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <system.web>
    <sessionState mode="InProc" timeout="20"></sessionState>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" maxRequestLength="1048576"/>
    <pages>
      <controls>
        <add assembly="TimePicker" namespace="MKB.TimePicker" tagPrefix="cc1"/>
        <add tagPrefix="ajaxtoolkit" namespace="AjaxControlToolkit" assembly="AjaxControlToolKit"/>
      </controls>
    </pages>

    <!--<authentication mode="Forms">
      <forms loginUrl="Views/Login/Login.aspx" defaultUrl="Views/Portal/Dashboard.aspx">
        <credentials passwordFormat="Clear">
          <user name="junaid" password="fastian123"/>
        </credentials>
      </forms>
    </authentication>
    <authorization>
      <deny users="?"/>
    </authorization>-->
  </system.web>
  <connectionStrings>
    <!--<add name="ArtistManagementSystemEntities" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=REDDEVIL\SQLEXPRESS;initial catalog=ArtistManagementSystem;persist security info=True;user id=sa;password=fastian123;MultipleActiveResultSets=True;App=EntityFramework&quot;"
      providerName="System.Data.EntityClient" />
    <add name="ArtistManagementSystemConnectionString" connectionString="Data Source=REDDEVIL/SQLEXPRESS;Initial Catalog=ArtistManagementSystem;Persist Security Info=True;User ID=sa;Password=fastian123;MultipleActiveResultSets=True;Application Name=EntityFramework"
      providerName="System.Data.SqlClient" />-->
    <add name="ArtistManagementSystem" connectionString="Data Source=REDDEVIL/SQLEXPRESS;Initial Catalog=ArtistManagementSystem;User ID=sa;Password=***********" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
  <appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
  </appSettings>
</configuration>

This is where I am getting the error: (Dashboard.aspx)

protected void showUpComingEvents()
    {
        //string myConnection = dbController.connectionString;
        string myConnection = dbController.connectionString;

        SqlConnection conn = new SqlConnection(myConnection);

        DataTable dt = new DataTable();
        conn.Open();
        string query = "SELECT Event.StartDate, Event.title, Artist.[Stage Name] FROM Event INNER JOIN Artist ON Event.ArtistID=Artist.ID WHERE StartDate >= GETDATE() ORDER BY  CONVERT(DateTime, StartDate,101)  DESC";
        SqlDataAdapter da = new SqlDataAdapter(query, conn);
        da.Fill(dt);
        conn.Close();

        if (dt.Rows.Count > 0)
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

    }

This is the dbController Class, it only has a connection string. (I know this is not a good approach but this was just to get things working)

dbController.cs :

public class dbController
{
    //public static string connectionString = "Data Source=SQL5027.myASP.NET;Initial Catalog=DB_A0C908_nbrewer0210;User Id=DB_A0C908_nbrewer0210_admin;Password=fastian123";
    public static string connectionString = "Data Source=REDDEVIL/SQLEXPRESS;Initial Catalog=ArtistManagementSystem;User ID=sa;Password=***********";
}

Note: I would like to mention a few things:

1) I can connect to the database using SqlServer Management Studio.

2) In Services, SQL Server (SQLEXPRESS) and SQL Server Browser are running.

3) In SQL Server Configuration Manager, In Client Protocols, TCP/IP & Named Pipes is enabled.

c#
asp.net
sql-server
visual-studio
sql-server-2008
asked on Stack Overflow Oct 25, 2016 by el323 • edited Oct 25, 2016 by el323

1 Answer

0

I had same problem. My server name was wrong in connection string.

answered on Stack Overflow Sep 22, 2019 by Phantom

User contributions licensed under CC BY-SA 3.0