I am building a windows service with ASP.NET C#. I am trying to make a call from a MySQl database using MySqlCommand object. I try to retrieve data from a table called tbl_patient in my database. When I pass the following query to my MySQlCommand object:
SELECT ID, ID_Institution, AdresseDomiciliaire, PersonneDeContact, Telephone, nom, prenom, lastSyncDate, DateModified FROM tbl_patient
it works, meaning that I retrieve the data, however, when I change the query to that one:
SELECT ID, ID_Institution, AdresseDomiciliaire, PersonneDeContact, Telephone, nom, prenom, lastSyncDate, DateModified FROM tbl_patient WHERE lastSyncDate < DateModified
I keep getting the error: Input string is not in the correct format, I guess the error is due to these datetime fields in the tbl_patient when the datareader is trying to read the rows returned by the query, Can somebody help me with this issue?
Here is the full code snippet of my method which tries to get the records from tbl_patient for a better understanding of my situation, I add it with the query which throws exception:
public static List<Tbl_Patient> getPatients()
{
List<Tbl_Patient> patients = new List<Tbl_Patient>();
string query = "SELECT ID, ID_Institution, AdresseDomiciliaire, PersonneDeContact, Telephone, nom, prenom, lastSyncDate, DateModified FROM tbl_patient WHERE lastSyncDate < DateModified"; // This query passed as cmdText in my MySqlCommand object throws exception
MySqlConnection connection = DBUtils.GetDBConnection();
connection.Open();
MySqlCommand cmd = new MySqlCommand(query, connection);
MySqlDataReader dataReader = cmd.ExecuteReader();
//Read the data and store them in the list
try
{
while (dataReader.Read())
{
Tbl_Patient patient = new Tbl_Patient();
patient.Id = (long)(dataReader["Id"]);
patient.ID_Institution = Convert.ToInt64(dataReader["ID_Institution"]);
patient.AdresseDomiciliaire = dataReader["AdresseDomiciliaire"].ToString();
patient.PersonneDeContact = dataReader["PersonneDeContact"].ToString();
patient.Telephone = dataReader["Telephone"].ToString();
patients.Add(patient);
}
}
catch(Exception e)
{
Console.WriteLine(e.Message); // Here is where the exception comes from
}
dataReader.Close();
connection.Close();
return patients;
}
I guess the problem was with my MySQl Connector because I see that the dataReader has rows (by logging in console with Console.WriteLine(dataReader.HasRows), it displays true), The datareader just could not read the rows because some fields, lastSyncDate, DateModified was of type datetime; What I do is remove MySQL.data.dll that I have imported manually in my project via reference in my Solution Explorer and import the latest version of MySQl connector with nuget package. After importing the latest version of MySQl connector, I retrieve my data.