Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I implemented a method to insert the record based on some condition using foreach loop as outer as well as inner loop. please have a look into below code...


C#
try
            {
                dbManager = new DBManager(DataProvider.SqlServer, Constants.ConnectionString);
                dbManager.ConnectionString = Constants.ConnectionString;
                dbManager.ProviderType = DataProvider.SqlServer;

                dbManager.Open();
                dbManager.Command.CommandTimeout = Constants.CommandTimeOut;
                dbManager.Command.CommandText = "DELETE dbo.PAYROLL_EMPLOYEE_LEAVES_SETUP WHERE EMP_NO='" + iObjects.EMP_NO + "'";
                dbManager.Command.Connection = dbManager.Connection;
                int _id = dbManager.Command.ExecuteNonQuery();


                if (!string.IsNullOrWhiteSpace(leavetypeid))
                {
                    string[] arrRay = leavetypeid.Split(',');
                    foreach (var id in arrRay)
                    {
                        if (!string.IsNullOrWhiteSpace(id))
                        {
                            iObjects.LEAVE_TYPE_ID = Convert.ToInt32(id);

                            //for leave types
                            if (!string.IsNullOrWhiteSpace(leavetypeid))
                            {
                                string[] aray_ltype = leavetype.Split(',');
                                foreach (var id_ltype in aray_ltype)
                                {
                                   if (!string.IsNullOrWhiteSpace(id_ltype))
                                    {
                                     iObjects.TOTAL_LEAVES = float.Parse(id_ltype);
                                    }
                                 }
                              }
                            sql = @"SET NOCOUNT ON INSERT INTO dbo.PAYROLL_EMPLOYEE_LEAVES_SETUP
                                (EMP_NO ,LEAVE_TYPE_ID,TOTAL_LEAVES,IS_ACTIVE,IS_DELETED,USER_ID,IP,CHECK_SUM,TIME_STAMP,SYSTEM_ID,SITE_ID)
                                VALUES ('{0}',{1},{2},'{3}', 0 ,'{4}', '{5}','{6}',GETDATE(),{7},'{8}') ";
                                //
                                sql = string.Format(sql,
                                    Helper.IsNull(iObjects.EMP_NO)
                                    , Helper.NullIf<int>(iObjects.LEAVE_TYPE_ID)
                                    , Helper.NullIf<float>(iObjects.TOTAL_LEAVES)
                                    , Helper.NullIf<bool>(iObjects.IS_ACTIVE)
                                    , Helper.IsNull(iObjects.USER_ID)
                                    , Helper.IsNull(iObjects.IP)
                                    , Helper.IsNull(iObjects.CHECK_SUM)
                                    , Helper.NullIf<int>(iObjects.SITE_ID)
                                    , Helper.IsNull(iObjects.SYSTEM_ID));


                                //
                                sql = sql.Replace("''", "NULL").Replace("'NULL'", "NULL").Replace("NNULL", "NULL");

                                dbManager.Command.CommandTimeout = Constants.CommandTimeOut;
                                dbManager.Command.CommandText = sql;
                                dbManager.Command.Connection = dbManager.Connection;
                                int _Count = dbManager.Command.ExecuteNonQuery();


                        }
                    }
                }
            }
            catch (Exception Exp)
            {
                dbManager.Command.Transaction.Rollback();
                throw Exp;
            }
Posted
Updated 29-Oct-15 4:05am
Comments
Krunal Rohit 29-Oct-15 10:08am    
Use break; in inner loop.

-KR
Maciej Los 29-Oct-15 10:16am    
Sounds like an answer ;)
Meer Wajeed Ali 29-Oct-15 10:27am    
I already tried with break but it jumped to finally block. please sugest where exactly I have to write and thanks..
Maciej Los 29-Oct-15 10:30am    
Please, improve you question and provide information about leavetypeid object. How it looks like?
johannesnestler 29-Oct-15 10:33am    
which finally? please show all relevant code, and just tell us where you want't to jump (out) on which condition. Break brings you out of the inner most Loop (which you should know or be able to read on MSDN). Otherwise use setting a flag variable (some boolean), and check inside the Loop if your flag variable signals to "jump out", or on any other (outer) loop level.

That function is faaaaar too long, and does too many different things. Split it into appropriate smaller functions, and the solution arises automatically.
C#
if (!string.IsNullOrWhiteSpace(leavetype))
                            {
                                string[] aray_ltype = leavetype.Split(',');
                                foreach (var id_ltype in aray_ltype)
                                {
                                   if (!string.IsNullOrWhiteSpace(id_ltype))
                                    {
                                     iObjects.TOTAL_LEAVES = float.Parse(id_ltype);
                                    }

                                 }
                             }
can be refactored to
C#
private bool TryGetLeaveTypeId(string leaveType, out float leaveTypeId)
{
    leaveTypeId = 0f;
    if (!string.IsNullOrWhiteSpace(leavetype))
                            {
                                string[] aray_ltype = leavetype.Split(&#39;,&#39;);
                                foreach (var id_ltype in aray_ltype)
                                {
                                   if (!string.IsNullOrWhiteSpace(id_ltype))
                                    {
                                     leaveTypeID  = float.Parse(id_ltype);
                                    }
                                 }
                             }
    return leaveTypeID != 0;
}
then inserted into your function etc.
 
Share this answer
 
From your question, you are looking for break
https://msdn.microsoft.com/en-us/library/ttw7t8t6.aspx[^]
https://msdn.microsoft.com/en-us/library/adbctzc4.aspx[^]

But as other comments state, your code may have other problems.
To understand why your code don't do what you want, you should use a debugger to follow step by step your code, inspect variable and see why it is doing what it does.
 
Share this answer
 
Hi,

We can achieve this by setting a flag. Below is the code

C#
List<string> test=new List<string>();
            test.Add("Hi");
            test.Add("Hello");
foreach (string item in test)
{
               foreach (string item2 in test)
               {
                   if(//Your condition here)
                   {
                      flag = true; 
                      break;
                   }
               }

               if (flag) break;//if true exit from outer loop as well else continue
 }


I hope this resolves your issue..

Thanks,
-Siva
 
Share this answer
 
v4
I think the reason of your trouble is this piece of code:
c3
if (!string.IsNullOrWhiteSpace(leavetypeid))
{
    string[] arrRay = leavetypeid.Split(',');
    foreach (var id in arrRay)
    {
        if (!string.IsNullOrWhiteSpace(id))
        {
            iObjects.LEAVE_TYPE_ID = Convert.ToInt32(id);

            //for leave types
            if (!string.IsNullOrWhiteSpace(leavetypeid))
            {
                string[] aray_ltype = leavetype.Split(',');


You should replace a second if with:
C#
if (!string.IsNullOrWhiteSpace(leavetype))



I'd suggest to read about SpringSplitOptions[^], which may help you to avoid empty entries:
C#
string leavetypeid = "a,b,c,,d,e,f,,,g";

var lids = leavetypeid.Split(new char[]{','}, System.StringSplitOptions.RemoveEmptyEntries);

foreach (var l in lids)
{
    Console.WriteLine("{0}", l);
}

Produces:
a 
b 
c 
d 
e 
f 
g 

Instead of:
a 
b 
c
NULL
d 
e 
NULL
NULL
f 
g


Got it?
 
Share this answer
 

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