I am trying to get the sum of total hours in a database. but while creating a new one there won't be any previous hours to calculate. Here is the following code:
private void calculateflyinghours()
{
string ConString = " datasource = localhost; port = 3306; username = root; password = 3306";
string Query = " Select sum(Flying_Hours)from aircraft." + Form1.a;
MySqlConnection ConDatabase = new MySqlConnection(ConString);
MySqlCommand cmdDataBase = new MySqlCommand(Query, ConDatabase);
MySqlDataReader myReader;
ConDatabase.Open();
myReader = cmdDataBase.ExecuteReader();
while ((myReader.Read()))
{
sum.Text = Convert.ToString(double.Parse(myReader.GetString(0)));
progressBar1.Increment(1);
}
myReader.Close();
}
I get the Following error while trying to open a new database with no data inserted :
Data is Null. This method or property cannot be called on Null values. At the
sum.Text = Convert.ToString(double.Parse(myReader.GetString(0)));
UPDATE: the code should be:
if (!myReader.IsDBNull(0)) { sum.Text = (double.Parse(myReader.GetString(0))).ToString(); }