try
{
SqlCommand sqlCommand = new SqlCommand("SELECT COUNT(*) FROM Doctor WHERE Id = @id", sqlServerConnection);
sqlCommand.Parameters.AddWithValue("@id", id.Text);
int numberOfDoctors = (int) sqlCommand.ExecuteScalar();
if(numberOfDoctors == 1)
{
Console.WriteLine("Doctor is already in database.");
}
else
{
Console.WriteLine("There is no doctor with this Id.");
}
}
catch (Exception exc)
{
Console.WriteLine(exc);
}
I have a code like this. I have an application that has a connection with SQL database. Firstly, sqlServerConnection object is defined in the code correctly. There is no problem with that object. I have a Doctor table in the database. id. Text comes from Text element that is the value user typed in. I want to be sure about whether this typed id is already in my database or not. Whichever value I type in I always see "Doctor is already in database." message in the console. When I remove WHERE clause from sqlCommand code works correctly. But when I add WHERE clause I can't track whether this user with the given id is in my database or not. Could you please help me? Thank you for your responses in advance. Have a great day :)