Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I need to delete a multiple rows in a sql table from asp.net in a single click...How to write a stored procedure for this...??????
Posted

 
Share this answer
 
Comments
idhris2011 14-Dec-11 6:58am    
thnx bro....but i should use "IN" keyword so that i can delete multiple rows at a time....so if i create a SP containing delete query with "IN" keyword, in what format should i pass the input parameter????
Does this help you :

http://www.aspsnippets.com/Articles/Select-and-delete-multiple-rows-in-ASP.Net-Gridview-control.aspx[^]

Specially Deleting multiple selected rows section

and this one :
http://geekswithblogs.net/dotNETvinz/archive/2009/02/22/gridview-multiple-delete-with-checkbox-and-confirm.aspx[^]

[EDIT]
After your comment I should say that you need to concatenate selected IDs at the application side and send them to a stored procedure. In the stored procedure you need to split received IDs by knowing the delimiter character and make a list of IDs so I have provided for you the code for the spliter :

SQL
declare @sep char(1)
declare @s varchar(max)
set @sep = ','
set @s= '16,17,18';

    WITH Pieces(pn, start, stop) AS (
      SELECT 1, cast(1 as int), CHARINDEX(@sep, @s)
      UNION ALL
      SELECT pn + 1, cast(stop + 1 as int), CHARINDEX(@sep, @s, stop + 1)
      FROM Pieces
      WHERE stop > 0
    ),shouldDelete(id) as
    (
    SELECT
      SUBSTRING(@s, start, CASE WHEN stop > 0 THEN stop-start ELSE LEN(@s) END) AS s
    FROM Pieces
  )
  delete from TableName where id in (select * from shouldDelete)


I have extracted splitter part from here : http://stackoverflow.com/questions/314824/t-sql-opposite-to-string-concatenation-how-to-split-string-into-multiple-reco[^]

16 , 17 and 18 are examples so convert @s to the parameter of stored procedure.
And remember that you have a limit of 8000 characters in case of using varchar for parameter type.

I'm sure you can handle rest of the work ;)

Good Luck
 
Share this answer
 
v3
Comments
idhris2011 14-Dec-11 7:16am    
Boss , I asked For Stored Procedures...?
i dono how to write a stored procedure for deleting using "IN"Keyword and how my parameters(n) should be passed....
Prashant Srivastava LKO 14-Dec-11 14:56pm    
Nice answer
Amir Mahfoozi 15-Dec-11 0:54am    
Thank you :)
Hi,

stored procedure is not that much difficult as ppl think, you only need to write normal delete query and passing some argument into it.

like,

SQL
delete * from table1 where column1 = @myParam


although this is not fully working SP. but i think you got idea. Here more info about Creating SP

then from asp.net application you can call this SP. Execute SP in ASP.NET

Hope this will help you,

thanks
-Amit,
 
Share this answer
 

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