Error Connecting to Database

Hi Experts,

When my connection.open() is excuted, i got this error. i am using .NET 2.0 to connect to a database in SQL2005. Kindly Advice! Many thanks!!!!

System.Data.SqlClient.SqlException was unhandled
Message="An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"
Source=".Net SqlClient Data Provider"
ErrorCode=-2146232060
Class=20
LineNumber=0
Number=53
Server=""
State=0
StackTrace:
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Connect(Boolean& useFailoverPartner, Boolean& failoverDemandDone, String host, String failoverPartner, String protocol, SqlInternalConnectionTds connHandler, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, SqlConnection owningObject, Boolean aliasLookup)
at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.SqlClient.SqlConnection.Open()
at ClientEdition.FrmMainApp.isVisitor(String nric) in C:\Projects\SOS\App\ClientEdition\ClientEdition\FrmMainApp.cs:line 91
at ClientEdition.FrmMainApp.CheckPassport(String nric) in C:\Projects\SOS\App\ClientEdition\ClientEdition\FrmMainApp.cs:line 56
at ClientEdition.FrmMainApp.btnRegister_Click(Object sender, EventArgs e) in C:\Projects\SecurityOperatingSystem\App\ClientEdition\ClientEdition\FrmMainApp.cs:line 27
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.PerformClick()
at System.Windows.Forms.Form.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.TextBoxBase.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.Control.PreProcessMessage(Message& msg)
at System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg)
at System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)
at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at ClientEdition.Program.Main() in C:\Projects\SOS\App\ClientEdition\ClientEdition\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()


Answer this question

Error Connecting to Database

  • SSIS - newbie

    hi darren.simcc

    can u please post the connection string which u r using...

    and also make sure that u are authorized to use Sql Server


  • GabrielMartin

    I can connect to it using SQL Server Management Studio and the SQL Server is stored on the local computer. The SQL Server 2005 is the developer Edition. I am developing on WindowsXP Professional.
  • WienerSchnitzel

    SQL Server 2005 Developer edition disables Remote Connections by default. You can enable it using SQL Server Surface Area Configuration (SSAC) tool. Follow these steps:

    1. Launch SSAC from  Programs/Microsoft SQL Server 2005/Configuration Tools/SQL Server Surface Area Configuration

    2. Click on "Surface Area Configuration for Services and Connections"

    3. On the left hand side treeview, under "Database Engine", select "Remote Connections"

    4. Choose "Local and remote connections" on the pane on right side.

    5. Press OK.

    No try your code again.


  • elledibbi

    Two things to note:
    1. Named Pipes does not work when configured as the only clientconnectivity option for ASP.NET running in default circumstances. Havea look at the following article:

      BUG: Named pipes do not work when workerprocess runs under ASPNET account
      http://support.microsoft.com/default.aspx scid=kb;en-us;Q315159
      Check that you are able to use TCP/IP connections instead
    2. Create a test file on your PC named "connection.udl" - it's just an empty text file. Run this file, and set the configuration parameters inside it to create a connection string. Perform a test connection after you have finished entering database details and connnection parameters. When you close the dialog, the udl file will now contain a connection string. Remove the "provider=" parameter, and you can then use it with a SqlConnection's ConnectionString parameter.



  • Error Connecting to Database