HighTechTalks DotNet Forums  

Creating New DataSet from Old DS

Dotnet Data Tools microsoft.public.dotnet.datatools


Discuss Creating New DataSet from Old DS in the Dotnet Data Tools forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Neal Rogers via .NET 247
 
Posts: n/a

Default Creating New DataSet from Old DS - 03-18-2005 , 06:58 AM






hi

I am trying to create a new DS from an incoming (ex DBase query).
The Incoming one has data, which spawns (should) multiple records, depending on the data in the pertinent field.
ie Incoming has 18 rows, 6 of which indicate the need to create 2 - 7 new rows, and drop that old incoming row.

I try creating a ds based on the incoming one.
Then iterating thru the old one, and where the data dictates, calling a rtn which appends new rows (actually a copy of the old row, with the necessary changes made to it's data, and appended to the new DS, but..
I usually get
"Error: this row already belongs to another table" ...
(!!! So what.... I want to add it to a new DS... ???? weird.. why restrict a developer??)

So... anybody ??

To clarify .. heres the code.

private shared _NewDs as DataSet

Function xxx(byval In_ds as dataset) as dataset
Dim dr As DataRow
Dim i As Integer

_NewDs.Merge(In_DS) 'or _NewDs = ActsLocsDaysIn_DS

For i = 0 To ActsLocsDaysIn_DS.Tables(0).Rows.Count - 1
dr = ActsLocsDaysIn_DS.Tables(0).Rows(i)

If Not IsDBNull(dr.Item("sugDay")) Then
Select Case Trim(dr.Item("sugDay"))
Case "U" 'WTF
ReplaceAppendSuggestedDays(dr, "3,4,5")
Case "V" 'M,WTF
ReplaceAppendSuggestedDays(dr, "1,3,4,5")
Case Else
'nothing
End Select
Else
'Nothing
End If
Next

Return _NewDs
end function

Private Shared Sub ReplaceAppendSuggestedDays(ByVal drIn As DataRow, ByVal sDays As String)
Dim drNew As DataRow
Dim sArr() As String = Split(sDays, ",")
Dim i As Integer
Dim iNumDaysReqd As Integer = UBound(sArr)

Try
drNew = drIn
For i = 0 To iNumDaysReqd - 1
'Insert new rows per suggested day (1 "U" row ) suggests 4 new rows
If sArr(i) = "1" Then ' Monday
drNew.Item("sugDay") = "1"
ElseIf sArr(i) = "2" Then ' Tuesday
drNew.Item("sugDay") = "2"
ElseIf sArr(i) = "3" Then ' Wednesday
drNew.Item("sugDay") = "4"
ElseIf sArr(i) = "4" Then ' Thursday
drNew.Item("sugDay") = "8"
ElseIf sArr(i) = "5" Then ' Friday
drNew.Item("sugDay") = "G"
ElseIf sArr(i) = "6" Then ' Saturday
drNew.Item("sugDay") = "W"
ElseIf sArr(i) = "7" Then ' Sunday
drNew.Item("sugDay") = "1S"
End If
drNew.AcceptChanges()
AppendToDS(drNew)
Next
Catch er As Exception
Throw New Exception("Error AppendSugDays : " & er.Message)
End Try
End Sub

Private Shared Sub AppendToDS(ByVal drnew As DataRow)
_NewDs.Tables(0).Rows.Add(drnew)
End Sub

-------------------------------
From: Neal Rogers

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>DId7ArLIIkat6Re6Sok4Bg==</Id>

Reply With Quote
  #2  
Old   
Alvin Bruney [MVP - ASP.NET]
 
Posts: n/a

Default Re: Creating New DataSet from Old DS - 04-24-2005 , 05:49 PM






Rows are added by reference. You will need to call the copy function on the
row so that a copy can be returned which you then add to your new database.
Otherwise, a reference to the existing row is returned.

Quote:
(!!! So what.... I want to add it to a new DS... ???? weird.. why restrict
a developer??)
Adding references to the other tables is not allowed to avoid some nasty
circular reference issues.

--
Regards,
Alvin Bruney - ASP.NET MVP

[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
Now available @ www.lulu.com/owc
"Neal Rogers via .NET 247" <anonymous (AT) dotnet247 (DOT) com> wrote

Quote:
hi

I am trying to create a new DS from an incoming (ex DBase query).
The Incoming one has data, which spawns (should) multiple records,
depending on the data in the pertinent field.
ie Incoming has 18 rows, 6 of which indicate the need to create 2 - 7 new
rows, and drop that old incoming row.

I try creating a ds based on the incoming one.
Then iterating thru the old one, and where the data dictates, calling a
rtn which appends new rows (actually a copy of the old row, with the
necessary changes made to it's data, and appended to the new DS, but..
I usually get
"Error: this row already belongs to another table" ...
(!!! So what.... I want to add it to a new DS... ???? weird.. why restrict
a developer??)

So... anybody ??

To clarify .. heres the code.

private shared _NewDs as DataSet

Function xxx(byval In_ds as dataset) as dataset
Dim dr As DataRow
Dim i As Integer

_NewDs.Merge(In_DS) 'or _NewDs = ActsLocsDaysIn_DS

For i = 0 To ActsLocsDaysIn_DS.Tables(0).Rows.Count - 1
dr = ActsLocsDaysIn_DS.Tables(0).Rows(i)

If Not IsDBNull(dr.Item("sugDay")) Then
Select Case Trim(dr.Item("sugDay"))
Case "U" 'WTF
ReplaceAppendSuggestedDays(dr, "3,4,5")
Case "V" 'M,WTF
ReplaceAppendSuggestedDays(dr, "1,3,4,5")
Case Else
'nothing
End Select
Else
'Nothing
End If
Next

Return _NewDs
end function

Private Shared Sub ReplaceAppendSuggestedDays(ByVal drIn As DataRow,
ByVal sDays As String)
Dim drNew As DataRow
Dim sArr() As String = Split(sDays, ",")
Dim i As Integer
Dim iNumDaysReqd As Integer = UBound(sArr)

Try
drNew = drIn
For i = 0 To iNumDaysReqd - 1
'Insert new rows per suggested day (1 "U" row ) suggests 4
new rows
If sArr(i) = "1" Then ' Monday
drNew.Item("sugDay") = "1"
ElseIf sArr(i) = "2" Then ' Tuesday
drNew.Item("sugDay") = "2"
ElseIf sArr(i) = "3" Then ' Wednesday
drNew.Item("sugDay") = "4"
ElseIf sArr(i) = "4" Then ' Thursday
drNew.Item("sugDay") = "8"
ElseIf sArr(i) = "5" Then ' Friday
drNew.Item("sugDay") = "G"
ElseIf sArr(i) = "6" Then ' Saturday
drNew.Item("sugDay") = "W"
ElseIf sArr(i) = "7" Then ' Sunday
drNew.Item("sugDay") = "1S"
End If
drNew.AcceptChanges()
AppendToDS(drNew)
Next
Catch er As Exception
Throw New Exception("Error AppendSugDays : " & er.Message)
End Try
End Sub

Private Shared Sub AppendToDS(ByVal drnew As DataRow)
_NewDs.Tables(0).Rows.Add(drnew)
End Sub

-------------------------------
From: Neal Rogers

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

Id>DId7ArLIIkat6Re6Sok4Bg==</Id



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.