2

I getting this error when I try to connection to sql server and I dont understand why. Here is the error:

Error: The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception.
Stack Trace: at System.Data.SqlClient.SqlConnection..ctor()
             at System.Data.SqlClient.SqlConnection..ctor(String connectionString)

Here is my code:

connectionString = "Data Source=localhost\SQLEXPRESSSERVER;Initial Catalog=TEST_DATABASE;User ID=USERNAME;Password=PASSWORD;"

        Dim myConnection As New SqlConnection(connectionString)
        myConnection.Open()
Will
  • 1,084
  • 5
  • 20
  • 42

2 Answers2

1

Try this connection string

connectionString = "Data Source=localhost\\SQLEXPRESSSERVER;Initial Catalog=TEST_DATABASE;User ID=USERNAME;Password=PASSWORD;"

You should escape your backslash \

Anyway, examine the inner exception for more details, as they usually provide additional information

saamorim
  • 3,855
  • 17
  • 22
1

I don't think the problem has anything to do with the connection string...

According to this, you probably have an error in your app.config or web.config...

Edit

Also, you should confirm if your code is throwing an innerexception along with the exception... Usually the real cause of the exception, will be in the innerexception...

Community
  • 1
  • 1
bastos.sergio
  • 6,684
  • 4
  • 26
  • 36