473,394 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

adding rows from 1 dataset to another

Hi all

im a bit confused here, I m trying to add the rows from 1
dataset.tables(0).rows to another ds2.tables(0).rows.add(row) there is
probably a better way to do this than going looping true the rows (if you
know tell me ;p), but that is how im trying to do this atm.
at a certain point i get an error message : This row already belongs to
another table.
i dont know w i am doing wrong (not that it is nice code but i added it
below anyway)

i'll try to explain the situation a bit, i have groups that can contain
elements or other groups and i have to go true a group to c if it has
subgroubs, then go true the subgroups ... there is no way to tell how many
levels there will be. I'm not even sure if it isn't possible to create an
sql statement to get the data all sugestions are welcome.

tnx

--
Juchtmans Eric
Omnipack

Private Function filterGroepen(ByVal gr As clsArtGroep) As DataSet

Try

Dim ds As DataSet

ds = SqlHelper.ExecuteDataset(gstrCnn, CommandType.Text, "Select artgdArtgID
from tblArtgroepdeel where artgdArtgID <> 0 " & _

"and artgdMainArtgID = " & gr.ID & "")

Dim rij, rij2,r As DataRow

Dim dssub As DataSet

Dim g As New clsArtGroep

For Each rij In ds.Tables(0).Rows

g.ID = (CInt(rij(0)))

dssub = filterGroepen(g)

For Each rij2 In dssub.Tables(0).Rows

r = rij2

ds.Tables(0).Rows.Add(r)

Next

Next

Return ds

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

End Function

structure of the tables to give you an idea (not complete)

tblMAIN (ID, NAME)

tblSUB (ID, MAINID, ELEMENT, SUBID) (or element is filled in or SUBID)
Nov 20 '05 #1
7 1661
Cor
Hi EricJ,

First do us next time a favor, paste your code in a textbox and then in the
message, this is terrible reading.

I can only see a part of your code (it is enough I think you can test it
yourself).

I think this is not right

You first have to make a datarow with the structure from ds
r=ds.Tables(0).newrow
r = rij2 If this will go, I don't believe I copy the selective Items, I think I will
try/see tomorrow if there is not a more clever way if nobody gives us an
answer about that

And then add the new row to the ds.Tables(0).Rows.Add(r)


I hope you succeed,

Because I cannot answer your post more I think today.

Let me know OK?

Cor

Nov 20 '05 #2
Cor
Hi Eric,
I look to it tomorrow,
What I ment was, paste it in a notebook and back in this message, but for me
it is OK in this way also but some don't like this either.
Cor
Nov 20 '05 #3
it will never be perfect for all :/
i just had a thought (stupid actually) if i start putting the data in a
string from the start it should work and probably be faster (ill have the
problem later on but for now i think i can continue).

but there should be a way to add all the rows from a dataset table to
another dataset table w the same structure, at least i think it should be
possible.

eric

"Cor" <no*@non.com> wrote in message
news:OJ**************@TK2MSFTNGP10.phx.gbl...
Hi Eric,
I look to it tomorrow,
What I ment was, paste it in a notebook and back in this message, but for me it is OK in this way also but some don't like this either.
Cor

Nov 20 '05 #4
ok this is working (seems i had a for loop to mutch) it would probably work
w the datasets 2

Try
gr = ANode.Tag
sb.Append(gr.ID)
filterGroepen(gr, sb)
Dim strSql As String
strSql = sb.ToString(0, sb.Length)
ds = SqlHelper.ExecuteDataset(gstrCnn, CommandType.Text, "SELECT
distinct artgID, " & _
"artgNaam, artgOms FROM tblArtgroep WHERE artgID not in
(" & strSql & ")")
catch ex As Exception
MessageBox.Show(ex.Message)
End Try

Private Sub filterGroepen(ByVal gr As clsArtGroep, ByRef sb As
System.Text.StringBuilder)
Try
Dim ds As DataSet
Dim r As DataRow
ds = SqlHelper.ExecuteDataset(gstrCnn, CommandType.Text, "Select
artgdArtgID from tblArtgroepdeel " & _
"where artgdArtgID <> 0 and artgdMainArtgID = " & gr.ID &
"")
Dim rij As DataRow
Dim g As New clsArtGroep
For Each rij In ds.Tables(0).Rows
g.ID = (CInt(rij(0)))
sb.Append(", ")
sb.Append(g.ID)
filterGroepen(g, sb)
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Nov 20 '05 #5
Cor
Hi Eric,

I found this on MSDN it is a part of an example describing the itemarray in
a datarow.

I was looking for that ExecuteDataset when I did realize you did make your
own class for datasets (Like I do myself also by the way). :-)))

But it makes it difficult to trace your problem. But I think this fits
almost totaly your problem and if not you can look for that itemarray as a
datarow property
So why would I do that.

Private Sub CreateRowsWithItemArray()
' Make a DataTable using the function below.
Dim dt As DataTable = MakeTableWithAutoIncrement()
Dim dr As DataRow
' Declare the array variable.
Dim myArray(1) As Object
' Create 10 new rows and add to DataRowCollection.
Dim i As Integer
For i = 0 to 9
myArray(0) = DBNull.Value
myArray(1)= "item " & i.ToString()
dr = dt.NewRow()
dr.ItemArray = myArray
dt.Rows.Add(dr)
Next
PrintTable(dt)
End Sub

I hope this helps.

Cor
Nov 20 '05 #6
credit where it belongs ;p i use the data access application block for .NET
for the data access and calling stored procedures ... (bit lazy but it comes
in handy ;) i do make classes for all my tables that handle elements of that
table :)

but i'll have a look at the itemarray you suggested it looks interesting.

tnx for the time :)

eric
Nov 20 '05 #7
Cor
Hi Eric,

I am widening my scoop a little bit.

And while looking in the adonet group, I saw an answer from Kevin about the
ImportRow.

This is all that I found about it on MSDN, but I think it is something you
can try.
I go now looking what that nice example I got from Fergus can do for me.

Calling NewRow adds a row to the table using the existing table schema, but
with default values for the row, and sets the DataRowState to Added. Calling
ImportRow preserves the existing DataRowState, along with other values in
the row.

Cor

Nov 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
2
by: a | last post by:
I get the error that "Argument"1": cannot convert from 'string' to 'System.Data.DataRow' at the foreach loop to add rows to the DataTable...any ideas how to fix this? Paul ...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
1
by: psb | last post by:
I thought this was weird?? is this a bug in framework 1.0??? (1.0 is the version I am running against) --------------------------- dim dtAll as new datatable dim dtTmp as datatable dtTmp =...
10
by: Trevor | last post by:
Hey, I am trying to do this tutorial on the microsoft site : http://msdn.microsoft.com/library/default.asp? url=/library/en-us/dndotnet/html/usingadonet.asp I can get everything to work up to...
16
by: Geoff Jones | last post by:
Hi Can anybody help me with the following, hopefully simple, question? I have a table which I've connected to a dataset. I wish to add a new column to the beginning of the table and to fill...
2
by: Niels Jensen | last post by:
I have the following code in a Sub which is called by a do loop statement for each line starting with unit info in an e-mail based game that I play. I'm exctracting the keywords from the text and...
6
by: Rudy | last post by:
Hi all, I know this is easy, just can't seem to get it. I have a windows form, and a text box, with a value already in it. I need to add that value to a table. It's just one value, so the entire...
12
by: JMO | last post by:
I can import a csv file with no problem. I can also add columns to the datagrid upon import. I want to be able to start importing at the 3rd row. This will pick up the headers necessary for the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.