I am attempting to make a craps game, store users/game data etc. In this problem I am attempting to see if a username is already taken. But for some reason, regardless of how I try to declare my variable to take in the result of ExecuteScalar(), I always get the error "Cannot convert from nVarChar to int".
private bool CheckForUser()
{
//returns true if Username is already registered
SqlConnection connection = new SqlConnection("...");
string query = "SELECT * FROM Users WHERE USERID = @alias";
SqlCommand cmd = new SqlCommand(query, connection);
cmd.Parameters.AddWithValue("@alias", tbNewUsername.Text);
connection.Open();
Object DbResult = cmd.ExecuteScalar(); //<-ERROR
connection.Close();
DbResult = (DbResult == DBNull.Value) ? null : DbResult;
if (DbResult != null)
return true;
else
return false;
}
This was my last attempt before looking for help. Casting to int does not work, string does not work, and declaring DbResult as an object does not work. I understand why it can not be converted to int, but i do not know why it continues to try.
Thank you for any help.