I'm attempting to create a Windows application with a login system. I'm new to Windows programming and encountering an error while trying to connect to a SQL Server Database hosted locally using SSMS 2019, we encountered the error "'ServerVersion' threw an exception of type 'System.InvalidOperationException'" and error occurs when trying to access the ServerVersion property of a database connection, such as SqlConnection or MySqlConnection, without first opening the connection.
We need to ensure that the database connection is opened before accessing any properties or executing commands.
Here's an example of how to correctly handle the database connection:
using (var connection = new SqlConnection(connectionString)) { connection.Open(); // Ensure the connection is opened // Now we can safely access the properties or execute commands Console.WriteLine($"Server version: {connection.ServerVersion}"); }
Please also verify if the password for the SQL Server has not expired. I encountered the same issue due to receiving the error message 'The password of the account has expired.' In such cases, you can resolve this by unchecking the 'Enforce password expiration' option under Login Properties.
By opening the database connection before accessing the ServerVersion property or executing any commands, we can prevent the InvalidOperationException from occurring.
Please check if you connection string and here's a simple example of how to read user data from a database while ensuring the connection is opened:
using System; using System.Data.SqlClient; class Program { static void Main(string[] args) { string connectionString = "Data Source=DESKTOP-MFLCOI2;Initial Catalog=TestDb;User ID=sa;Password=pass@1234";
using (var connection = new SqlConnection(connectionString)) { connection.Open(); // Ensure the connection is opened // Perform SQL query to retrieve user data string query = "SELECT * FROM Users"; SqlCommand command = new SqlCommand(query, connection); // Execute the command and read the results SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { // Access user data from each row string username = reader["Username"].ToString(); string email = reader["Email"].ToString(); // Display user data Console.WriteLine($"Username: {username}, Email: {email}"); } // Close the reader and connection reader.Close(); } } }
Read Similar Articles
- Best Way To Replace An Object In A List Of Objects
- Solved Error: Only authentication clear text password and authentication md5 password supported for now. received 10
- What are the Advantages and Disadvantages of Indexes In Sql Server?
- [Solved]- "error: schema validation failed with the following errors: data path """" must have required property 'browsertarget'."
- How To Get Video Stream From An IP Camera Using Opencv In Python?
- Insert ,Update,Delete in MVC with Entity Framework
- [Solved]-Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.