Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to connect to SQL, ERROR = ServerVersion 'conn.ServerVersion' threw an exception of type 'System.InvalidOperationException'

I'm getting an error that I can't find an answer for anywhere online. It says

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

I've blocked out some words as not to compromise our data. It's not even making the connection.

Screenshot

This connection string works in previous application version (Visual Studio 2012, .NET Framework 4.5.2 using Microsoft.Practices.EnterpriseLibrary.Data.Sql) so I know it's not the credentials. We are re-writing the old application so the new one is using Visual Studio 2015, .NET Framework 4.5.2, and System.Data.SqlClient.

Here is the web.config connection string:

<connectionStrings>
<add name="DataConnection" 
     connectionString="Data Source=mydatabaseserver\tst1;Initial Catalog=LeaseData;Persist Security Info=True;User ID=myID;Password=mypassword" 
     providerName="System.Data.SqlClient" />
</connectionStrings>

This is my c# code calling to the database using my connection string that is available in the web.config.

public static DataTable ContractDetails(string contractID, string pointOfContact)
    {
        string connString = ConfigurationManager.ConnectionStrings["DataConnection"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connString))
        {
            try
            {
                if (connString.Length > 0)
                {
                    using (SqlCommand cmd = new SqlCommand("spNameOfStoredProcedure"))
                    {
                        using (SqlDataAdapter da = new SqlDataAdapter())
                        {
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.Connection = conn;
                            da.SelectCommand = cmd;

                            cmd.Parameters.AddWithValue("@ContractID", contractID);
                            cmd.Parameters.AddWithValue("@PointOfContact", pointOfContact);
                            cmd.ExecuteNonQuery();

                            using (DataTable dt = new DataTable())
                            {
                                da.Fill(dt);
                            }
                        }
                    }
                }
            }
            catch (SystemException)
            {

            }
        }

Let me know if there is anything else that would be helpful in solving this issue!

Thanks!

like image 532
Geek Wurk Avatar asked Jan 25 '26 00:01

Geek Wurk


1 Answers

In your screenshot you are viewing the properties of the connection prior to the connection being opened by the DataAdapter. Thus the connection is closed at that point - hence the InvalidOperationException on the call to ServerVersion. See MSDN.

like image 159
strickt01 Avatar answered Jan 27 '26 13:01

strickt01



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!