473,396 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to Handle a SQL Statement with Quotes

To any who might be able to help this is the situation.I have a
dropdown list that gets populated from SQL. It contains both single and
double quotes. It populates the dropdown list just fine. I create a
variable and assign its value to whatever is the selected value of the
dropdown list control. (Line 1) Based on that variable I use it to
search for another item in the table, then populate a Gridview (Lines
19-26). The problem (I think) is that since the variable
(sProductDescripton) has single and double quotes, when it is used in
the query statement the quotes are not being 'seen'. (Line 12) Below is
my code:

1 Dim sProductDescripton As String =
Me.ddlProduct.SelectedValue
2 Dim sDistrict As String = Me.ddlDivision.SelectedValue
3 Dim sProductType As String = Me.ddlProdType.SelectedValue
4 Dim sPart As String = "Panel"
5
6 Dim myConn As New SqlConnection
7 Dim myCmd As New SqlCommand
8 Dim sSQL As String
9 Dim sConn As String
12 sSQL = "select NAME, PART from POP_PRODUCT where TYPE=
'" & sProductType & "' and PART= '" & sPart & "' and DISTRICT= '" &
sDistrict & "' and NAME='" & sProductDescripton & "'"
13 sConn = "Server=USFBGDXPP196\SQLEXPRESS;Initial
Catalog=APPS;Integrated Security=True"
14
15 myConn = New SqlConnection(sConn)
16 myConn.Open()
17 myCmd = New SqlCommand(sSQL, myConn)
18
19 Dim dSet As New Data.DataSet
20 Dim dView As New Data.DataView
21 Dim myDataAdapter As SqlDataAdapter
22 myDataAdapter = New SqlDataAdapter(sSQL, sConn)
23 myDataAdapter.Fill(dSet)
24
25 Me.gvPanel.DataSource = dSet
26 Me.gvPanel.DataBind()
27
28 myCmd.Dispose()
29 myConn.Close()

Could anyone offer any suggestions?

Jan 10 '07 #1
1 1607
There are two ways to handle this - #1 is much more preferable for more
reasons than 1:
1. Parameterized Queries (2 part tutorial:
http://aspnet101.com/aspnet101/tutorials.aspx?id=1)
2. http://aspnet101.com/aspnet101/tutorials.aspx?id=2 - remember
concatenation of queries is not recommended at all.

--
David Wier
MVP/ASPInsider
http://aspnet101.com
http://aspexpress.com
"swep" <pe*******@gmail.comwrote in message
news:11*********************@i39g2000hsf.googlegro ups.com...
To any who might be able to help this is the situation.I have a
dropdown list that gets populated from SQL. It contains both single and
double quotes. It populates the dropdown list just fine. I create a
variable and assign its value to whatever is the selected value of the
dropdown list control. (Line 1) Based on that variable I use it to
search for another item in the table, then populate a Gridview (Lines
19-26). The problem (I think) is that since the variable
(sProductDescripton) has single and double quotes, when it is used in
the query statement the quotes are not being 'seen'. (Line 12) Below is
my code:

1 Dim sProductDescripton As String =
Me.ddlProduct.SelectedValue
2 Dim sDistrict As String = Me.ddlDivision.SelectedValue
3 Dim sProductType As String = Me.ddlProdType.SelectedValue
4 Dim sPart As String = "Panel"
5
6 Dim myConn As New SqlConnection
7 Dim myCmd As New SqlCommand
8 Dim sSQL As String
9 Dim sConn As String
12 sSQL = "select NAME, PART from POP_PRODUCT where TYPE=
'" & sProductType & "' and PART= '" & sPart & "' and DISTRICT= '" &
sDistrict & "' and NAME='" & sProductDescripton & "'"
13 sConn = "Server=USFBGDXPP196\SQLEXPRESS;Initial
Catalog=APPS;Integrated Security=True"
14
15 myConn = New SqlConnection(sConn)
16 myConn.Open()
17 myCmd = New SqlCommand(sSQL, myConn)
18
19 Dim dSet As New Data.DataSet
20 Dim dView As New Data.DataView
21 Dim myDataAdapter As SqlDataAdapter
22 myDataAdapter = New SqlDataAdapter(sSQL, sConn)
23 myDataAdapter.Fill(dSet)
24
25 Me.gvPanel.DataSource = dSet
26 Me.gvPanel.DataBind()
27
28 myCmd.Dispose()
29 myConn.Close()

Could anyone offer any suggestions?

Jan 10 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Hans-Dieter Franz | last post by:
Hello, I have a problem with a Java user-defined stored procedure and the "alter sequence" statement. I use DB2 8.1.2 and had the same problem with 8.1.0, but not with 7.*. I get the following...
3
by: Danny | last post by:
Here is the sql statement. Set rs2 = db.OpenRecordset("select * from Names where InStr(descriptions" + "+" + """" + "|" + """" + "," + """" + asearchkey + """" + ") > 0") this searches a...
12
by: ColinWard | last post by:
Hi. I am trying to run the following code when the user clicks a button, but I am getting a syntax error in the SQL. I have a feeling it has to do with brackets. Can anyone help? here is the...
3
by: NeilH | last post by:
Hello All I was wondering if someone could offer a rather inexperienced person some advice. Im trying to get my asp page to look at an access data I created the following query in access...
2
by: Wayne | last post by:
This is probably simple but: The following portion of an SQL statement that I've copied from the SQL view of the query grid (the query works fine) is giving me an error to the effect that it is...
11
by: sm | last post by:
Hi All, Can anybody give me the syntax to insert a record into SQL server through VB code using variables? The following statement is failing! sInsertQuery = "INSERT INTO TestTab (Col1, Col2,...
3
by: Assembly file locked | last post by:
I'm trying to put a SQL statement in a web.config file. But I'd like to have a customized column name as I normally do in ORACLE: e.g. select reg_date "Registration" from patient But when I...
4
by: Jatinder | last post by:
Hi, I am trying to grant connect privilege to a user present on my O.S. (Windows) using following statement. GRANT CONNECT ON DATABASE TO user "user1" now when I execute this statemnt from...
2
by: barry.edmund.wright | last post by:
Hi All, I want to build a Find Statement on the fly. The code below starting with 'This Code Works! is fine but as I said I want to build the Me!cboSelect1 & "=" & rs(Me!cboSelect1) portion of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.