I have a login form that I wanna select the userID (which is in the form of an int) from the database, and store it as a string.
string insertQuery =
"SELECT UserID FROM Customers WHERE Email = @Email AND Password = @Password";
SqlCommand com = new SqlCommand(insertQuery, conn);
com.Parameters.AddWithValue("@Email", tbEmail.Text);
com.Parameters.AddWithValue("@Password", tbPassword.Text);
string result = (string)com.ExecuteScalar();
But after I login, I get this error:
System.InvalidCastException: 'Unable to cast object of type 'System.Int32' to type 'System.String'.
string insertQuery = "SELECT CONVERT(NVARCHAR(20),UserID) FROM Customers WHERE Email = @Email AND Password = @Password";(string)in front if it, add.ToString()behind it.intto read it. This seems like an X-Y Problem.