Solved

Checkboxlist problem

Posted on 2010-09-02
14
303 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

734 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