0

Hi I am not able to delete a single row in MVC, where if i try to delete one row the entire rows were getting deleted. Here is my delete method

public string DeleteRegistrationForm(int ID)
{
    string Msg = null;
    Database database = DatabaseFactory.CreateDatabase();
    DataSet ds = new DataSet();
    // List<Registrationform> results = new List<Registrationform>();
    try
    {
        string str = "DeleteStudentDetails";
        DbCommand storedProcCommand = database.GetStoredProcCommand(str);
        ds = database.ExecuteDataSet(storedProcCommand);
        ds.GetChanges();

        Msg = "DeleteOperationSuccessful";

       // ds.delete("id = ID"); 
         // results = ds.Tables[0].AsEnumerable()
         //             .Select(row => new Registrationform
         //             {
         //                 Id = 0,
         //                 Name = null,
         //                 Age = 0,
         //                 EMailId = null,
         //                 phNumber = null)
         //             }).ToList();
    }

    catch
    {
        Msg = "DeleteOperationFailed";
    }

    return Msg;
}

Here is my stored procedure

ALTER procedure [dbo].[DeleteStudentDetails]
as
begin
delete from  Registrationform
where ID = ID
end
Simon Karlsson
  • 4,090
  • 22
  • 39
Nandish Y
  • 21
  • 2
  • 4
  • 3
    And what does your `DeleteStudentDetails` stored proc look like? What does `database.ExecuteDataSet` do? The fact that you're not using the `ID` parameter makes it hard for me to see how you'd expect it to know which row to delete... – Jon Skeet Feb 01 '16 at 07:23
  • row implies that you are working with front-end..., there are hundreds of examples on how to delete a single record. if you SP works then it should be fine. BTW you are not sending and ID or any type of param with your SP.. which implies there is no filter. Anyway please endure you handle the front-end of delete a row... that is a different story... both are not really MVC framework. Hope this helps. – Seabizkit Feb 01 '16 at 07:28
  • Now I passed the parameter – Nandish Y Feb 01 '16 at 07:36
  • string str = "DeleteStudentDetails"; DbCommand storedProcCommand = database.GetStoredProcCommand(str); database.AddInParameter(storedProcCommand, "@p_ID", DbType.Int64,ID); ds = database.ExecuteDataSet(storedProcCommand); – Nandish Y Feb 01 '16 at 07:36
  • but still the entire rows were getting deleted – Nandish Y Feb 01 '16 at 07:36

2 Answers2

4

In addition to @Jezzabeanz answer you need to edit your procedure.. The WHERE ID=ID part in your query is always true and ALWAYS deletes all records.

First, Change your stored proc to include parameters.

ALTER procedure [dbo].[DeleteStudentDetails](@ID INT) 
as begin 
delete from Registrationform where ID=@ID 
end

And then include @Jezzabeanz answer also to achieve what you need. Hope this helps.

Abdul Hameed
  • 1,025
  • 12
  • 27
3

You need to add a parameter before you execute your Stored Procedure. I've never seen it done your way before. The following code is a rough example and untested.

 DbCommand storedProcCommand = database.GetStoredProcCommand(str);
storedProcCommand.Parameters.Add("@ID", SqlDbType.Int).Value = ID;

More info here: Passing parameter to stored procedure in C#

Community
  • 1
  • 1
ZeroBased_IX
  • 2,667
  • 2
  • 25
  • 46