This is my function:
public int gtCountCertificatByExercice()
{
DataTable resDataTable = new DataTable();
DBConnection dbConnection = new DBConnection();
string query = "SELECT COUNT(id) AS nb_cert " +
"FROM crs_certificat " +
"WHERE id_exercice = " + IdExercice + " ";
NpgsqlConnection conn = dbConnection.Conn;
NpgsqlCommand cmd = conn.CreateCommand();
cmd.CommandText = query;
Int32 nbCertByExercice = 0;
try
{
conn.Open();
NpgsqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
nbCertByExercice = reader.GetInt32(0);
}
MessageBox.Show("" + nbCertByExercice);
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
MessageBox.Show(ex.Message);
}
conn.Close();
return nbCertByExercice;
}
and I always get this error: "Specified cast is not valid" !!
but when I use this:
while (reader.Read())
{
nbCertByExercice = Int32.Parse(reader["nb_cert"].ToString());
}
It works fine !!
I also have the same problem with dateTime type!!
What should I do do get directly the type of the field ?