Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Checkboxlist problem

Posted on 2010-09-02
14
Medium Priority
?
308 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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 700 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 700 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

885 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