Solved

How can I copy a rows from a dataview, edit rows and re-insert them back into the underlying dataset

Posted on 2011-02-21
2
1,013 Views
Last Modified: 2012-05-11
I have a dataset that I use to create a view filtered by certain criteria. I then take this view and create a temp. table. I go through the rows in the temp table and change some values and I need to take the rows in the temp table and add them to the original dataset.

What am I missing? My error is noted below in code next to the *****
When I step through the code everything is ok until the line with the error, where I am trying to add the row back to the original dataset


Dim dvView as dataview
Dim m_dstBaseSettings as Dataset
Dim m_ProjCode as String
Dim dtTempTable as DataTable
Dim drRow as Datarow 


dvDataview = m_dstBaseSettings.Tables("BASE_SETTINGS").DefaultView
 dvDataview.RowFilter = "SETTING_DESCRIPTION = 'REQUIRED' OR SETTING_DESCRIPTION = 'REQUIRED_W_DEFAULT'"
 dvDataview.Sort = "SETTING_DESCRIPTION, SETTING_NAME"
 'Create a temporary table so Station_Code can be added
 dtTempTable = dvDataview.ToTable.Copy
 'Loop through temp table
 For Each drRow In dtTempTable.Rows
     'Assign the stationcode to the new project station code
      drRow.Item("STATION_CODE") = m_ProjCode
      drRow.EndEdit()
      m_dstBaseSettings.Tables("BASE_SETTINGS").Rows.Add(drRow) ****ERROR HERE "Row already belongs to another table"

  Next

Open in new window

0
Comment
Question by:imonfireDAMMIT
2 Comments
 
LVL 11

Accepted Solution

by:
srikanthreddyn143 earned 500 total points
ID: 34947450
Row you are trying to add to the m_dstBaseSettings.Tables("BASE_SETTINGS") is referring to dtTempTable and so you are getting that error.

Why dont you create a row for original datatable and assign the values fro mtemp table and add it this way

For Each drRow In dtTempTable.Rows
Dim dr As Datarow = m_dstBaseSettings.Tables("BASE_SETTINGS").NewRow
dr(colname) = drRow(colName)
     'Assign the stationcode to the new project station code
      dr.Item("STATION_CODE") = m_ProjCode
     
      m_dstBaseSettings.Tables("BASE_SETTINGS").Rows.Add(dr)
  Next

0
 
LVL 1

Author Closing Comment

by:imonfireDAMMIT
ID: 34950663
Thank you! I didn't understand why what I was trying to do wasn't working. In my head it made perfect sense :) you saved me from a headache and now I can move on.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now