Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Checkboxlist problem

Posted on 2010-09-02
14
Medium Priority
?
306 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

705 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