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 
5
  • 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... Commented Feb 1, 2016 at 7: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. Commented Feb 1, 2016 at 7:28
  • Now I passed the parameter Commented Feb 1, 2016 at 7:36
  • string str = "DeleteStudentDetails"; DbCommand storedProcCommand = database.GetStoredProcCommand(str); database.AddInParameter(storedProcCommand, "@p_ID", DbType.Int64,ID); ds = database.ExecuteDataSet(storedProcCommand); Commented Feb 1, 2016 at 7:36
  • but still the entire rows were getting deleted Commented Feb 1, 2016 at 7:36

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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#

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.