Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / ADO.NET / April 2006

Tip: Looking for answers? Try searching our database.

Passing variable to a SQL statement does not work?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
glenn - 21 Apr 2006 20:05 GMT
Hi folks,

The problem I have is that a query string works if hard-coded but
if I pass a variable to it, it does not work as shown here.

Dim queryString, q1, q2 As String

This works:
  querystring="SELECT * FROM USERS WHERE CNAME = Microsoft"

This does not work:
  Dim var as string
  var = "Microsoft"
  querystring="SELECT * FROM USERS WHERE CNAME = '" & var & "'"

I have done a Response.Write (q1) and Response.Write (q2) where
q1 is a hard-coded SQL statement and where q2 passes a variable to a SQL
string

The values written to the screen are exactly the same.  

Why does the hard-coded version work and the var version not work?

Thanks for any clues.
glenn
glenn - 21 Apr 2006 20:15 GMT
SORRY FOR THE DUPLICATE POST.  IT'S MY MISTAKE.

> Hi folks,
>
[quoted text clipped - 21 lines]
> Thanks for any clues.
> glenn
Norman Yuan - 21 Apr 2006 20:28 GMT
From your post, I see the completely opposite result:

1. When print these two query string to the screen, they are NOT the same:

first one:

SELECT * FROM USERS WHERE CNAME = Microsoft

second one:

SELECT * FROM USERS WHERE CNAME = 'Microsoft'

2. It seems the column "CNAME" is text type. Most people who know SQL in
general, would say the second is correct, while the first is not, because it
misses the single quote mark around the text value in WHERE clause.

So, how come the first one works for you? What special database is it?

> Hi folks,
>
[quoted text clipped - 21 lines]
> Thanks for any clues.
> glenn
JT - 22 Apr 2006 06:34 GMT
Hi Glenn,
Your statement is actually querying for a row where CNAME = & var &.  What
you want is:
Dim var as string = "'Microsoft'"
  querystring="SELECT * FROM USERS WHERE CNAME = " & var

Signature

John

> Hi folks,
>
[quoted text clipped - 21 lines]
> Thanks for any clues.
> glenn

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.