Build comma seperated string from datatable

I have a datatable in VB code called dtSpecialty

It has 3 columns
ID  string value
Name string value
EmailBlastDefault Boolean value

I need to build a comma seperated string that cycles through the table rows and where EmailBlastDefault = 1 (True) add name to string called liThisSpecialty

So I can use the resulting string (liThisSpecialty)  to run this

        For Each liThisSpecialty In cblSpecialties.Items
            If InStr(sSpecialtiesToSkip, "," & liThisSpecialty.Text & ",") > 0 Then
            Else
                liThisSpecialty.Selected = True
            End If
        Next
Larry Bristersr. DeveloperAsked:
Who is Participating?
 
Paul_Harris_FusionConnect With a Mentor Commented:
dim sb as new system.text.stringbuilder
dim csvString as string

sb.length =0
for each row as datarow in dtSpecialty.Rows
    if cint(row("EmailBlastDefault")) = 1 then
         if sb.length>0 then sb.append(",")      ' remove this line to retain the leading comma
          sb.append (row("Name").tostring )
    end if
next

csvString = sb.tostring
0
 
CodeCruiserCommented:
Can you show a sample of rows in this table?
0
 
Larry Bristersr. DeveloperAuthor Commented:
Code Cruiser

Sure

ID             Name               EmailBlastDefault
ANTE         Antepartum          1
BIOMED   BIOMEDTECH      0
BMET        BMET                       1

Would build this string
,Antepartum,BMET ,
0
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.

 
Paul_Harris_FusionConnect With a Mentor Commented:
Actually - the code sample should build a csv string according to your sample data but I don't understand the original post where you say

So I can use the resulting string (liThisSpecialty)  to run this

        For Each liThisSpecialty In cblSpecialties.Items
            If InStr(sSpecialtiesToSkip, "," & liThisSpecialty.Text & ",") > 0 Then
            Else
                liThisSpecialty.Selected = True
            End If
        Next
0
 
Larry Bristersr. DeveloperAuthor Commented:
Hey guys,
 Worked it out myself...
This is final solution...does anyone feel like they have points coming to them?


    Sub BindChoices()
        Dim dtSpecialty As New DataTable
        dtSpecialty = GetSpecialty()
        BindToDatasource(cblSpecialties, dtSpecialty, "Name", "ID")

        Dim liThisSpecialty As ListItem
        Dim sSpecialtiesToSkip As String = ","

        For Each row As DataRow In dtSpecialty.Rows
            If row.Item("EmailBlastDefault") = "0" Then
                sSpecialtiesToSkip = sSpecialtiesToSkip & row.Item("Name") & ","
            End If
        Next row

        For Each liThisSpecialty In cblSpecialties.Items
            If InStr(sSpecialtiesToSkip, "," & liThisSpecialty.Text & ",") > 0 Then
            Else
                liThisSpecialty.Selected = True
            End If
        Next
    End Sub

Open in new window

0
 
Paul_Harris_FusionCommented:
That's up to you to decide.
0
 
Larry Bristersr. DeveloperAuthor Commented:
Gave me a great start
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.