Skip to content
Advertisement

Query not executing as expected, returns -1 no matter what

I’m working on a user login system for a semester final. I am using C# in Visual Studio with ADO.NET. I have a query that I use on a database table named Credentials:

I also have the string connection as

I then run the query against the table with int count = selectCommand.ExecuteNonQuery();. selectCommand is the SqlCommand object with the above query in it. No matter what, count will equal -1 even if I enter an existing username and password. I want the count variable to be 1 when someone enters in a correct username and password combo.

And then finally in the login form when the user presses login (The message box is just a placeholder for now):

Obviously this is just for a college project so don’t say anything about how this is clearly an unsafe way to log users into a system.

Advertisement

Answer

ExecuteNonQuery is not what you want to execute. It is a query. You should use ExecuteReader which returns a SqlDataReader and then you can read how many rows are returned. Or ExecuteScalar will return only one column of one row.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement