parameterswebmatrixwebmatrix-2webmatrix-3

Parameter Placeholders are causing an error in Webmatrix


Every time I try using parameter placeholders either in a SELECT, INSERT, or UPDATE statement in WebMatrix I get the same error. It works fine in WHERE clause. What am I doing wrong I need some help.

Here is my code:

  if (IsPost && Validation.IsValid())
        {  
           Admission1 = Request["AdmDate1"];
           Discharge1 = Request["DisDate1"];
           LOC1 = Request["LOC1"];
           Program1 = Request["ProgramName1"];
           Notes = Request["Notes"];           
           var SQLUPDATE = "UPDATE SSI_Screening_New SET AdmissionDate1=@49, DischargeDate1=@50,  LOC1=@51, ProgramName1=@52, Notes1=@53 WHERE (ID) = (@0)";  
           db.Execute(SQLUPDATE, Admission1, Discharge1, LOC1, Program1, Notes, SSId);

           if (ModelState.IsValid) 
           {     
              Response.Redirect("~/ThankYou");
           }             
        }

Is there an alternative for using parameter placeholders in Webmatrix?

Here is the error I get every time:

Exception Details: System.Data.SqlClient.SqlException: Must declare the scalar variable "@49".

Thanks in advance for your help!


Solution

  • You are on the right track, but you must start your parameter placeholders at @0, then @1, @2, @3... and so forth. You can't jump to @49, which is why it's saying you must declare that variable. Try using @0 thru @5 and it will work.

               var SQLUPDATE = "UPDATE SSI_Screening_New SET AdmissionDate1=@0, DischargeDate1=@1,  LOC1=@2, ProgramName1=@3, Notes1=@4 WHERE (ID) = (@5)";