Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I created a stored procedure to Delete an object from the table Movie in my db and I called it from a DataAccessLayer method of my application (C#).

I passed the stored procedure as a parameter of the SqlCommand object together with the SqlConnection object, then I added the parameters of the stored procedure by using the method Parameters , which is called by the SqlCommand object.
The method doesn't work (the item has not been deleted); for it didn't throw any exception I suppose the issue is caused by a non-correspondence between the parameters in the stored procedure and those that I'm trying to add them here...
The stored procedure works fine.

What I have tried:

public bool Delete(Movie movie)  
        {
            try
            {
                bool result = false;
                string DeleteProcedure = "[dbo].nameOfTheProcedure";
                using (SqlConnection conn = DB.GetSqlConnection())
                {
                    using (SqlCommand cmd = new SqlCommand(DeleteProcedure, conn))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@MovieId", SqlDbType.NVarChar).Value = movie.Id;
                        cmd.Parameters.Add("@Title", SqlDbType.NVarChar).Value = movie.Title;
                        
                        cmd.Parameters.Add("@Director", SqlDbType.NVarChar).Value = movie.Director;                       
                        conn.Open();
                        result = cmd.ExecuteNonQuery() == 1;
                    }
                }
                return result;
            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }
Posted
Updated 10-Nov-20 20:30pm
v5
Comments
PIEBALDconsult 10-Nov-20 22:20pm    
Never delete.

1 solution

It's going to depend on what the SP actually looks like: it needs to be something like this:
SQL
CREATE PROCEDURE [dbo].[nameOfTheProcedure]
@MOVIEID NVARCHAR(10),
@TITLE NVARCHAR(MAX)  -- This should be irrelevent - the ID should be unique.
AS
   DELETE FROM [dbo].[YourDBTableName]
   WHERE Id = @MOVIEID
GO
 
Share this answer
 
v2
Comments
xhon 11-Nov-20 3:04am    
my stored procedure works, it's just that the C# method returns false instead than true because the item has not been deleted (I can delete it by calling my stored procedure on my db manager application though)
OriginalGriff 11-Nov-20 3:33am    
How do you know it works? How did you test it?
What is different about the way you tested it, compared to how you are calling it now?
xhon 11-Nov-20 4:15am    
the stored procedure works when I call it from the Sql Server Manager, whereas the method in Visual Studio where I call the stored procedure doesn't return the expected result
OriginalGriff 11-Nov-20 4:21am    
But when you call it from SSMS, you pass the parameters manually, and possibly differently.
So use the debugger and look at two things: Exactly what is in the parameters you pass, and exactly what does the ExecuteNonQuery call return?
How does that differ from the SSMS way?
xhon 11-Nov-20 5:34am    
no, I don't pass the parameters manually, I pass them directly from the C# method. I checked them by storing the results of the "cmd.Parameters.Add" statements on variables (one for each parameter added) and they are correct. I assigned the result of "cmd.ExecuteNonQuery == 1" to the variable "result" and the method returns it and it's false (so the result of ExecuteNonQuery==1 is false) instead than true

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900