Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 188865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:07:28+00:00 2026-05-11T16:07:28+00:00

I’m trying to build a list that will be used as the in clause

  • 0

I’m trying to build a list that will be used as the in clause of a select statement. The requirement is to have the user enter a comma separated list of descriptions. Each description can contain spaces so I can’t remove the spaces before splitting by comma to add the single quotes around each description. I want to remove all white space after a single quote since no description will start with a space. What’s the best way to do this in VB.NET? Regular expression or a string function? Here’s what I have so far.:

Partial Class Test
    Inherits System.Web.UI.Page

    Protected Sub cmdGetParts_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdGetParts.Click
        Dim sDescriptionList As String = ""
        BuildList(sDescriptionList)
        RemoveSpacesFromList(sDescriptionList)
        FillGrid(sDescriptionList)
    End Sub

    'Build descriptions List based on txtDescriptionList.Text
    Private Sub BuildList(ByRef sDescriptionList As String)
        Dim sDescriptionArray As String()
        sDescriptionArray = txtDescriptionList.Text.Trim.Split(","c)
        Dim iStringCount As Integer = 0
        For Each description In sDescriptionArray
            If iStringCount > 0 Then
                sDescriptionList = sDescriptionList & ","
            End If
            sDescriptionList = sDescriptionList & "'" & description & "'"
            iStringCount = iStringCount + 1
        Next
    End Sub

    **'This procedure removes unwanted spaces from  description list
    Private Sub RemoveSpacesFromList(ByRef sList As String)
        sList = sList.Replace("' ", "'")
    End Sub**

    'This procedure fills the grid with data for descriptions passed in
    Private Sub FillGrid(ByVal sDescriptionList As String)
        Dim bo As New boPart
        Dim dtParts As Data.DataTable
        dtParts = bo.GetPartByDescriptionList(sDescriptionList)
        GridView1.DataSource = dtParts
        GridView1.DataBind()
    End Sub
End Class 

Edited: After reviewing this code I think I may be able to just place
description.Trim inside the For Each loop of the BuildList procedure.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:07:28+00:00Added an answer on May 11, 2026 at 4:07 pm

    Use a regular expression to match a comma with any surrounding white space, and replace with apostropes and a comma. The starting apostrope for the first item and the ending apostrophe for the last item you simply add afterwards.

    The RemoveSpacesFromList method is no longer needed, as the BuildList method does it all.

    Protected Sub cmdGetParts_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdGetParts.Click
        Dim descriptions As String = txtDescriptionList.Text
        descriptions = BuildList(descriptions)
        FillGrid(descriptions)
    End Sub
    
    ''//Build descriptions List based on a comma separated string
    Private Function BuildList(ByVal descriptions As String) As String
       Return "'" + Regex.Replace(descriptions, "\s*,\s*", "','", RegexOptions.Compiled) + "'"
    End Function
    

    Note:
    If you are using this string to build an SQL query, your application is wide open for SQL injection attacks. Using parameterised queries is the preferred method, but that might not be convenient in your case. User input has to at least be sanitised before being used in a query.

    Edit:
    If the adapter uses apostrophes as escape character in a string literal, you can escape the string properly like this:

    Private Function BuildList(ByVal descriptions As String) As String
       Return "'" + Regex.Replace(descriptions.Replace("'","''"), "\s*,\s*", "','", RegexOptions.Compiled) + "'"
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.