C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Part 1:Delete multiple records from DataGrid with the help of CheckBoxes using Stored Procedure


Posted Date: 29 Jun 2006    Resource Type: Articles    Category: Databases
Author: Anuj RathiMember Level: Gold    
Rating: Points: 10



Introduction


Normally we use DataGrid to show our records in the web page. & uses CheckBoxes to select one or more records to delete from the DataGrid. If we want to delete any single row then there is no tough work. But Problems comes when we want to delete multiple records simultaniously by selecting rows with the help of CheckBoxes. Means, If the user Checked one or more CheckBoxes then all coresponding rows should be deleted. Lets see how:

How to select multiple records from DataGrid


First of all we have to identify the rows to be deleted. Suppose in our aspx page we have a DataGrid named dgSkills and it contains a template column with CheckBoxes. Now user clicked in the required fields.Now Our first job is to retrieve that rows. For this purpose your table must contain a PK field or Unique field to identify the rows uniquely.Lets see how:

Step 1: Find the Checked rows
Step 2: Concatinate with "," operator
Step 3: Perform Delete operation

Identifying selected rows



Declare a string variable that will store the PK ID of all selected rows

String strStateID = String.Empty;

string strSkillID
foreach(DataGridItem dgItem in dgSkill.Items)
if (((CheckBox)dgItem.FindControl("chkSelect")).Checked == true)
strSkillID += "," + dgItem.Cells[1].Text.ToString();

if(strSkillID != string.Empty)
strSkillID = strSkillID.Substring(1);


The last line remove the first "," from the string.
Now you have all the row like (2,5,7,10) and you have to perform delete operation. I am using upadate in place of Delete and place two extra Columns in my table :


Alter Table SkillMaster
ADD Active bit Default 1,DateOfDeletion dateTime


When I want to delete any record, I will set Active to "0" and DateOfDeletion to Current date. It is the good practice to keep record in your database. Because when you want to show all active users then you can do it easily by "where active = 0" clause and in future if you want to see any past record or deleted record then you can see easily because you have all records in your database. Most of the Companies use this type of strategy.

Continue........

Summary


In this article we saw that how to identify the selected rows & retrive them and how we can save all those rows on which we have to perform DELETE Operation or Update Operation.




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Replacement of xp_cmdshell
Previous Resource: Part 2: Delete multiple records from DataGrid with the help of CheckBoxes using Stored Procedure
Return to Discussion Resource Index
Post New Resource
Category: Databases


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use