Procedure or function 'SP1' expects parameter '@p2', which was not supplied Mvc4/
  • hi  please i use proc to add record in database but i'm facing this error please how i can fix it thank you;

     public void AddClient(Client ClientD)

                    SqlConnection conx = new SqlConnection(ConfigurationManager.AppSettings["cnx"]);
                    Client Clien1 = new Client();
                    //SqlCommand cmd = new SqlCommand("insert into Client values(@p1,@p2,@p3,@p4,@p5)", conx);
                    SqlCommand cmd = new SqlCommand("SP1", conx);
                    cmd.CommandType = CommandType.StoredProcedure;

                    SqlParameter p1 = new SqlParameter();
                    p1.ParameterName = "@p1";
                    //p1.DbType = DbType.Int32;
                    p1.Value = Clien1.IdClient;

                    SqlParameter p2 = new SqlParameter();
                    p2.ParameterName = "@p2";
                    //p2.DbType = DbType.String;
                    //p2.Size = 60;
                    p2.Value = Clien1.NomClient;

                    SqlParameter p3 = new SqlParameter();
                    p3.ParameterName = "@p3";
                    //p3.DbType = DbType.String;
                    //p3.Size = 60;
                    p3.Value = Clien1.Pass;

                    SqlParameter p4 = new SqlParameter();
                    p4.ParameterName = "@p4";
                    //p4.DbType = DbType.String;
                    //p4.Size = 60;
                    p4.Value = Clien1.Pass;

                    SqlParameter p5 = new SqlParameter();
                    p5.ParameterName = "@p5";
                    //p1.DbType = DbType.Int32;
                    p5.Value = Clien1.Age;



        My proc in sql is that:

    create proc SP1(@p1 int,@p2 varchar(30),@p3 varchar(30),@p4 varchar(30),@p5 int)
    insert into Client values(@p1,@p2,@p3,@p4,@p5)

    my table Client is that:

    create  table Client(IdClient int primary key,
    NomClient varchar(50),
    Pass char(3),
    Email varchar(30),
    Age int)
    thank you friends ;) .
  • 5 Comments sorted by
  • everything seems perfect there should not be error.
  • yes but i have error i don't know why :/ .thank you ...
  • Use Entity Framework to make your life easier as a programmer.
  • Vote Up0Vote Down Jaguar777Jaguar777
    51.00 Karma Accepted Answer
    Go to my github url and download this helper.cs file and add it to your project.
    Add this on top of your code:
    using DbHelper=Microsoft.ApplicationBlocks.Data.SqlHelper;
    Then change your function to this below.If result>0 then client is inserted.If result=0 then client not inserted  or an error occurred.
    public int AddClient(Client ClientD)
               var connectionstring = ConfigurationManager.AppSettings["cnx"];
               var storeproc = "";
               var par=new SqlParameter[4];
               par[0]=new SqlParameter("IdClient",ClientD.IdClient);
               par[1]=new SqlParameter("NomClient",ClientD.NomClient);
               par[2]=new SqlParameter("Pass",ClientD.Pass);
               par[3]=new SqlParameter("Age",ClientD.Age);

                   int result = DbHelper.ExecuteNonQuery(
                           new SqlConnection(connectionstring),
                           CommandType.StoredProcedure, storeproc,
                   return result > 0 ? 1 : 0; //if result>0 then everything is fine
               catch (Exception)
                   return 0;
    I attached a sample project but i advise you to start using Entity Framework to make your life easier.

  • Vote Up0Vote Down Jaguar777Jaguar777
    51.00 Karma Accepted Answer
    Make IdClient column identity unless you have client id with you.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Popular Posts of the Week