Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ServerVersion = 'conn.ServerVersion' threw an exception of type 'System.InvalidOperationException'

Tags:

c#

.net

sql

I've seen this error answered for other people, but I can't seem to get it to work. I'm trying to connect to a SQL Server Database project locally.

I'm getting this error thrown on Open() when I do the follow:

SqlConnection conn = null;
conn = new SqlConnection(connection)
conn.Open()

Where connection is a connection string passed from my webconfig. I've found examples online and tried these three connection strings and I get the same error every time:

<add name="TICKETING_CONNECTION" connectionString="Server=(local); DataBase=Ticketing_DB; Integrated Security=SSPI"/>

<add name="CONN"
connectionString="Data Source=localhost; Integrated Security=SSPI;Initial Catalog=Ticketing_DB"
providerName="System.Data.SqlClient" />

<add name="CONN2"
connectionString="Data Source=(local);Initial Catalog=Ticketing_DB;"
providerName="System.Data.SqlClient"
/>

Could the problem be stemming from the path of where the database is located on my machine? I've tried copying the c:\ path but didn't have any luck. I've seen some examples online use a .mdf file, but my project does not have this file either.

This is my first time trying to connect to a database project, so any help or advice would be greatly appreciated

like image 496
JDeffo Avatar asked Feb 06 '23 11:02

JDeffo


1 Answers

If you are using Visual Studio try going to the Server Explorer and Right click on Data Connections and Add new connection by selecting DataSource as Microsoft SQL Server and provider as .net framework provider for SQL Server. Under the server name check for the SQL Server if it is listed with your machine name. Then try to provide the database details and user name and password for SQL Authentication or select windows for windows authentication. Then do a test connection. If it is fine on click of OK it will add the data connection. Right click the properties of data connection which is newly added and navigate to properties to extract the connection string. Compare it with yours. This can help you troubleshoot the issue.

like image 142
Baskar Rao Avatar answered Feb 08 '23 00:02

Baskar Rao