none
continue if loop if failed RRS feed

  • Question

  • Hi,

    I am using the following to loop through date range and import into a database from text files. I want to know how can I tell the code IF FILE DOES NOT EXITS THEN CONTINUE TO NEXT FILE?

    for (DateTime date = last_date; date.Date <= DateTime.Today; date = date.AddDays(1))
    {
      string file_name = "H" + date.ToString("yyyy-MM-dd") + ".TXT";
    
      MySqlBulkLoader bl = new MySqlBulkLoader(sql_connection);
      bl.TableName = "processing_table";
      bl.FieldTerminator = ",";
      bl.LineTerminator = "\n";
      bl.FileName = @"C:\Temp\Attendence\" + file_name;
      bl.NumberOfLinesToSkip = 0;
    
      int count = bl.Load();
    
      string sql = "SELECT source, created_date, created_time, company, action, employee_number, employee_name, outlet, field_1, field_2, field_3, field_4 FROM processing_table";
      MySqlCommand cmd = new MySqlCommand(sql, sql_connection);
      MySqlDataReader rdr = cmd.ExecuteReader();
    
      while (rdr.Read())
      {
          SplashScreenManager.Default.SetWaitFormDescription(rdr[1] + " - " + rdr[2]);
      }
                    
      rdr.Close();
    }
    


    Friday, September 19, 2014 9:26 PM

Answers

  • if you want to tell your code by using the keyword "continue" you can try

    if (!File.Exists(@"C:\Temp\Attendence\" + file_name
    )) continue;
    if the file does not exist the code will not do the next statements and restart the loop with the next file.

    Saturday, September 20, 2014 9:29 AM
  • if (!File.Exists(bl.FileName))
    continue;
    This would solve your problem.


    Saturday, September 20, 2014 3:27 PM

All replies

  • if you want to tell your code by using the keyword "continue" you can try

    if (!File.Exists(@"C:\Temp\Attendence\" + file_name
    )) continue;
    if the file does not exist the code will not do the next statements and restart the loop with the next file.

    Saturday, September 20, 2014 9:29 AM
  • if (!File.Exists(bl.FileName))
    continue;
    This would solve your problem.


    Saturday, September 20, 2014 3:27 PM