There is a code for example:

foreach (DataRow row1 in ds.Tables[2].Rows) { if (String.IsNullOrEmpty(row1["clID"].ToString())) { // ΠΊΠ°ΠΊΠΎΠ΅ Ρ‚ΠΎ дСйствиС dict.Add("{Note}", "ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅"); break; } if (!String.IsNullOrEmpty(row1["clID"].ToString())) { // Π΄Ρ€ΡƒΠ³ΠΎΠ΅ дСйствиС dict.Add("{Note}", "Π—Π°ΠΌΠ΅Ρ‚ΠΊΠ°"); break; } } 

The result of the procedure:

 | ID | clID | +----+------+ | 1 | 1444 | | 2 | 1456 | | 3 | null | | 4 | null | 

How to cancel the 2nd if if the 1st if is executed.
The problem is that after the first condition is met, it goes to the second one and displays an error
It is necessary if in the table at least one clID is null then the 1st if was executed
if in the table all clID is not null then the second condition

  • else if ....... - Alexey Shimansky
  • Your second if is already "canceled" by the break; statement break; - Pavel Mayorov
  • @Pavel Mayorov, yes, and the first if is also satisfied and an exception was thrown - Zhandos
  • You have a mistake elsewhere, and it has nothing to do with the "disconnection of the condition" - Pavel Mayorov
  • @Pavel Mayorov, in this case, the first if is done when we go into the array for the third time and cancel. Then there was a problem if the second also satisfied our condition and also fulfilled and left an exception. And I need if in the table at least one clID is zero, then the 1st if was performed and that's all - Zhandos

3 answers 3

since the conditions here are opposite. you can replace two if with an if-else

 if (String.IsNullOrEmpty(row1["clID"].ToString())) { // ΠΊΠ°ΠΊΠΎΠ΅ Ρ‚ΠΎ дСйствиС dict.Add("{Note}", "ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅"); break; } else { // Π΄Ρ€ΡƒΠ³ΠΎΠ΅ дСйствиС dict.Add("{Note}", "Π—Π°ΠΌΠ΅Ρ‚ΠΊΠ°"); break; } 

Perhaps you should consider using the Convert.ToString function instead of calling the ToString method. If row1 ["clID"] is already null , there will be no error when using the first function, and if the second is used, there will be an exception.


Update to the question: in this case, the condition must be checked before taking action, for example using the linq: method Any

 if(ds.Tables[2].Rows.AsEnumerable().Any(dr=>String.IsNullOrEmpty(Convert.ToString(row1["clID"])))){ foreach (DataRow row1 in ds.Tables[2].Rows) { // ΠΊΠ°ΠΊΠΎΠ΅ Ρ‚ΠΎ дСйствиС dict.Add("{Note}", "ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅"); } }else{ foreach (DataRow row1 in ds.Tables[2].Rows) { // Π΄Ρ€ΡƒΠ³ΠΎΠ΅ дСйствиС dict.Add("{Note}", "Π—Π°ΠΌΠ΅Ρ‚ΠΊΠ°"); } } 
  • in this case, the second condition is also satisfied, that is, you need without else if (...) - Zhandos
  • @Zhandos, in my answer there is no else if as well as there is no second condition - Grundy
  • @Zhandos, updated the answer - Grundy 8:43 pm
  • how can this be implemented through ado.net - Zhandos
  • here the first if is not executed as id = 1 clID is not null, but I need a foreach loop to check all the lines, and the first if is executed and did not go to the second - Zhandos

You can create a boolean variable as the execution flag, in the iterations we started the flag = false, if the condition is true, and read this flag where appropriate. At the end of the loop body, reset the flag to false.

      Boolean isCond = false; foreach (DataRow row1 in ds.Tables[2].Rows) { if (String.IsNullOrEmpty(row1["clID"].ToString())) { // ΠΊΠ°ΠΊΠΎΠ΅ Ρ‚ΠΎ дСйствиС dict.Add("{Note}", "ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅"); isCond = true; break; } if (!isCond) { foreach (DataRow row1 in ds.Tables[2].Rows) { if (!String.IsNullOrEmpty(row1["clID"].ToString())) { // Π΄Ρ€ΡƒΠ³ΠΎΠ΅ дСйствиС dict.Add("{Note}", "Π—Π°ΠΌΠ΅Ρ‚ΠΊΠ°"); break; } } }