Stored Procedure Parameter not supplied

This is my model class same as table in database:

public class AttendanceModel {         public int UserId { get; set; }         [Display(Name = "Login As")]         public string UserRole { get; set; }         public string Password { get; set; }         public bool isActive { get; set; }         public string Email { get; set; }         public int Mobile { get; set; } } 

This is my view file. If I have any error please let me know:

 @model AttendanceManagement.Models.AttendanceModel  @{     ViewBag.Title = "LoginForm";     Layout = null; }  <!DOCTYPE html> <head>     <title>Login</title>     <!-- meta tags -->     <meta charset="UTF-8" />     <meta name="viewport" content="width=device-width, initial-scale=1.0" />     <meta name="keywords" content="Art Sign Up Form Responsive Widget, Audio and Video players, Login Form Web Template, Flat Pricing Tables, Flat Drop-Downs, Sign-Up Web Templates,         Flat Web Templates, Login Sign-up Responsive Web Template, Smartphone Compatible Web Template, Free Web Designs for Nokia, Samsung, LG, Sony Ericsson, Motorola Web Design" />     <!-- /meta tags -->     <!-- custom style sheet -->     <link href="~/LoginStyles/css/style.css" rel="stylesheet" />     <!-- /custom style sheet -->     <!-- fontawesome css -->     <link href="~/LoginStyles/css/fontawesome-all.css" rel="stylesheet" />     <!-- /fontawesome css -->     <!-- google fonts-->     <link href="//,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900,900i"           rel="stylesheet">     <!-- /google fonts-->  </head>  @using (Html.BeginForm()) {     <body>         <h1>Register</h1>         <div class="w3l-login-form">             <h2>Register Here</h2>             <div class="w3l-form-group">                 <label>Usertype:</label>                 <div class="group">                                             <select id="txtUser" class="form-control">                             <option>Admin</option>                             <option>Teacher</option>                             <option>Student</option>                         </select>                                     </div>             </div>              <div class="w3l-form-group">                 <label>Email:</label>                 <div class="group">                     <i class="fas fa-envelope"></i>                     <input type="email" class="form-control" placeholder="Email" required="required" id="txtEmail" />                 </div>             </div>              <div class="w3l-form-group">                 <label>Password:</label>                 <div class="group">                     <i class="fas fa-unlock"></i>                     <input type="password" class="form-control" placeholder="Password" required="required" id="txtPassword" />                 </div>             </div>              <div class="w3l-form-group">                 <label>Mobile:</label>                 <div class="group">                     <i class="fas fa-phone"></i>                     <input type="number" class="form-control" placeholder="Mobile No" required="required" id="txtMobile" />                 </div>             </div>              <input type="button" id="btnRegister" value="register" onclick="Register()" />         </div>     </body> }  @*@section Scripts {     @Scripts.Render("~/bundles/jqueryval") }*@  <script src="~/Scripts/jquery-1.10.2.min.js"></script> <script language="javascript">     function Register() {         $("#btnRegister").val('Please wait..');         $.ajax({             url: "@Url.Content("~/Attendance/Register")",             data: { Usertype: $("#txtUser").val(), Email: $("#txtEmail").val(), Password: $("#txtPassword").val(), Mobile: $("#txtMobile").val() },             cache: false,             type: "POST",             success: function (data) {                 if (data.StatusCode == 'OK') {                     alert("Successfull login.");                     window.location.href = '@Url.Content("~/Attendance/LoginForm")';                 } else {                     alert(data.Message);                 }             },             error: function (reponse) {                 alert("error : " + reponse);             }         });         $("#btnRegister").val('register');     } </script> 

This is my controller stored procedure method. I have checked it many times cannot find the error in the stored procedure:

 private AttendanceModel insertUserdetails(AttendanceModel register)  {      string constr = ConfigurationManager.ConnectionStrings["AttendanceConnect"].ToString();      DataTable dt = new DataTable();       using (SqlConnection con = new SqlConnection(constr))      {          using (SqlCommand cmd = new SqlCommand("attendance_insert", con))          {              using (SqlDataAdapter da = new SqlDataAdapter())              {                  cmd.CommandType = CommandType.StoredProcedure;                   cmd.Parameters.AddWithValue("pUserRole", SqlDbType.VarChar).Value = register.UserRole;                  cmd.Parameters.AddWithValue("pEmail", SqlDbType.VarChar).Value = register.Email;                  cmd.Parameters.AddWithValue("pPassword", SqlDbType.VarChar).Value = register.Password;                  cmd.Parameters.AddWithValue("pMobile", SqlDbType.VarChar).Value = register.Mobile;                   da.SelectCommand = cmd;                   con.Open();                  da.Fill(dt);              }          }      }       if (dt != null && dt.Rows != null && dt.Rows.Count > 0)      {          register = (from DataRow dr in dt.Rows                      select new AttendanceModel()                             {                                 UserId = Convert.ToInt32(dr["UserId"].ToString()),                                 UserRole = dr["UserRole"].ToString(),                                 Email = dr["Email"].ToString(),                                 Password = dr["Password"].ToString(),                                 Mobile = Convert.ToInt32(dr["Mobile"].ToString()),                                 isActive = Convert.ToBoolean(dr["isActive"].ToString())                             }).ToList().FirstOrDefault();      }       return register;  } 

This is my stored procedure code:

SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO  ALTER PROCEDURE [dbo].[attendance_insert]      @pUserRole varchar(10),     @pEmail varchar(50),     @pPassword varchar(50),     @pMobile varchar(50) AS BEGIN     SET NOCOUNT ON;      INSERT INTO AttendanceLogin (UserRole, Email, Password, Mobile, isActive)      VALUES (@pUserRole, @pEmail, @pPassword, @pMobile, 1) END 

Image shows parameter not provided and very difficult to get hold of error. Have checked model class, database table, Procedure parameter many times. PLz help me to note error as I repeat this mistake always.

Add Comment
1 Answer(s)

You have committed two mistakes that’s why you are getting "Parameter not supplied"


In this class AttendanceModel, the UserId is not set as primary key. Hence the stored procedure is expecting its value.


In your controller, you are expecting UserId here:

UserId = Convert.ToInt32(dr["UserId"].ToString()) 

This will obviously cause the error as it is not able to find UserId and you are not passing it in your stored procedure.

What you can do is add a [Key] data annotation to make it primary key. This would autogenerate the primary key on that field and you will get rid of this error:

public class AttendanceModel {     [Key] // <-- Add this. You need to import using System.ComponentModel.DataAnnotations     public int UserId { get; set; }     [Display(Name = "Login As")]     public string UserRole { get; set; }     public string Password { get; set; }     public bool isActive { get; set; }     public string Email { get; set; }     public int Mobile { get; set; } } 

And then check


Your code where you are inserting the data is incorrect. If you have used Stored Procedure, you need to pass "@pUserRole" whereas you have just passed pUserRole, and similar change in all the variables. Look at the change below:

 using (SqlDataAdapter da = new SqlDataAdapter())  {        cmd.CommandType = CommandType.StoredProcedure;          cmd.Parameters.AddWithValue("@pUserRole", SqlDbType.VarChar).Value = register.UserRole;        cmd.Parameters.AddWithValue("@pEmail", SqlDbType.VarChar).Value = register.Email;        cmd.Parameters.AddWithValue("@pPassword", SqlDbType.VarChar).Value = register.Password;        cmd.Parameters.AddWithValue("@pMobile", SqlDbType.VarChar).Value = register.Mobile;         da.SelectCommand = cmd;         con.Open();         da.Fill(dt);  } 
Answered on August 30, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.