?
Solved

Need help dynamically adding columns to a dataset's datatable, for export to xml

Posted on 2008-10-01
2
Medium Priority
?
569 Views
Last Modified: 2008-10-01
I am pulling data from the company database.  The addresses are then geo-coded via the google geocoding service.
Since my dataset does not contain lat and long columns, i am trying to add them through code.  I am also removing rows where the LatLong could not be returned.

HOWEVER I find that the XML file does not contain the fields i have added. Please help!
Public Function DataSetToXML(ByVal oDS As DataSet) As DataSet
 
 
 
        '*Add columns to the datatable if they do not already exist
        If Not ColumnExists(oDS.Tables(0), "ProjectLat") Then
            'Doesnt exist, add the column
            oDS.Tables(0).Columns.Add(New Data.DataColumn("ProjectLat", Type.GetType("System.Decimal")))
            oDS.Tables(0).AcceptChanges()
        End If
 
        If Not ColumnExists(oDS.Tables(0), "ProjectLong") Then
            'Doesnt exist, add the column
            oDS.Tables(0).Columns.Add(New Data.DataColumn("ProjectLong", Type.GetType("System.Decimal")))
            oDS.Tables(0).AcceptChanges()
        End If
 
 
        'For Each orow As DataRow In oDS.Tables(0).Rows
        Dim i As Integer
        For i = oDS.Tables(0).Rows.Count - 1 To 0 Step -1
 
 
            Dim oitem As New GoogleSkunkWorksData.PCLSite
            Dim strPointCoOrd As String = ""
 
            With oitem
                .ProjectAddress1 = objDALHelper.NothingIfNull(oDS.Tables(0).Rows(i).Item("Address 1"))
                .ProjectAddress2 = objDALHelper.NothingIfNull(oDS.Tables(0).Rows(i).Item("Address 2"))
                .ProjectAddress3 = objDALHelper.NothingIfNull(oDS.Tables(0).Rows(i).Item("Address 3"))
                .ProjectCity = objDALHelper.NothingIfNull(oDS.Tables(0).Rows(i).Item("City"))
                .ProjectCountry = objDALHelper.NothingIfNull(oDS.Tables(0).Rows(i).Item("Country"))
                'Grab the geocoded address
                strPointCoOrd = GeoCode(oDS.Tables(0).Rows(i))
 
 
                Dim arrNewSplit As String() = Nothing
                arrNewSplit = strPointCoOrd.Split(",")
                Dim bSuccess As Boolean = True
                For Each s As String In arrNewSplit
                    If Not s <> "" Then
                        'Next iteration, the address cant be geo-coded!
                        'remove the row from the Dataset
                        oDS.Tables(0).Rows.RemoveAt(i)
                        oDS.Tables(0).AcceptChanges()
                        
                        bSuccess = False
                    Else
                        .ProjectLat = CDbl(arrNewSplit(0))
                        .ProjectLong = CDbl(arrNewSplit(1))
                    End If
                Next
 
              
            End With
        Next
        oDS.Tables(0).AcceptChanges()
        oDS.Tables(0).WriteXml(XMLDataPath, XmlWriteMode.IgnoreSchema)
        
        Return oDS

Open in new window

0
Comment
Question by:peterdidow
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 1500 total points
ID: 22619132
When writing a Dataset to XML, any columns which contain no data are ignored. When you are looping through the rows, you will need to add something (possibly an empty string) to the columns for them to be exported to the XML file.

Wayne
0
 

Author Comment

by:peterdidow
ID: 22619410
Thanks webtubbs!

it turned out that I DID have null values....for ALL of the co-ords.
In case your curious, here is what I had:

For Each s As String In arrNewSplit
                    If Not s <> "" Then
                        'Next iteration, the address cant be geo-coded!
                        'remove the row from the Dataset
                        oDS.Tables(0).Rows.RemoveAt(i)
                        oDS.Tables(0).AcceptChanges()
                       
                        bSuccess = False
                    Else
                        .ProjectLat = CDbl(arrNewSplit(0))
                        .ProjectLong = CDbl(arrNewSplit(1))
                    End If
                Next
I CHANGED TO:
For Each s As String In arrNewSplit
                    If Not s <> "" Then
                        'Next iteration, the address cant be geo-coded!
                        'remove the row from the Dataset
                        oDS.Tables(0).Rows.RemoveAt(i)
                        oDS.Tables(0).AcceptChanges()
                       
                        bSuccess = False
                    Else

                        oDS.Tables(0).Rows(i).Item("ProjectLat") = CType(arrNewSplit(0), Double)
                        .ProjectLat = objDALHelper.NothingIfNull(oDS.Tables(0).Rows(i).Item("ProjectLat"))

                        oDS.Tables(0).Rows(i).Item("ProjectLong") = CType(arrNewSplit(1), Double)
                        .ProjectLong = CType(arrNewSplit(1), Double)

                    End If
                Next

So thanks, i found the solution shortly after your comment!
0

Featured Post

Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

752 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