I have an database that I need to validate a user input against. I can't figure out the syntax for the while loop I'm using to validate the input.
Expecting:
/*
User inputs "Hondas"
Console checks database if "Hondas" exists.
If it doesn't exist, prompt user again.
If it does exist, continue w/ program.
*/
Current Code:
// Open a connection to MySQL
conn = new MySqlConnection(cs);
conn.Open();
//Declare DataReader
MySqlDataReader rdr = null;
// Form SQL Statement
string stm = $"select count(*) from vehicle where make = \"{sqlInput}\"";
// Prepare SQL Statement
MySqlCommand cmd = new MySqlCommand(stm, conn);
rdr = cmd.ExecuteReader();
// Output Results
while (rdr.Read() && rdr.GetInt32(0) <= 0)
{
Console.Clear();
Console.WriteLine("This make doesn't exist in the database, try again.");
sqlInput = Console.ReadLine();
// I believe I'm supposed to have some connection info here.
}
This successfully validates however the loop doesn't work properly. It only runs one time if I have multiple Incorrect
entries. I'm not terribly familiar with how the connection should look. Thanks for the help.