Solved

Checkboxlist problem

Posted on 2010-09-02
14
305 Views
Last Modified: 2012-05-10
Hi,

I'm using the code below to get the values of all the checkboxlist on my page .


Thanks in advance

else if (cntrl.GetType() == typeof(CheckBoxList))
                {
                    if (!string.IsNullOrEmpty(((CheckBoxList)cntrl).Text))
                    {
                        ids = ((CheckBoxList)cntrl).ID;
                        value = ((CheckBoxList)cntrl).Items.ToString();
                    }
                }


The problem I’m having is if I had a checkbox list called chkage
using the above method how do I add the selected values to 
the code below
  if (!string.IsNullOrEmpty(ids))
                {
                    Session[id] = value;

                    if (Mapp.ContainsKey(ids))
                    {
                        //now get the db parameter name
                        string newValue = Mapp[ids];

                        if (!criteria.ContainsKey(newValue))
                            criteria.Add(newValue, value);

                    }
                }

Open in new window

0
Comment
Question by:cjJosephj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
14 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 33590284
string s = "";
                for (int i = 0; i < chkage.Items.Count; i++)
                    if (chkage.Items[i].Selected)
                         s = s + chkage.Items[i].Text + ";";    //do something with your selection            
               
0
 
LVL 6

Expert Comment

by:nipunu
ID: 33594425
// Get Values from CheckBoxList
String values = "";
for (int i=0; i< cbl.Items.Count; i++)
{
        if(cbl.Items[i].Selected)
        {
                values += cbl.Items[i].Value + ",";
        }
}
                       
values = values.TrimEnd(',');
0
 

Author Comment

by:cjJosephj
ID: 33595475
I used the code below but i get 2,3, but i need 2,3
              else if (cntrl.GetType() == typeof(CheckBoxList))
                {
                    if (!string.IsNullOrEmpty(((CheckBoxList)cntrl).Text))
                    {
                        id = ((CheckBoxList)cntrl).ID;
                        
                    foreach (ListItem item in ((CheckBoxList)cntrl).Items)
                    {
                        if (item.Selected)
                        {
                            value += item.Value + ",";  
                     
                    
                        }
                    }
                   
                    
                  }
                }

Open in new window

0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 6

Expert Comment

by:nipunu
ID: 33595623
Use the following after the foreach loop

value = value.TrimEnd(',');

Example :

          foreach (ListItem item in ((CheckBoxList)cntrl).Items)
          {
                        if (item.Selected)
                        {
                            value += item.Value + ",";  
                     
                   
                        }
           }


0
 
LVL 6

Accepted Solution

by:
nipunu earned 175 total points
ID: 33595629
Sorry take this as the example


Example :

          foreach (ListItem item in ((CheckBoxList)cntrl).Items)
          {
                        if (item.Selected)
                        {
                            value += item.Value + ",";  
                     
                           
                        }
           }

           value = value.TrimEnd(',');

           //your code
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 33596828
Here is a utility function you can use to append strings with any delimiter
 public string AddString(string aBaseString, string aAppend, string aDelimiter)
        {
            if (String.IsNullOrEmpty(aBaseString))
                return aAppend;
            else
                return aBaseString + aDelimiter + aAppend;
        }
in your code
foreach (ListItem item in ((CheckBoxList)cntrl).Items)
                    {
                        if (item.Selected)
                        {
                            value = AddString(value, item.Value + ",");  
                     
                   
                        }
                    }
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 33596837
my mistake
foreach (ListItem item in ((CheckBoxList)cntrl).Items)
                    {
                        if (item.Selected)
                        {
                            value = AddString(value, item.Value, ",");          
                   
                        }
                    }
0
 

Author Comment

by:cjJosephj
ID: 33597731
Thanks guys the only problem is my query expects an int but now it is getting an interger

so i'm getting the error message
Conversion failed when converting the varchar value '2,3' to data type int.

I have increased the points to 350
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 33599007
I assume your query is using this information in a where clause like

where fieldname = value

consider parsing values using a function and instead have the query do something like

 where fieldname in (select values from int_values(value))

Let me know if you need a function to parse value
0
 

Author Comment

by:cjJosephj
ID: 33599149
Not sure what changes to apply

I have this in my query
 @AgeID     varchar(100) = '',


                                                             CASE
                        WHEN @AgeID = 0 THEN 1
                        WHEN AgeID in (@AgeID) THEN 1
                        ELSE 0
                  END = 1


Thanks
0
 
LVL 32

Assisted Solution

by:Ephraim Wangoya
Ephraim Wangoya earned 175 total points
ID: 33599523
Here is a function to split the values. I copied it from a previous post

In your query, you can use it as
When AgeID in (select Value from GET_INT_TABLE(@AgeID) )..

CREATE FUNCTION [dbo].[GET_INT_TABLE]
(
      @STRINPUT VARCHAR(MAX)      
)
RETURNS @INTTABLE TABLE
(
      VALUE INT
)
AS
BEGIN
      DECLARE @insertvalue varchar(32)
      DECLARE @pos INT
      DECLARE @nextpos INT
      DECLARE @valuelen INT

      SELECT @pos = 0, @nextpos = 1
      WHILE @nextpos > 0
      BEGIN
            SELECT @nextpos = charindex(',', @STRINPUT, @pos + 1)
            SELECT @valuelen = CASE
                              WHEN @nextpos > 0 THEN
                                    @nextpos
                                          ELSE
                                    len(@STRINPUT) + 1
                                   END - @pos - 1
         
        set @insertvalue = (substring(@STRINPUT, @pos + 1, @valuelen))                      
        if IsNumeric(@insertvalue) = 1
        begin
                  INSERT @INTTABLE (VALUE)
                  VALUES (@insertvalue)
            end
            SELECT @pos = @nextpos
      END
           
      RETURN      
END
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 33599730
Remove this line from the query

 WHEN @AgeID = 0 THEN 1

so you will only have
 CASE
       When AgeID in (select Value from GET_INT_TABLE(@AgeID)) then 1
       ELSE 0
     
0
 

Author Comment

by:cjJosephj
ID: 33600427
Thanks for your help.
I have the query beloW

Which has the value 2,3 as @AGEID  but i still get the error message

Conversion failed when converting the varchar value '2,3' to data type int.


CASE
   When AGEID in (select Value from GET_INT_TABLE(@AGEID)) 
then 1 
  ELSE 0 
END = 1

Open in new window

0
 

Author Comment

by:cjJosephj
ID: 33601385
Thanks got it to work
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question