?
Solved

Typed Dataset Adding DataTable

Posted on 2005-04-26
5
Medium Priority
?
2,470 Views
Last Modified: 2013-12-03
I have an auto generated typed dataset from a Data > DataAdapter Control in VS.NET.
I have the need to create a datarow from the typed dataset with values that I collect in my application.
I am able to add the row to a typed datatable from the typed recordset but when it comes time to add the instance of the datatable to the recordset I get error: DataTable already exists in this DataSet.

I have managed to make this work by creating first just a generic DataSet and adding the typed DataTable to it as follows:

            Dim ds As DataSet = New DataSet("dsCustomErrors")
            Dim dt As New dsCustomErrors.CustomErrorsDataTable
            Dim dr As dsCustomErrors.CustomErrorsRow = dt.NewCustomErrorsRow

            Try
                dr.err_Msg = _exc.Message.ToString
                dr.err_AppName = _values.ApplicationName
                ds.DataSetName = _values.ApplicationName
                dt.Rows.Add(dr)
                ds.Tables.Add(dt)
                ds.WriteXml(GetFilePath())
            Catch ex As Exception
                Debug.WriteLine(ex.Message)

            End Try

But if I wanted to use the typed DataSet? How do I "merge" the DataTable back to the typed DataSet?
0
Comment
Question by:rommelb
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:Havagan
ID: 13870839
If all you want to do is add a new row to your existing datatable you don't need to add the datatable back to the dataset... just add the row and accept changes to reset the rowstate. If you want to add a second datatable to your dataset that mirrors the first datatable you'll have to Copy the table into the dataset.

Dim ds As DataSet = New DataSet("dsCustomErrors")
Dim dt As New dsCustomErrors.CustomErrorsDataTable
Dim dr As dsCustomErrors.CustomErrorsRow = dt.NewCustomErrorsRow

Try
     dr.err_Msg = _exc.Message.ToString
     dr.err_AppName = _values.ApplicationName
     dt.Rows.Add(dr)
     ds.AcceptChanges
     ds.WriteXml(GetFilePath())
Catch ex As Exception
     Debug.WriteLine(ex.Message)
End Try

Paul
0
 

Author Comment

by:rommelb
ID: 13870978
The strongly typed dataset declaration would look like this:
Dim ds As dsCustomErrors = New dsCustomErrors ' This is an instance of the auto generated typed dataset

Instead of the generic (that works currently but is not what I really wanted):
Dim ds As DataSet = New DataSet("dsCustomErrors")

I guess I don't see how that instance of the dsCustomErrors relates to the instance of CustomErrorsDataTable.
I really do not want to Copy the DataTable either though, I want it to be part of the instance of dsCustomErrors, ds.
0
 
LVL 6

Accepted Solution

by:
Havagan earned 1500 total points
ID: 13872433
I just cut and pasted your code from your first post and didn't really pay attention to how you were declaring your dataset. The change to make is only where you're attempting to add a datatable to your dataset -- just add a new row to the existing datatable/dataset. The important piece is below.

Instead of:

...
dt.Rows.Add(dr)
ds.Tables.Add(dt)
...

You want:

...
dt.Rows.Add(dr)
ds.AcceptChanges
...

It doesn't matter whether you're adding to a strongly typed dataset or not. To explain, any dataset (strongly typed or otherwise) is the owner of it's datatables and the datatables is the owner of it's datarows. If you attempt to simply add a datarow from one table to another, or add one datatable from one dataset to another, an excepton will be raised. (If you had wanted to copy the datatable, you instead have to use the .Copy (schema+data) or .Clone (schema only) method to duplicate the data from one datatable into another.)  In this case, since you only want to add a new row to your dataset and then write your dataset to xml, you simply add the new row to the existing datatable, call the .AcceptChanges method and call the .WriteToXml method of the dataset.

Paul
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 13877958
To merge the data from the Typed DataTable into the Typed DataSet, we do it this way:

(drums rolling: pseudo-code follows)
Dim ds As New dsCustomErrors
Dim dt As New dsCustomErrors.CustomErrorsDataTable
Dim dr As dsCustomErrors.CustomErrorsRow = dt.NewCustomErrorsRow
Try
     dr.err_Msg = _exc.Message.ToString
     dr.err_AppName = _values.ApplicationName
     dt.Rows.Add(dr)
     ds.Merge(dt)
Catch ex As Exception
     Debug.WriteLine(ex.Message)
End Try

0
 

Author Comment

by:rommelb
ID: 13878905
This is the part that was also missing, the link between the ds and the dt.
dt = ds.CustomErrors
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

809 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