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()

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for responding. Tried the backslash and still unsuccessful.
Does the exception has an innerexception? Is so, what is the error?
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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.