I am learning mvc from this video where I am getting data from a form and saving it to database. When I ran my code first it gave an error stating
Procedure or function ‘spAddEmployee’ expects parameter ‘@Employee_Name’, which was not supplied.
I followed a solution in which I initialized my stored procedure as NULL but now I am getting another error stating “@Parameter1 is not a parameter for procedure”.
This is my stored procedure
ALTER PROCEDURE [dbo].[spAddEmployee] ( @Employee_Name varchar(max) = NULL, @Employee_Age int = NULL, @Employee_Salary int = NULL, @Employee_City varchar(50) = NULL ) AS BEGIN INSERT INTO tblEmployee (Employee_Name, Employee_Age, Employee_Salary, Employee_City) VALUES (@Employee_Name, @Employee_Age, @Employee_Salary, @Employee_City) END
And this is my ADO.NET code:
public void AddEmployee(Employee employee) { string connectionString = ConfigurationManager.ConnectionStrings["EmployeeContext"].ConnectionString; using (SqlConnection con = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand("spAddEmployee", con) { CommandType = CommandType.StoredProcedure }; SqlParameter paramName = new SqlParameter(); paramName.ParameterName = "@Employee_Name"; paramName.Value = employee.Employee_Name; cmd.Parameters.Add(paramName); SqlParameter paramAge = new SqlParameter(); paramAge.ParameterName = "@Employee_Age"; paramAge.Value = employee.Employee_Age; cmd.Parameters.Add(paramAge); SqlParameter paramSalary = new SqlParameter(); paramSalary.ParameterName = "@Employee_Salary"; paramSalary.Value = employee.Employee_Salary; cmd.Parameters.Add(paramSalary); SqlParameter paramCity = new SqlParameter(); paramCity.ParameterName = "@Employee_City"; paramCity.Value = employee.Employee_City; cmd.Parameters.Add(paramCity); con.Open(); cmd.ExecuteNonQuery(); } }
And this is my controller code
[HttpPost] [ActionName("Create")] public ActionResult Create_Post(FormCollection formCollection) { Employee employee = new Employee(); employee.Employee_Name = formCollection["Employee_Name"]; employee.Employee_Age = Convert.ToInt32(formCollection["Employee_Age"]); employee.Employee_Salary = Convert.ToInt32(formCollection["Employee_Salary"]); employee.Employee_City = formCollection["Employee_City"]; EmployeeBuissnessLayer employeeBuissnessLayer = new EmployeeBuissnessLayer(); employeeBuissnessLayer.AddEmployee(employee); return RedirectToAction("Index"); }
I am getting error at line
employeeBuissnessLayer.AddEmployee(employee);
Please help I have tried a lot of solutions but none of them have worked.
Advertisement
Answer
This
SqlParameter paramName = new SqlParameter(); paramName.ParameterName = "@Employee_Name"; paramName.Value = employee.Employee_Name; cmd.Parameters.Add(paramName);
Should be
SqlParameter paramName = new SqlParameter(); paramName.ParameterName = "@Employee_Name"; // Always set the datatype paramName.SqlDbType = SqlDbType.NVarChar; // For strings, always set the length paramName.Size = 128; // Max string length // Ensure you pass DBNull not C# null paramName.Value = employee.Employee_Name ?? System.DBNull.Value; cmd.Parameters.Add(paramName);
Note: its best practice not to name your Stored Procedure with the sp
prefix.