coder coder - 7 months ago 18
SQL Question

How to go inside For Loop only if condition is PROCESSED

I have a

MultipleDropdownlist
from where a User can select multiple Items.

Now what I want is, the condition should go inside the
FOR LOOP
only if the items selected from the
dropdownlist
is
PROCESSED


Below is my code.

DataTable dtspmonthyear = new DataTable();
ObjPriCon.Open();

var selectedItemstest = cmbEmp_Name.Items.Cast<ListItem>().Where(x => x.Selected).Select(x => x.Value).ToArray();

var resulttest = String.Join(",", selectedItemstest);
var count = selectedItemstest.Length;
str_emp_sel = resulttest;

using (SqlConnection conn = new SqlConnection("ConnectionString"))
{
using (SqlCommand sqcmd = new SqlCommand("select month(a.dt_of_leave)month, year(a.dt_of_leave)year " +
"from emp_mst a where month(a.dt_of_leave) >= month(getdate())-1 and " +
"year(a.dt_of_leave)= case when month(getdate())=1 " +
"then year(getdate())-1 else year(getdate()) end " +
"and emp_card_no IN (" + str_emp_sel + ") order by emp_name", ObjPriCon))
{
SqlDataAdapter damonthyear = new SqlDataAdapter(sqcmd);
damonthyear.Fill(dtspmonthyear);



for (i = 0; i < dtspmonthyear.Rows.Count; i++)
{
CF.ExecuteQuerry("exec Emp_Resign_Allocate_Leave '" + str_emp_sel + "','" + dtspmonthyear.Rows[0]["month"].ToString() + "', '" + dtspmonthyear.Rows[0]["year"].ToString() + "'");
}
}
}

Answer

Just filter the items before putting into selectedItemstest variable.

var selectedItemstest = cmbEmp_Name.Items.Cast<ListItem>()
                        .Where(x => x.Selected && x.Value.ToLower().Contains("processed"))
                        .Select(x => x.Value).ToArray();