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.