Solved

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

Posted on 2014-10-06
5
372 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 40

Accepted Solution

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

Expert Comment

by:louisfr
ID: 40365375
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
ID: 40367545
Thanks for your responses.  I am working on the project this evening.

jimbo99999
0
 

Author Closing Comment

by:Jimbo99999
ID: 40368126
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
ID: 40368618
It's working great until someone puts "blabla'); drop table VeryImportantTable; --" in your input string.
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

751 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