Solved

VB.Net - Search String for Character and Add Single Quotes

Posted on 2014-10-06
5
315 Views
Last Modified: 2014-10-08
Good Day Experts!

I have a VB.Net form that takes User input and retrieves records from a SQL database for display in a DataGrid.

I need to allow the User to enter multiple values in on2 of the textboxes.  My plan was to tell the User to enter just a single value or multiples separated by a comma.  I have my query behind the "Get Records" button built to accept
where in ('Value1','Value3'...etc).

I was going to iterate through the string until each comma was encountered then add a starting/ending single quote around the value then put the string back together.  

Question:

Can you think of any functions that may help me achieve this end goal in the simplest fashion?

Thanks,
jimbo99999
0
Comment
Question by:Jimbo99999
  • 2
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
Comment Utility
Dim values As String = "One,Two,Three"
            values = "'" & values.Replace(",", "','") & "'"
0
 
LVL 11

Expert Comment

by:louisfr
Comment Utility
As always when injecting user input into SQL, use a parameterized query.
        Dim csv = "value1,value2,value3"
        Dim values = csv.Split(",")
        Dim cmd = New SqlCommand()
        For i As Integer = 0 To values.Length - 1
            cmd.Parameters.AddWithValue(i.ToString("'@p'0"), values(i))
        Next
        cmd.CommandText = "select ... where in (" + String.Join(",", Enumerable.Range(0, values.Length).Select(Function(i) i.ToString("'@p'0"))) + ");"

Open in new window

0
 

Author Comment

by:Jimbo99999
Comment Utility
Thanks for your responses.  I am working on the project this evening.

jimbo99999
0
 

Author Closing Comment

by:Jimbo99999
Comment Utility
Thanks James...I did not go down that path of thought process to achieve the end goal.  It is working great now.

jimbo99999
0
 
LVL 11

Expert Comment

by:louisfr
Comment Utility
It's working great until someone puts "blabla'); drop table VeryImportantTable; --" in your input string.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now