ondrovic
ondrovic

Reputation: 1175

Entity Framework Code First Stored Proc Incorrect Syntax / Must Declare Scalar Varible

Working on getting a stored procedure to execute from a web app. I can verify that the values are being populated like the should but I either get one of two errors.

Here is the relevant code, I have also looked around and tried a few different solutions, just stuck as to why it's not working.

// incorrect syntax near newCompany - have tried with both the parameter1 and parameter1.Value, parameter2 and parameter2.Value
public virtual int usp_TransferRecords(int oldCompany, int newCompany)
{
    SqlParameter parameter1 = new SqlParameter("OldCompany", oldCompany);
    SqlParameter parameter2 = new SqlParameter("NewCompany", newCompany);
    return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreCommand("usp_TransferRecords @OldCompany @NewCompany", parameter1, parameter2);
}

// Message = "Must declare the scalar variable \"@OldCompany\"." - have tried with both the parameter1 and parameter1.Value, parameter2 and parameter2.Value
public virtual int usp_TransferRecords(int oldCompany, int newCompany)
{
    SqlParameter parameter1 = new SqlParameter("OldCompany", oldCompany);
    SqlParameter parameter2 = new SqlParameter("NewCompany", newCompany);

    return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreCommand("usp_TransferRecords @OldCompany, @NewCompany", parameter1, parameter2);
}

This is the stored procedure:

CREATE PROCEDURE [dbo].[usp_TransferRecords]
    @OldCompany int,
    @NewCompany int
AS
BEGIN
    -- delete existing records from new company
    DELETE FROM [dbo].[Table] WHERE CompanyID = @NewCompany

    -- copy records from old company to new new company
    INSERT INTO [dbo].[Table] ([ID], [CompanyID], [City], [State], [Priority])
        SELECT NEWID(), @NewCompany, City, State, Priority
        FROM [dbo].[TABLE]
        WHERE CompanyID = @OldCompany

    -- reset record pages for both new and old companies
    SET NOCOUNT ON;

    -- deletes Dynamic Content Cache for both new and old companies
    DELETE FROM [dbo].[Table]
    WHERE CompanyID IN (@OldCompany, @NewCompany) 
      AND (ContentKey LIKE '%-generic-service-page' OR
           ContentKey LIKE '%-item-custom-service-page' OR
           ContentKey LIKE '%-about-page')

    -- deletes contents to so they will be regenerated
    DELETE FROM [dbo].[TABLE]
    WHERE CompanyID IN (@OldCompany, @NewCompany) 
      AND IsCityPage = 1
END

Web UI markup:

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "Form", enctype = "multipart/form-data" }))
{
    <h2 style="text-align:center"> This copy Records from old Company to new Company</h2>
    <div class="centered" style="width:400px">
    @Html.TextBox("oldCompany", id, new { onkeyup = "this.value=this.value.replace(/[^0-9]/g,'')", watermark = "Old Company #", @class = "watermark", style = "display:inline-block;" })
    @Html.TextBox("newCompany", String.Empty, new { onkeyup = "this.value=this.value.replace(/[^0-9]/g,'')", watermark = "New Company #", @class = "watermark", style = "display:inline-block;" })
    <input type="submit" name="submitButton" value="Copy" class="right orangebutton" style="position:absolute;left:62%;" />
    </div>
}

I got it to work properly by changing the following

// Original
public virtual int usp_TransferRecords(int oldCompany, int newCompany)
{
    SqlParameter parameter1 = new SqlParameter("OldCompany", oldCompany);
    SqlParameter parameter2 = new SqlParameter("NewCompany", newCompany);
    return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreCommand("usp_TransferRecords @OldCompany, @NewCompany", parameter1, parameter2);
}

to:

// Working
public virtual int usp_TransferRecords(int oldCompany, int newCompany)
{
   var @params = new SqlParameter[]
   {
       new SqlParameter("OldCompany", oldCompany),
       new SqlParameter("NewCompany", newCompany)
   };
        return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreCommand("usp_TransferRecords @OldCompany, @NewCompany", @params);
}

If any has any insight as to why this method worked over the other original one I would love to find out. Thanks all who replied.

Upvotes: 0

Views: 263

Answers (2)

ondrovic
ondrovic

Reputation: 1175

Here is what eventually

// Working
public virtual int usp_TransferRecords(int oldCompany, int newCompany)
{
   var @params = new SqlParameter[]
   {
       new SqlParameter("OldCompany", oldCompany),
       new SqlParameter("NewCompany", newCompany)
   };
   return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreCommand("usp_TransferRecords @OldCompany, @NewCompany", @params);
}

Upvotes: 1

Sam Axe
Sam Axe

Reputation: 33738

// Message = "Must declare the scalar variable \"@OldCompany\"." - have tried with both the parameter1 and parameter1.Value, parameter2 and parameter2.Value
public virtual int usp_TransferRecords(int oldCompany, int newCompany)
{
    SqlParameter parameter1 = new SqlParameter("OldCompany", oldCompany);
    SqlParameter parameter2 = new SqlParameter("NewCompany", newCompany);
    return ((IObjectContextAdapter)this).ObjectContext.ExecuteStoreCommand("usp_CopyRecord @OldCompany, @NewCompany", parameter1, parameter2);
}

Your error message tells you the problem.

"Must declare the scalar variable \"@OldCompany\"."

"@OldCompany" != "OldCompany"

Upvotes: 2

Related Questions