Delete seleted rows from DataTable
Delete seleted rows from DataTable
Delete seleted rows from DataTable
Delete seleted rows from DataTable
Delete seleted rows from DataTable
Delete seleted rows from DataTable Delete seleted rows from DataTable Delete seleted rows from DataTable Delete seleted rows from DataTable Delete seleted rows from DataTable Delete seleted rows from DataTable Delete seleted rows from DataTable Delete seleted rows from DataTable
Delete seleted rows from DataTable Delete seleted rows from DataTable
Delete seleted rows from DataTable
Go Back  Xtreme Visual Basic Talk > > > Delete seleted rows from DataTable


Reply
 
Thread Tools Display Modes
  #1  
Old 09-19-2006, 11:29 AM
easoftware easoftware is offline
Freshman
 
Join Date: Aug 2006
Posts: 48
Default Delete seleted rows from DataTable


I am using VB in Visual Studio.Net 2003. I am new to VB, having worked with Delphi. I am using an Access Database and OleDbDataAdapters. I am having problems finding the correct way to delete some records from a DataTable.

An example of my database is (from baseball); Four tables: Teams, Players, Positions (1st Base, 2nd Base, Pitcher, Catcher) and PlayersAtPositions. Teams has a One to Many relationship to Players. Positions has a One to Many relationship to PlayersAtPositions. A player can be into PlayersAtPositions multiple times, because several players play at more than one position.

All the linking and updating is working fine. The problem I am having is deleting a player. PlayersAtPosition is a child of both Positions and Players, but since the data is displayed with the Positions table, I have it as a child of Positions.

I need to manually delete the player’s rows in PlayersAtPosition when a player is deleted. I can think of two ways:

1) Select all the rows from PlayersAtPosition with PlayerID = Player being deleted, and then call delete for each row:

Private Sub DeletePlayerAtPos1(ByVal aPlayerID As Integer)

Dim SelectStr As String = "[PlayerId] = '" & aPlayerID.ToString & "'"
Dim ToDelRows() As DataRow = DataSet1.PatPTable.Select(SelectStr)
Dim r As DataRow
For Each r In ToDelRows
r.Delete()
Next
PatPOleDbDataAdapter.Update(PatPTable)
End Sub

2) Use a Delete Query. I can get the delete query to work in Access, but how do I get a delete query to work in VB? I can create the Delete Query, and set up a parameter in the PlayerId field. I know I need to set the parameter:

DeletePatPOleDbDataAdapter.SelectCommand.Parameters("PlayerId").Value = aPlayerId.ToString()

How do I execute the query?
How do I or do I need to include the DeletePatPOleDbDataAdapter in my DataSet?
Should I use a delete query?

Thanks
Eric
Reply With Quote
  #2  
Old 09-19-2006, 08:57 PM
krixna krixna is offline
Newcomer
 
Join Date: May 2005
Posts: 9
Default

What you're looking for it's called ForeignKeyConstraint object. here's how you do it:
1. First you set the parent (player table) and child (playersatposition table) column on the ForeignKeyConstraint object.
2. Set the ForeignKeyConstraint.deleteRule to cascade.
3. Add that constraint to the dataset that contain player and playersatpositiontable.
4. Set Dataset.enforceconstraint to true.

After that everytime you delete a player, all the child row in the playersatposition will automatically be deleted.
Reply With Quote
  #3  
Old 09-20-2006, 12:05 PM
easoftware easoftware is offline
Freshman
 
Join Date: Aug 2006
Posts: 48
Default

Thanks for your help. It seems to be working correctly. But now I have a related question.

On my form, I right-click on my DataSet, then select “View Schema”. From the Toolbox, I drag a “Relation” object onto my Players table in my DataSet.xsd window.

In the Edit Relation window,
I set Parent Element to “Players”, and Child Element to “PlayersAtPosition”.

In Fields, the Key Field is “PlayerId” (from Players) and the Foreign Key Field is “Player Id” (from PlayersAtPosition).

In DataSet Properties, I have checked “Create foreign key constraint only”. Delete Rule is set to “Cascade”. Update Rule and Accept/Reject Rule are set to “Default”.

Should I set Update Rule and Accept/Reject Rule to “None” or should I leave them at “Default”?

Thanks again.
Eric
Reply With Quote
  #4  
Old 09-20-2006, 08:17 PM
krixna krixna is offline
Newcomer
 
Join Date: May 2005
Posts: 9
Default

To be safe set both update and acceptreject rule to cascade.
Rule.None enumeration would throw an exception when you try to update/delete a column that has a related row.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Delete seleted rows from DataTable
Delete seleted rows from DataTable
Delete seleted rows from DataTable Delete seleted rows from DataTable
Delete seleted rows from DataTable
Delete seleted rows from DataTable
Delete seleted rows from DataTable Delete seleted rows from DataTable Delete seleted rows from DataTable Delete seleted rows from DataTable Delete seleted rows from DataTable Delete seleted rows from DataTable Delete seleted rows from DataTable
Delete seleted rows from DataTable
Delete seleted rows from DataTable
 
Delete seleted rows from DataTable
Delete seleted rows from DataTable
 
-->