Solved

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

Posted on 2014-10-06
5
337 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
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net How to Exit Sub - Exit Form??? 5 57
Crystal reports vb.net 2 39
VB.NET 2008 - SQL Timeout 9 23
Create a datatable in vb.net dynamically 1 11
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

813 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

15 Experts available now in Live!

Get 1:1 Help Now