473,671 Members | 2,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
(sProductDescri pton) 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 sProductDescrip ton As String =
Me.ddlProduct.S electedValue
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='" & sProductDescrip ton & "'"
13 sConn = "Server=USFBGDX PP196\SQLEXPRES S;Initial
Catalog=APPS;In tegrated Security=True"
14
15 myConn = New SqlConnection(s Conn)
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.F ill(dSet)
24
25 Me.gvPanel.Data Source = dSet
26 Me.gvPanel.Data Bind()
27
28 myCmd.Dispose()
29 myConn.Close()

Could anyone offer any suggestions?

Jan 10 '07 #1
1 1615
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*******@gmai l.comwrote in message
news:11******** *************@i 39g2000hsf.goog legroups.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
(sProductDescri pton) 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 sProductDescrip ton As String =
Me.ddlProduct.S electedValue
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='" & sProductDescrip ton & "'"
13 sConn = "Server=USFBGDX PP196\SQLEXPRES S;Initial
Catalog=APPS;In tegrated Security=True"
14
15 myConn = New SqlConnection(s Conn)
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.F ill(dSet)
24
25 Me.gvPanel.Data Source = dSet
26 Me.gvPanel.Data Bind()
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
6659
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 message: SQL0206N "dipf.AENDERUNGSLISTE_SEQ" ist in dem verwendeten Kontext ungültig.
3
429
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 recorset and returns all the names that have asearchkey somewhere in their description so if "hello" is in "this is a hello test" will be returned
12
2878
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 SQL(watch for word wrap: StrSql = "insert into(ProductName)" 'SQL string which executes with the RunSQL statement
3
5583
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 and it worked running it in access. However when i paste it into the asp page i get an "Expected end of
2
1916
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 expecting an "end of statement" after the first quotation mark before the word "Fail". From my very limited knowledge of SQL, I'm assuming that it sees the first quotation mark as the end of the statement. How can I remedy this? strSQL = strSQL...
11
16217
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, Col3, Col4) Values(" & _ str1 & "," & dt1 & "," & str2 & "," & str3 & ")" where str1 = "col1"
3
6244
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 put the above string into the web.config file, it fails due to a double quotes. Is there any way I can do it? <appSettings> <add key="Patient" value="select reg_date "Regiatration" from
4
5558
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 db2 clp(ofcourse prefixing db2) it works fine, but when I put it inside an sql file and then when I try to run the file using db2 -tvf, it gives me following error msg
2
1412
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 the Find statement below on the fly: rsTemp1.FindFirst Me!cboSelect1 & "=" & rs(Me!cboSelect1) on the fly.
0
8481
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8823
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8672
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6234
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4412
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.