22

I have a stored procedure that has a parameter called UserName and in my code behind I have a SqlCommand object that I add the parameters to with the Add method. But for some reason when the command object tries to run the ExecuteReader method, it throws an exception. I am totally at a loss and have no idea why it's not recognizing the parameter. Before the ExecuteReader method is run I have a break point set so I can confirm the command object does contain the parameters being set, which is true. I know the stored procedure does return the correct data when the parameters are not added to the command object, but are hard coded in the actual stored procedure. Below is the exception message that is given in the catch block. I will also paste my code and first part of stored procedure. I would greatly appreciate any help in this issue, seeing that I have tried many different approaches to no avail. Thanks in advance.



Exception Message

Procedure or function 'someStoredProcedure' expects parameter '@UserName', which was not supplied.



Code Behind

private DataTable GetLossMitData(string code, DateTime? start, DateTime? end)  
{  
DataTable results = new DataTable();  
string connectionString = ConfigurationManager.ConnectionStrings["asdf"].ConnectionString;  
string userName = String.Empty;  

try  
{  
    using (SPSite site = new SPSite(ConfigurationManager.AppSettings["someName"]))  
    {  
        using (SPWeb web = site.OpenWeb())  
        {  
            userName = web.CurrentUser.Email.ToString();  
        }  
    }  

    using (SqlConnection connection1 = new SqlConnection(connectionString))  
    {  
         connection1.Open();  
         using (SqlCommand command1 = new SqlCommand("someStoredProcedure", connection1))  
         {  
             command1.Parameters.Add(new SqlParameter("@UserName", userName));  
             command1.Parameters.Add(new SqlParameter("@ProductCode", code));  

             SqlDataReader dr = command1.ExecuteReader(CommandBehavior.CloseConnection);  
             results.Load(dr);  
         }  
         connection1.Close();  
    }  
}  
catch (Exception ex)  
{  
}  
return results;  
}  



Stored Procedure

@UserName nvarchar(256),  
@ProductCode nvarchar(256),
@StartDate nvarchar(256) = '1/1/1900',
@EndDate nvarchar(256) = '12/30/2012'

AS
BEGIN
SET NOCOUNT ON;

Declare @UserID int

Select @UserID = Users.UserID
from Users
where Users.Email = @UserName
1

5 Answers 5

51

Try making sure that the command type is set to stored procedure.

mycommand.CommandType = System.Data.CommandType.StoredProcedure;
Sign up to request clarification or add additional context in comments.

10 Comments

You get an "Incorrect syntax near..." error if it's not set to StoredProcedure, so I don't think it's this
@Ray's answer doesn't make sense given the context. If the command type is set to Text, then calling this stored procedure will never work unless you do something like @SLaks suggested by placing the parameters within the statement. Changing the command type to stored procedure should most definitely work.
If anything, getting an error whenever the command type is not set to stored procedure only seems to provide more evidence that this is in fact the issue.
The error message returned by sql server indicates that it knows that the command is a stored proc call. I agree with you that it should be explicitly setup in the command object so sql server doesn't have to guess, but I don't think this causes the error.
@Ray - The reason that this does cause the issue is because you're sending SQL a text command like "exec SPROC". If SPROC is supposed to accept parameters and you do not specify those parameters within the text command, then the command type must be changed to stored procedure so that it can be sent to the SQL server correctly. That is why OP got an "expects parameter" error.
|
10

You will get this exception if the value of your 'userName' variable is null

If null is valid, then pass 'DBNull.Value' to the db instead:

command1.Parameters.Add(new SqlParameter("@UserName", (userName ?? DBNull.Value));   

13 Comments

Wrong. That's not the problem here, and it wouldn't throw this exception anyway.
@SLaks - Your downvote is a bit hasty - I make this mistake all too frequently, and this is the exception that I get to spank me
Actually, I didn't downvote; somebody else did. However, you're still wrong.
userName is not null and should never be null as long as they have logged in. But I say should as if anything in code is ever certain. But I will add that just in case. Thanks
@Slaks - Sorry for the assumption. @jhorton - "should never be null" - I have said that myself once or twice, which is why I have seen this same exception happen to me.
|
3
Command1.CommandType = System.Data.CommandType.StoredProcedure

This will force the ExecuteReader to perform the exec instead of just trying it as a flat command.

Comments

3

By default, the CommandText property needs to contain a complete SQL command, not just the name of the stored procedure.

You can change this by to set the SqlCommand's CommandType property to StoredProcedure.

Alternatively, you could explicitly pass the parameters, by changing the CommandText to "someStoredProcedure @UserName, @ProductCode"; this is a complete SQL statement and will work with the default CommandType of Text.

EDIT: I just tried it, and the only way to get that error message without setting CommandType to StoredProcedure (which he did not do) is if CommandText is EXEC someStoredProcedure. Passing a null parameter gives a different error.

2 Comments

You get an "Incorrect syntax near..." error if it's not set to StoredProcedure, so I don't think it's this. I've reproduced the error as given by OP, using @Ray's answer so I think he may be correct
+1 as I've reproduced also it this way now. It appears it matters what version of SQL Server you're using - SQL 2008 reports this as the error given by the OP. In SQL 2000 (and I think 2005 though I need to recheck), it's reported as I said in my comment above - "Incorrect syntax...". Think I may knock up a blog post on this!
0

ADO.NET provides classes to run the DB operations in .NET platform and handle the results with several methods. One the important parts is executing related command in database & retrieve the result of it. ADO.NET provides SqlCommand class for this part of operations.

Based on your choose & design of code, there are several ways to manage this. I believe the best way is writing Stored Procedures in your database & execute them in your code. For making the correct code you need to set these parts in your SqlCommand object:

1 - Connection (provide the connection to database)

2 - CommandType (detect your choose for executing type like simple text or stored procedure)

3 - CommandText (based on CommandType, it can be the name of stored procedure or full database command text)

If your command includes parameters (stored procedure or simple text), you have to add them in Parameters object of SqlCommand. Now you can run your command on database & retrieve the result.

For reading data for example you can choose ExecuteReader() method (there are another ways with some differences too) and for DML operations you can use ExecuteNonQuery() method of SqlCommand object.

If you did it in right way, it should work properly without problem.

Note: these classes are in System.Data.SqlClient namespace & optimized for Sql Server. For another vendors you can use similar classes in System.Data.Common package (modern .NET).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.