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
- [Solved]-"failed to set the 'value' property on 'htmlinputelement': this input element accepts a filename, which may only be programmatically set to the empty string."
- Instead Of Delete Trigger In Sql Server With Example
- Solve Error: Exception of type 'Microsoft.TeamFoundation.Git.Contracts.GitCheckoutConflictException' was thrown
- [Solved]-How to Upload PDF File using jquery MVC?
- Clustered and Non Clustered Index In Sql server With Real Example
- DDL Triggers In Sql Server with Example | Database and Server Scoped Triggers
- How to Bind Data in DataList Control in Asp.Net – C#
- [Solved] Cannot implicitly convert type 'Microsoft.AspNetCore.Mvc.BadRequestObjectResult'
- Wh to mAh Conversion Calculator | Wh to mAh Conversion Formula