0

I am trying to execute a stored procedure from EF5 using Database.SqlQuery. But 2nd parameter is not recognizing here.

Error: "The parameterized query '(@custNum nvarchar(7), @PrimaryDisc bigint, @SecondaryDisc bigint)' expects the parameter '@PrimaryDisc', which was not supplied."

Code

var results = _MiscContext.Database.SqlQuery<TempTechDisciplines>(
        "exec sp_getTechnicalDiscipline @CustNum, @PrimaryDisc, @SecondaryDisc", 
        new SqlParameter("custNum", CustomerNum), 
        new SqlParameter("PrimaryDisc",SqlDbType.BigInt, 0), 
        new SqlParameter("SecondaryDisc",SqlDbType.BigInt, 0))
        .ToList<TempTechDisciplines>();

What is the issue here?

5
  • 1
    Try @ while adding parameter names like : new SqlParameter("@custNum", CustomerNum), Commented Nov 26, 2013 at 18:23
  • 1
    did not work. by adding @ Commented Nov 26, 2013 at 18:24
  • 2
    I don't think you're using the SqlParameter constructor you think you are. Commented Nov 26, 2013 at 18:28
  • Could casing be an issue? The query itself uses CustNum, but the parameter is named custNum. Commented Nov 26, 2013 at 18:43
  • Never used the biginteger types, but I'd try new SqlParameter("@PrimaryDisc", new BigInteger(0)). Commented Nov 26, 2013 at 18:45

2 Answers 2

1

It works for me from this article.

http://blogs.msdn.com/b/diego/archive/2012/01/10/how-to-execute-stored-procedures-sqlquery-in-the-dbcontext-api.aspx

var custNum = new SqlParameter {ParameterName = "CustNum", Value = CustomerNum};
var primaryDisc = new SqlParameter { ParameterName = "PrimaryDisc", Value = 0 };
var secondaryDisc = new SqlParameter { ParameterName = "SecondaryDisc", Value = 0 };

var results = _MiscContext.Database.SqlQuery<TempTechDisciplines>(
             "exec sp_getTechnicalDiscipline @CustNum, @PrimaryDisc,
              @SecondaryDisc",
              custNum,primaryDisc,secondaryDisc).ToList<TempTechDisciplines>();
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, setting the actual value should fix it.
0

If a parameter value (CustomerNum) is null, the parameter will not be serialized, and this error will occur.

Check and add default values for each parameter you add ('0' for CustomerNum), and check if CustomerNum <> 0 instead IS NULL if needed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.