Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi all,

I have labels "Hardwork" and 1 datagirdview display when load form. I use the code below to do the quantity comparison in the column "TotalTime". I want if the value is in column "TotalTime"> = 30 then labels "Harwork" + 1

but not run.the result is: specified cast is not valid.

Please, help me fix it

What I have tried:

C#
public void BtnSearch_Click(object sender, EventArgs e)

       {
           db = new DbConnector();
           lbTotal.Text = "00";

           db.fillDataGridView("select *from tbl_WorkLoad where TimeComplete Between'" + dateTimePicker1.Value.ToString("dd-MM-yy| HH:mm:tt") + "' and '" + dateTimePicker2.Value.ToString("dd-MM-yy| HH:mm:tt") + "'", dataGridView1);

           const string HardWorkLabelText = "Hard Work Count: {0}";
           const int HardWorkThreshold = 30;

           try
           {
               IEnumerable<DataGridViewRow> rows = dataGridView1.Rows.Cast<DataGridViewRow>().Where(r => ((Int32)r.Cells["TotalTime"].Value) >= HardWorkThreshold);

               lbHardwork.Text = string.Format(HardWorkLabelText, rows.Count());

               {
                   for (int i = 0; i < dataGridView1.Rows.Count; i++)
                   {
                       lbTotaltime.Text = (Convert.ToString(double.Parse(lbTotaltime.Text) + double.Parse(dataGridView1.Rows[i].Cells[7].Value.ToString())));

                  
           }
           catch (Exception ex)
           {

               MessageBox.Show(ex.Message.ToString());
           }
       }
Posted
Updated 25-Oct-20 20:51pm
Comments
BillWoodruff 26-Oct-20 2:20am    
You have already asked this question and received answers with working code, and suggestions on how to analyze why your codes does not work,. Why are you reposting.?
Sơ Mi Tv 26-Oct-20 2:32am    
but I got a problem specified cast is not valid. I think there is a need to convert int32 =>??? i don't know :(
Sơ Mi Tv 26-Oct-20 2:37am    
My suggestion is as follows:
You can count the HardworkCount inside the for loop only... and display it in label after the for loop –

Is this right?

1 solution

That code won't even compile, much less give casting errors - there is a spurious open curly bracket before the for loop that has no matching closing curly bracket, and the closing curly bracket at the end of the end of the loop is also missing.

I'd suspect that if similar code was executed the casting error is down to the date format you are passing to your DB: you should never concatenate strings to form SQL expressions, but pass values as parameters using appropriate datatypes: passing a date and time should mena using a DateTime value as a parameter so the DB doesn't have to try and work out what date format you are using (and normally get it wrong).
 
Share this answer
 
Comments
Sơ Mi Tv 26-Oct-20 3:09am    
thanks for suggestion
BillWoodruff 26-Oct-20 11:16am    
fyi: this is a repost, and i already supplied the OP with verified working code, and ideas about how to debug his code: https://www.codeproject.com/Answers/5283430/Count-label-and-compare-number-in-Csharp

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900