?
Solved

SQL query question

Posted on 2011-03-06
8
Medium Priority
?
272 Views
Last Modified: 2012-05-11
Hi,
I have the following line of code:
 query = "sp_GetUserLogin '" + userID + "', '" + password + "'";
that I am trying to rewrite like:
query = "sp_GetUserLogin @userID, @password";
but I am getting SqlException was unhandled by user code (Incorrect syntax near ‘sp_GetUserLogin’)
Can someone tell me what is wrong with my code? Thanks!
0
Comment
Question by:avi7
8 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 35050012
Can you post some more code. Do you want to pass UserID and Password as parameters to the SP?
0
 
LVL 11

Expert Comment

by:JoeNuvo
ID: 35050997
the way you try to call, still be inline SQL
so, even you want to put the parameter name.
code will be look like this

query = "sp_GetUserLogin @username =  '" + userID + "',  @password = '" + password + "'"; 

Open in new window


if you don't want to do as above, you should give more details, for ex. what is your application language, etc.
0
 

Author Comment

by:avi7
ID: 35053439
It is C# and I was trying to do something like this:

query = "sp_GetUserLogin '" + userID + "', '" + password + "'";
SqlCommand cmd;
cmd = new System.Data.SqlClient.SqlCommand(query, conn);            
cmd.Parameters.AddWithValue("@userID", userID);
cmd.Parameters.AddWithValue("@password", password);

conn.Open();
SqlDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
 try
            {
                if (reader.HasRows == true)
                {
                    reader.Close();
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:lalitgada
ID: 35053634
check the ' syntax.

and if you want to use the aspnetmembership feature then change the logic.
0
 
LVL 9

Assisted Solution

by:kaminda
kaminda earned 320 total points
ID: 35055176
Hi,

You are adding the parameters to the command with

cmd.Parameters.AddWithValue("@userID", userID);
cmd.Parameters.AddWithValue("@password", password);

So you dont have to state in the query, just state the sp name then your code will work. If you like to dynamically call the sp then remove the parameter adding part to command object and use a query such as ;

EXEC sp_GetUserLogin 'userid', 'password'
0
 
LVL 11

Accepted Solution

by:
JoeNuvo earned 600 total points
ID: 35056840
try

query = "EXEC sp_GetUserLogin @userID , @password";
SqlCommand cmd;
cmd = new System.Data.SqlClient.SqlCommand(query, conn);            
cmd.Parameters.AddWithValue("@userID", userID);
cmd.Parameters.AddWithValue("@password", password);

.
.
.

Open in new window

0
 
LVL 2

Assisted Solution

by:EL_Barbado
EL_Barbado earned 80 total points
ID: 35072670
You need to put EXEC in front of the sp_GetUserLogin
0
 

Author Closing Comment

by:avi7
ID: 35142457
Thanks so much!
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

862 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