Solved

Displaying to Listbox or CheckBoxList Coma Separated Values from single column database

Posted on 2009-06-29
5
2,741 Views
Last Modified: 2013-11-26
Hi guys,

I'm designing an app to store our staff IT preferences and came up a formView with Listboxes and checkboxlist to store multiple values. The PropertyCheckBoxList that has 2 checkboxes (Windows and Linux) while IntereststListBox has many items (C#, VB.NET, Java, Perl, PHP, Python). I figured I would store the values in 2 columns only: one for Interests and one for  Property as comma separated values for ease of use after looping through each control selection. ie column Interests (VB.NET, Java)

On FormView Insert mode: It works fine - see code in the FormView insert click event below
On FormView Read mode: How do I retrieve these comma separated values to display them back in the Property CheckboxList and InterestListBox ?

Thanks in advance for your kind and helpful attention,
Best regards,

Jeewai

Protected Sub InsertButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        'Get reference to controls	
Dim PropertyCheckBoxList As CheckBoxList = CType(FormView1.FindControl("TabContainer1").FindControl("TabPanel2").FindControl("PropertyCheckBoxList"), CheckBoxList)
 Dim InterestsListListBox As ListBox = CType(FormView1.FindControl("TabContainer1").FindControl("TabPanel8").FindControl("InterestsListListBox"), ListBox)
 
 'Implement string method to list all selected values
 Dim sqlproperty As String = String.Empty
Dim sqlp As String = String.Empty
       For i As Integer = 0 To PropertyCheckBoxList.Items.Count - 1
            If PropertyCheckBoxList.Items(i).Selected = True Then
                sqlproperty += PropertyCheckBoxList.Items(i).Text & ", "
            End If
        Next
        sqlp = sqlproperty 
Dim sqlinterests As String = String.Empty
        Dim sqlint As String = String.Empty
        For i As Integer = 0 To InterestsListListBox.Items.Count - 1
            If InterestsListListBox.Items(i).Selected = True Then
                sqlinterests += InterestsListListBox.Items(i).Text & ","
            End If
        Next
        sqlint = sqlinterests
 
        'Set insert parameters in datasource
ObjectDataSource1.InsertParameters("Property").DefaultValue = sqlp
ObjectDataSource1.InsertParameters("InterestsList").DefaultValue = sqlint

Open in new window

0
Comment
Question by:jeewai
[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
  • 3
5 Comments
 
LVL 15

Accepted Solution

by:
Solar_Flare earned 250 total points
ID: 24740675
you can take your list of comma separated values, split them, and then loop through the checkboxlist items and tick them if appropriate.

string prefs = "Java, C#, Ruby";
 
foreach (ListItem i in CheckBoxList1.Items)
{
   foreach (string s in prefs.Split(','))
   {
      if(i.Value == s)
      {
         i.Selected = true;
         break;
      }
   }
}

Open in new window

0
 
LVL 12

Assisted Solution

by:CmdoProg2
CmdoProg2 earned 250 total points
ID: 24744532
After you spilt the values, use the findbyvalue method to set the listitiem.


 Public Sub FindPeference()
    Dim preferences As String = "VB.NET,Java,ASP.NET"
    Dim li As ListItem
    Dim preference() As String = preferences.Split(",")
    'Clear any previous setting
    CheckBoxList1.SelectedIndex = -1
    For Each pref As String In preference
      li = CheckBoxList1.Items.FindByValue(pref)
      If li Is Nothing = False Then li.Selected = True
    Next
  End Sub

Open in new window

0
 

Author Comment

by:jeewai
ID: 24744968
Thanks Solar_Flare and CmdProg2 for your input

Ok, findbyvalue seems direct, yet can you be more detailed and explain how do I open a connection to the database (code behind) and pass the stored values from my colum (ex Java, ASP.NET) to a string. Should the code be fired up in the page events so that the FormView read mode elements automatically get the specific selections ? I'm still kinda new with all these procedures and the most descriptive you are the quicker I will understand the business logic. :-)

Thanks in advance for your time
0
 

Author Comment

by:jeewai
ID: 24752836
Please can you give me some additional information on how to implement the solutions that you proposed. Where exactly do you call the function FindPreference  (at page load ?) and how do I refer the string to the database. (I'm using objectdatasource from table data adapter see previous code) .
I 'm ok when dealing with insert and updates on object datasource but can't figure the steps for retrieving the values and ultimately dsiplay them in the formview. I'm sorry if my question sounds rather simple but I'm still fairly new to it all :-)
I'll be monitoring my request cosely today.

Thanks in advance,
Jeewai
0
 

Author Comment

by:jeewai
ID: 24757082
Guys,

Here I 've attached the .aspx and .aspx.vb files to give you a clear idea of my situation. I've tried my best to make it as clear as possible to facilitate your assistance. Please should you need more information or a clarification, let me know.  Basically, all I need is te create connection and place the preference code bits in the right place. Please have a look.

Regards,
Jeewai
Code-1-FormView.doc
Code-2-Code-behind.doc
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

751 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