-1

I have a Multiple Select DROPDOWNLIST to select items. See the Dropdownlist below

[![dropdownlist][1]][1]

What I am doing is, I am selecting 2 items from the list. One of PROCESSED and another of PENDING

So what's happening wrong here is, when the condition is PROCESSED it works properly and goes in IF condition but second time it is PENDING but still it goes in the IF condition.

using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
{
    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", conn))
    {
        SqlDataAdapter damonthyear = new SqlDataAdapter(sqcmd);
        damonthyear.Fill(dtspmonthyear);

        for (i = 0; i < dtspmonthyear.Rows.Count; i++)
        {
            if (cmbEmp_Name.SelectedItem.Text.Contains("PROCESSED") == true)
            {
                //CF.ExecuteQuerry("exec Emp_Resign_Allocate_Leave '" + str_emp_sel + "','" + dtspmonthyear.Rows[0]["month"].ToString() + "', '" + dtspmonthyear.Rows[0]["year"].ToString() + "'");
            }
            else
            {
                // not going in else for `PENDING`
            }
        }
    }
}

This is the markup:

<asp:DropDownCheckBoxes ID="cmbEmp_Name" AddJQueryReference="true" Width="60%" runat="server"
                            DataTextField="Employee Name" DataValueField="User_ID" UseSelectAllNode="false">
                            <Style DropDownBoxBoxWidth="500px" DropDownBoxBoxHeight="45%" SelectBoxWidth="55%" />
                        </asp:DropDownCheckBoxes>
Nad
  • 4,605
  • 11
  • 71
  • 160
  • You can bet that `if()` ain't broken, so it must be your code. Set breakpoints, step through your code and inspect your variables. Did you notice that you're looking at `cmbEmp_Name.` **`SelectedItem`**, so the same item every iteration? – CodeCaster Apr 29 '16 at 10:19
  • Have you tried debugging it? – Christoph K Apr 29 '16 at 10:19
  • @CodeCaster: tried debugging and checked, it's going in `IF` condition always. – Nad Apr 29 '16 at 10:19
  • what is the value of "cmbEmp_Name.SelectedItem.Text" ? –  Apr 29 '16 at 10:19
  • 2
    I bet that cmbEmp_Name.SelectedItem is always returning the same selected item (first one from all selected items).. Shouldn't you iterate through the selected items ? – Ponas Justas Apr 29 '16 at 10:21
  • @PranavPatel: the value is `GAURAV JHUNJHUNWALA-2222(March - 2016) PROCESSED` – Nad Apr 29 '16 at 10:22
  • yes then is returns true, –  Apr 29 '16 at 10:23
  • @PonasJustas: yes second time also its taking the first one. So what to do for this – Nad Apr 29 '16 at 10:23
  • If you're selecting two items won't it return true since you're only checking if it contains Pending which is true? – Peter B Apr 29 '16 at 10:23
  • cmbEmp_Name.SelectedItem.Text.Contains("PROCESSED") == true so your selected value "GAURAV JHUNJHUNWALA-2222(March - 2016) PROCESSED" contains word "PROCESSED" so it returns true –  Apr 29 '16 at 10:23
  • @PranavPatel: so how to make it check depending upon `PROCESSED` and `PENDING` – Nad Apr 29 '16 at 10:24
  • try something similar to foreach (var item in cmbEmp_Name.SelectedItems) { if (item.Text.Contains("PROCESSED") == true) { //CF.ExecuteQuerry("exec Emp_Resign_Allocate_Leave '" + str_emp_sel + "','" + dtspmonthyear.Rows[0]["month"].ToString() + "', '" + dtspmonthyear.Rows[0]["year"].ToString() + "'"); } else { // not going in else for `PENDING` } } – Ponas Justas Apr 29 '16 at 10:24
  • Also a bit redundant is saying "== true" as Contains returns a bool. Otherwise I'd try something as recomended by @Ponas – Peter B Apr 29 '16 at 10:25
  • @PonasJustas: the code is too messy to read and copy. kindly put it as an answer – Nad Apr 29 '16 at 10:26
  • @coder, what if you select value which contains "PENDING" ? –  Apr 29 '16 at 10:26
  • @PranavPatel: when I select as `PENDING` I get text as `NEHA ARUN KHANNA-2145(March - 2016) PENDING` – Nad Apr 29 '16 at 10:27
  • What kind of control is this? – CodeCaster Apr 29 '16 at 10:39
  • There is no such control in ASP.NET. If you donwloaded some control from the web, read its documentation to perform the task you want to do (_"Iterate over selected items"_?). – CodeCaster Apr 29 '16 at 10:43
  • @CodeCaster: I guess it is almost an ASP.NET control. I have downloaded its dll and I am using very much easily fine with it. I m just stucked in `iteration` part. let me know how to move further with this – Nad Apr 29 '16 at 10:45

2 Answers2

3

try something similar to:

for (i = 0; i < dtspmonthyear.Rows.Count; i++)
{
    foreach (var item in cmbEmp_Name.Items)
    {
        if (item.Selected)
        {
            if (item.Text.Contains("PROCESSED"))
            {
                //CF.ExecuteQuerry("exec Emp_Resign_Allocate_Leave '" + str_emp_sel + "','" + dtspmonthyear.Rows[0]["month"].ToString() + "', '" + dtspmonthyear.Rows[0]["year"].ToString() + "'");
            }
            else
            {
                // not going in else for `PENDING`
            }
        }
    }
}
Ponas Justas
  • 305
  • 1
  • 6
1

You apparently downloaded some control from the web. Its documentation states:

DropDownCheckBoxes is an ASP.NET server control directly inheriting from standard ASP.NET CheckBoxList control.

Cool, so we can just search the web for what we want to do: "ASP.NET CheckBoxList get selected items", which yields Q&As like How can I get the CheckBoxList selected values, what I have doesn't seem to work C#.NET/VisualWebPart, How to get values of selected items in CheckBoxList with foreach in ASP.NET C#? and so on:

foreach (ListItem item in dropDownCheckBoxList.Items)
{
    if (item.Selected)
    {
        // Do what you want to do
    }
}
Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • do I need to add this as `item.Selected == "PROCESSED")` to check for processed one ?? or as it is? – Nad Apr 29 '16 at 10:48
  • No, the `ListItem.Selected` property indicates that the `ListItem` instance is selected, as simple as that. If you want to get its text, you use `item.Text`. – CodeCaster Apr 29 '16 at 10:50
  • So, If I select one from `PENDING` also then also it will go as in `IF` condition.. – Nad Apr 29 '16 at 10:51
  • No, you need to stop copy-pasting code and try to understand what you read and paste. This code I provided gives you the items that were selected. I don't know what your code is supposed to do, but this should help you further with getting the items that were selected. It might be very possible that within this `if()` shown here, you need to write more code, but I don't know what your code is supposed to do and was frankly hoping you could figure it out from there. – CodeCaster Apr 29 '16 at 10:52
  • I was already getting the `selecteditem` the one which u provided.my main concern was I wan not able to get the items for `processed` and `pending` i tried with ur code like this also `item.Selected.ToString() == "PROCESSED"` but stil for processed it is going in `else` part... – Nad Apr 29 '16 at 10:58
  • No, you do **not** need to access `SelectedItem`, because that only returns one item while you can select multiple. I don't know why you did `item.Selected.ToString()`, because that is only going to yield "True" or "False" and never "PROCESSED". You need, inside `if (item.Selected) { ... }`, to inspect `item.Text`. Again, **stop copy-pasting and start debugging**. Set breakpoints, inspect your variables. – CodeCaster Apr 29 '16 at 10:59
  • I got the answer which I was expecting from you. The thing which u answered was already done by me. IF you go through my question u will get what I exactly wanted and where I was stucked. Upvote +1 for your documentation and logic part. – Nad Apr 29 '16 at 11:25
  • Ok, happy to help. – CodeCaster Apr 29 '16 at 11:28