How to pass exception from stored procedure to Post Action method

I have a Post Action method that passes two parameters to the repository method: -

 [HttpPost]
        public void Delete(string id, int classid )
        {
            repository.Deleteuserclass(id, classid);        }

      

where the repository method is: -

public void Deleteuserclass(string a, int u)

    {
        entities.deleteuserclass(a,u);}

      

and finally the repository method will call the following stored procedure: -

ALTER PROCEDURE dbo.deleteuserclass
@userid nvarchar(50),
@classid int
AS
Begin

Delete from Users_Classes where UserID = @userid  AND ClassID = @classid
END

      

How can I pass an exception to the action method to be raised on the database side if the stored procedure doesn't delete the record?

Edit: - I am updating the stored procedure like this: -

ALTER PROCEDURE dbo.deleteuserclass
@userid nvarchar(50),
@classid int
AS
Begin

Delete from Users_Classes where UserID = @userid  AND ClassID = @classid
if @@rowcount = 0     
    Raiserror('No record deleted',1,16) 
END

      

and I updated the action method: -

        public ActionResult Delete(string id, int classid ) {
try{
                       Thread.Sleep(1000);
                       Users_Classes u_c = r.FindUserClass(id, classid);
                       r.Deleteuserclass(id, classid);
                       return Json(new { IsSuccess = "True", id = u_c.UserID + u_c.AddedDate.ToString("ddMMyyyyHHmmss"), name = u_c.UserID }, JsonRequestBehavior.AllowGet);
                   }
                   catch (ArgumentNullException)
                   {
                       return Json(new { IsSuccess = "F1" }, JsonRequestBehavior.AllowGet);}
                   catch (DbUpdateConcurrencyException)
                   {return Json(new { IsSuccess = "F2" }, JsonRequestBehavior.AllowGet); }
                   catch (NullReferenceException)
                   {return Json(new { IsSuccess = "F4" }, JsonRequestBehavior.AllowGet); }
                   catch (Exception e)
                   {return Json(new { IsSuccess = "F5" }, JsonRequestBehavior.AllowGet);}

      

but the problem is that if the stored procedure did not delete any entry, the application threw a folloiwng NullReferenceException

on exception return Json(new { IsSuccess = "True", id = u_c.UserID + u_c.AddedDate.ToString("ddMMyyyyHHmmss"), name = u_c.UserID }, JsonRequestBehavior.AllowGet);

and it will not throw an exception that should already have been raised in the stored procedure level 'No record deleted'

before return

statment is reached .. so can be a problem?

+3


source to share


2 answers


What you want to do is pass the return value from your stored procedure, which is irregular. You would change your accumulated process something like this:

ALTER PROCEDURE dbo.deleteuserclass
@userid nvarchar(50),
@classid int
AS
Begin

Delete from Users_Classes 
where UserID = @userid  
AND ClassID = @classid

if @@rowcount = 0
    return -1

END

      

I'm not sure how you are calling the stored procedure (not as familiar with EF), but you should be able to capture the return value:



// notice this method now returns bool instead of void
public bool Deleteuserclass(string a, int u)
{
    return entities.deleteuserclass(a,u) > 0;
}

      

And then in your POST action method, just check the result of the bool

method DeleteUserClass()

:

[HttpPost]
public void Delete(string id, int classid )
{
    if (!repository.Deleteuserclass(id, classid))
        // no user deleted, handle accordingly        
}

      

+4


source


modify the stored procedure as

ALTER PROCEDURE dbo.deleteuserclass 
  @userid nvarchar(50), 
  @classid int 
AS 
Begin  
  Delete from Users_Classes  where UserID = @userid   AND ClassID = @classid  

  if @@rowcount = 0     
    Raiserror('User Deletion failed',1,16)
END 

      

Handle the exception and reuse the repository method



public void Deleteuserclass(string a, int u)      
{
  try{
         entities.deleteuserclass(a,u);
  }catch(Exception e)
  {
    throw e;
  }
} 

      

Finally, change the calling method as

public bool Deleteuserclass(string a, int u) 
{
    try{
     return entities.deleteuserclass(a,u) > 0; 
    }
    catch(Exception e)
    {
      //handle exception
      logError(e.Message);
    }
} 

      

0


source







All Articles