Solved

SQL query question

Posted on 2011-03-06
8
264 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 40

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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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 80 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 150 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 20 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

786 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