Limna
Limna

Reputation: 403

Error converting data type nvarchar to int - while executing stored procedure

I have a stored procedure that performs Create, Edit, Delete and Read operations.

ALTER PROCEDURE [dbo].[CURDOpSP]    
@operation varchar(50), 
@regid int = 0,
@fname varchar(50) = null,
@lname varchar(50) = null,
@phone varchar(50) = null,
@email varchar(50) = null

AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
if(@operation = 'Insert')
BEGIN
    insert into Register(fname,lname,phone,email,status)values(@fname,@lname,@phone,@email,0);
END

else if(@operation = 'Select')
BEGIN
    select * from Register where status = 0;
END

else if(@operation = 'Edit')
BEGIN
    update Register set fname= @fname, lname = @lname, phone = @phone, email = @email where RegisterId = @regid;
    
END

else if(@operation = 'Delete')
BEGIN
    Update Register set status = 1 where RegisterId = @regid;
END
END

And I have called this procedure on Controller.

// GET: /Employee/Create
public ActionResult Create()
{
    return View();
}

// POST: /Employee/Create
[HttpPost]
public ActionResult Create(Register register)
{           
    if (ModelState.IsValid)
    {
        db.Database.ExecuteSqlCommand("dbo.CURDOpSP @p0, @p1, @p2, @p3, @p4", "Insert", register.fname, register.lname, register.phone, register.email);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(register);   
}   
    
// POST: /Employee/Delete/5        
public ActionResult Delete(int id)
{
    Register register = db.Registers.Find(id);
    if (register == null)
    {
        return HttpNotFound();
    }           
    db.Database.ExecuteSqlCommand("dbo.CURDOpSP @p0, @p1", "Delete",register.RegisterId); 
            
    db.SaveChanges();
    return RedirectToAction("Index");
}

When I try to create a new record, I'm getting this error:

Error converting data type nvarchar to int.

Source Error:

Line 40: if (ModelState.IsValid)

Line 41: {

Line 42: db.Database.ExecuteSqlCommand("dbo.CURDOpSP @p0, @p1, @p2, @p3, @p4", "Insert", register.fname, register.lname, register.phone, register.email);

Line 43: db.SaveChanges();

Line 44: return RedirectToAction("Index");

Other three operations are working fine.

If I place the parameter @regid in stored procedure, at the end of parameter declarations like:

ALTER PROCEDURE [dbo].[CURDOpSP]    
@operation varchar(50),     
@fname varchar(50) = null,
@lname varchar(50) = null,
@phone varchar(50) = null,
@email varchar(50) = null,
@regid int = 0

Then the creation will be fine. But now delete function will not work.

Upvotes: 3

Views: 1287

Answers (1)

Mairaj Ahmad
Mairaj Ahmad

Reputation: 14624

You need to pass other parameters as well when you call it in Delete method like this. I suppose the paramerter @regid is at end like you have posted in end.

db.Database.ExecuteSqlCommand("dbo.CURDOpSP @p0, @p1,@p2,@p3@,@p4,@p5", "Delete","","","","",register.RegisterId); 

Upvotes: 4

Related Questions