473,326 Members | 2,108 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,326 software developers and data experts.

Select Statement with Parameters

The following code returns no results. Removing the quotes from the
question mark yields the following error: Incorrect syntax near '?'.
Replacing the question mark with William does give results. Please
help!!

://Bill

<%@ Page language="VB"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML>
<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
Dim login_user As String
login_user = Request.ServerVariables("LOGON_USER")
Session.Add("User_ID", login_user)
Response.Write("Session ID from the SCRIPT section is: <B>" &
Session("User_ID") & "</B>. Moving on....")
End Sub
</script>
<body>
<form id="Form1" method="post" runat="server">
<P>Show My Orders:</P>

<asp:SqlDataSource
id="OdbcDataSource1"
runat="server"
ProviderName="System.Data.SqlClient"
ConnectionString="Data Source=XXX;Initial
Catalog=Employee;Persist Security Info=True;User ID=YYY;Password=ZZZ"
SelectCommand="SELECT EmpFirstname, EmpMiddlename,
EmpLastName
FROM Employee
WHERE EmpFirstname = '?'
ORDER BY EmpLastName;"/>
<SelectParameters>
<asp:SessionParameter
Name="empid"
SessionField="User_ID"
DefaultValue="William" />
</SelectParameters>
</asp:SqlDataSource>

<p>
<asp:GridView
id="GridView1"
runat="server"
DataSourceID="OdbcDataSource1" />
</p>
</form>
</body>
</HTML>

Jun 15 '06 #1
10 1476
On 15 Jun 2006 11:04:03 -0700, wh*****@gmail.com wrote:
The following code returns no results. Removing the quotes from the
question mark yields the following error: Incorrect syntax near '?'.
Replacing the question mark with William does give results. Please
help!!


You can't use the ? syntax. Use named parameters.

WHERE EmpFirstname = '@empid'
Jun 15 '06 #2
Erik Funkenbusch wrote:
On 15 Jun 2006 11:04:03 -0700, wh*****@gmail.com wrote:
The following code returns no results. Removing the quotes from the
question mark yields the following error: Incorrect syntax near '?'.
Replacing the question mark with William does give results. Please
help!!


You can't use the ? syntax. Use named parameters.

WHERE EmpFirstname = '@empid'


The apostrophes is added automatically.

WHERE EmpFirstname = @empid
Jun 15 '06 #3
EmpFirstname = @empid generates a SQL error: "Must declare
variable....."

Göran Andersson wrote:
Erik Funkenbusch wrote:
On 15 Jun 2006 11:04:03 -0700, wh*****@gmail.com wrote:
The following code returns no results. Removing the quotes from the
question mark yields the following error: Incorrect syntax near '?'.
Replacing the question mark with William does give results. Please
help!!


You can't use the ? syntax. Use named parameters.

WHERE EmpFirstname = '@empid'


The apostrophes is added automatically.

WHERE EmpFirstname = @empid


Jun 15 '06 #4
On 15 Jun 2006 12:52:06 -0700, wh*****@gmail.com wrote:
EmpFirstname = @empid generates a SQL error: "Must declare
variable....."


You need to set the name of the parameter to @empid as well.
Jun 15 '06 #5
Thanks for all the help, I must also add that I am new to all this.
Still getting a 'must declare variable error...' (tried Name="@empid"
and Name="empid")

<%@ Page language="VB"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML>
<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
Dim login_user As String
login_user =
Request.ServerVariables("LOGON_USER")
Session.Add("User_ID", login_user)
Response.Write("Session ID from the SCRIPT
section is: <B>" & Session("User_ID") & "</B>. Moving on....")
End Sub
</script>
<body>
<form id="Form1" method="post" runat="server">
<P>Show My Orders:</P>

<asp:SqlDataSource
id="OdbcDataSource1"
runat="server"
ProviderName="System.Data.SqlClient"
ConnectionString="Data Source=ZZZZ;Initial
Catalog=Employee;Persist Security Info=True;User ID=XXXX;Password=XXXX"
SelectCommand="SELECT EmpFirstname, EmpMiddlename, EmpLastName FROM
Employee WHERE EmpFirstname = @empid ORDER BY EmpLastName;"/>
<SelectParameters>
<asp:SessionParameter
Name="@empid"
SessionField="User_ID"
DefaultValue="William" />
</SelectParameters>

</asp:SqlDataSource>

<p>
<asp:GridView
id="GridView1"
runat="server"
DataSourceID="OdbcDataSource1" />
</p>
</form>
</body>
</HTML>

Jun 15 '06 #6
On 15 Jun 2006 13:26:29 -0700, wh*****@gmail.com wrote:
Thanks for all the help, I must also add that I am new to all this.
Still getting a 'must declare variable error...' (tried Name="@empid"
and Name="empid")


Actually, i was mistaken. You don't need the @ on the name attribute of
the parameter.

Still, this doesnt' seem right. You only get that sort of error when using
a stored procedure AFAIK.

Are you sure it's not coming from somewhere else?
Jun 15 '06 #7
Still, this doesnt' seem right. You only get that sort of error when using
a stored procedure AFAIK.
This is what I thought as well but I am an amatuer. :-)

Are you sure it's not coming from somewhere else?


I don't think so based on the error message:

System.Data.SqlClient.SqlException: Must declare the variable '@empid'.

Looks like since the error includes, SqlClient.SqlException, the code
isn't completing the SQL statement with the parameters before sending
it to the server. Right?

Jun 16 '06 #8
Bueller, Bueller, Buuuuuuueller....

Jun 19 '06 #9
I am having the same problem. When I look at the query as it is passed to
SQLserver (in sql profiler), I can see that the query still has the @variable
in the string. It should be replaced before that. Don't ask me why it isn't
though I am new to .net too. BTW did you ever get this resolved?

"wh*****@gmail.com" wrote:
Bueller, Bueller, Buuuuuuueller....

Aug 29 '06 #10

nate schrieb:
though I am new to .net too. BTW did you ever get this resolved?
You may not use oledb - because it can't handle parameters. SQL works.

Sep 25 '06 #11

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

Similar topics

21
by: John Fabiani | last post by:
Hi, I'm a newbie and I'm attempting to learn howto create a select statement. When I use >>> string1='18 Tadlock Place' >>> cursor.execute("SELECT * FROM mytest where address = %s",string1) All...
2
by: web developer | last post by:
hi I need to write a stored procedure that takes input parameters,and according to these parameters the retrieved fields in a select statement are chosen. what i need to know is how to make the...
1
by: Dieter Gasser | last post by:
I have two SQL Server stored procedures, PROC1 and PROC2. PROC1 has about 50 input parameters. PROC2 is the main procedure that does some data modifications and afterwards calls PROC1 using an...
2
by: Gadrin77 | last post by:
as a newbie to XSL, is it possible to mimic a SELECT/CASE statement using XSL? I tried a quickie and I kept getting errors either using PARAM or WITH-PARAM in the wrong place or VARIABLE. I...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
7
by: Matt Jensen | last post by:
Howdy Fairly simple question I think, I presume the answer is no it can't be reused for 2 *SELECT* statements, but just hoping for clarification. Just asking in the interests of trying to minimise...
6
by: Jack | last post by:
I have the following: * An OLEDBCommand with command text "SELECT CAMPAIGN, DAY_OUT WHERE (CAMPAIGN LIKE '@campaign')" * A DataAdapter that point the select to the above command * A data grid...
1
by: jamminc | last post by:
Hi All, I am currently developing a module where the user will input multiple items (as many as 20-600) and I am suppose to retrieve it from a sql database all the information and update it with...
1
by: serena.delossantos | last post by:
Trying to insert into a history table. Some columns will come from parameters sent to the store procedure. Other columns will be filled with a separate select statement. I've tried storing the...
7
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, i noticed when i run my stored procedure in sql studio i get a result set from my select statement and then below that i get a return value. how about when it's called from inside csharp....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.