Add Columns and Rows to Existing Dataset

Hello,

I already have dataset and want to add 3 more columns and insert data into it. How can I do it in VB.NET.
ASPDEVAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alfred A.Commented:
Hi,

I suppose you are really referring to a DataTable.  You can try the following:

Adding a Row example:

Dim dRow as DataRow = Dataset.Tables("NameOfDataTable").NewRow()
 dRow("columnName1"] = "columnValue1"
 dRow("columnName2"] = "columnValue2"
 Dataset.Tables("NameOfDataTable").Rows.Add(dRow)

Adding a Column example:

Dim dCol1 As DataColumn = New DataColumn("OrderID", System.Type.GetType("System.Int32"))
Dim dCol2 As DataColumn = New DataColumn("Description", System.Type.GetType("System.String"))
Dim dCol3 As DataColumn = New DataColumn("OrderName", System.Type.GetType("System.String"))

Dataset.Tables("NameOfDataTable").Columns.Add(dCol1)
Dataset.Tables("NameOfDataTable").Columns.Add(dCol2)
Dataset.Tables("NameOfDataTable").Columns.Add(dCol3)

I hope this helps.
0
ASPDEVAuthor Commented:
No, I need if possible to add Columns, Rows to an existing Dataset without having any schema issue.

My already existing Dataset has

Id       FName   LName

1       John     David

2        Peter     Troy


I tried Ds.Merge() method, it doesn't work either.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Alfred A.Commented:
Hi ASPDEV,

Have you tried using ds.Clone() method?  You can try the following:

1.  Clone an existing Dataset
2.  Copy the DataRows, existing and to be added, using ImportRow
3.  You can then overwrite the existing one with the new Dataset.
0
ASPDEVAuthor Commented:
How can add two datatables to a single dataset.

0
Alfred A.Commented:
Hi ASPDEV,

Check the following website:

http://msdn.microsoft.com/en-us/library/kax0zhe7%28VS.80%29.aspx

It explains about ds.Copy() and ds.Clone().
0
Alfred A.Commented:
Hi ASPDEV,

To join the tables, you can use the GetParentRow() method in combination with a "Relation" in the DataSet.
0
Alfred A.Commented:
Hi,

Or you can do the something like this, there might be some typos or missing parts but I just quickly generated one.

Dimt ds as Dataset = New DataSet()
ds.Tables.Add(Table1)
ds.Tables.Add(Table2)
ds.Relations.Add("JoinName", Table1.Columns("Test1"), Table2.Columns("Test1"))
Table2.Columns.Add("Test2", System.Type.GetType("System.String"), "Parent.Test2")
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CodeCruiserCommented:
Did you try something like this?

'dSet is a dataset

With dSet
    .Tables(0).Columns.Add("Address", GetType(String))

    For i as Integer = 0 to .Tables(0).Rows.Count - 1
         .Tables(0).Rows(i).Item(3) = "somevalue"
    Next
End With
0
ASPDEVAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.