• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 795
  • Last Modified:

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
0
lrbrister
Asked:
lrbrister
  • 3
  • 3
2 Solutions
 
CodeCruiserCommented:
Can you show a sample of rows in this table?
0
 
lrbristerAuthor Commented:
Code Cruiser

Sure

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

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

 
Paul_Harris_FusionCommented:
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
 
lrbristerAuthor 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
 
lrbristerAuthor Commented:
Gave me a great start
0

Featured Post

Industry Leaders: 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!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now