none
Datarow Null Exception, AND Can't declare as New... wha?? RRS feed

  • Question

  • Hello there!  I am running on 2.0 and using vb.  I have an xml file that I've got extracted into a dataset.  Unfortunately, it splits the integers into a different table!  So, for this reason, I'm trying to create a new recordset with one table so I can sort the darn thing.

    My problem is that I'm stuck on something...

    Here's one version (only one record is added for now; there will be more later)....

           
    Code Snippet
      
    For Each row In xmlDataSet.Tables(0).Rows

             Dim combineRow As DataRow
             combineRow(0) = row(0)
             combineTable.Rows.Add(combineRow)
         Next



    This throws an exception saying it's not set to an instance.  Okay, I'll buy that.  So, I try to add "New"...

    Code Snippet

        

     For Each row In xmlDataSet.Tables(0).Rows

              Dim combineRow As New DataRow
              combineRow(0) = row(0)
              combineTable.Rows.Add(combineRow)
          Next



    Now it says that it is not accessible because "New" is protected.  I NEED this row to be declared in the loop because it fills and gets added to the table en masse after all the records are added.  This code is sitting in a sub within a class, FYI.

    Thank you, in advance, for any assistance you may provide
    =Adam=


    EDIT:  Okay.  I thought I'd try just taking the column from one and add it to the other.  Here's the code:

    Code Snippet

    Dim transferColumn As DataColumn
    transferColumn = xmlDataSet.Tables(1).Columns(1)
    xmlDataSet.Tables(0).Columns.Add(transferColumn)


    It didn't like that. 

    [ArgumentException: Cannot add a SimpleContent column to a table containing element columns or nested relations.]

    I am so completely confused.  Doesn't .net support tables with multiple datatypes?  And if not, then how can I sort the tables simultaneously?  I looked up the dataview and sorts and they seem to apply only to one table at a time.  I am very frustrated.  Sad

    Thanks
    =A=


    Tuesday, August 7, 2007 2:55 PM

Answers

  • Hi,

     

    To create a DataRow, you must need to use the NewRow() method on your DataTable. This is to automatically populate the Datarow with the columns needed.

     

    Dim newRow As DataRow = myDataTable.NewRow()

     

    If what you are doing is to transfer data from your 2 datatables, then I guess you'll have to create a storage for you combined data and manually insert the rows there:

     

    Code Snippet

    Dim combinedData As New DataTable()

    'construct the needed fields for your combined data

    DIm newRow as DataRow = combinedData.NewRow()

    newRow("field1") = table1.Rows(i)("field1")

    newRow("field2") = table2.Rows(i)("field2")

    combinedData.Rows.Add(newRow)

     

     

    This is just a pseudocode so don't copy paste this in your program because this definitely will not execute.

     

    Also, just an advise, maybe you can just format your XML so that it will just be extracted on a single DataTable. You can use XSLT to transform your XML into a format that will be much friendlier.

     

    cheers,

     

    Paul June A. Domag

    Tuesday, August 7, 2007 5:40 PM
  • Thank you!  That did the trick.  Here's the full sub in case anyone is looking for this solution in the future...


    Code Snippet

    Public Sub printRows(ByVal sortBy As String)
            Dim table As New DataTable
            Dim row As DataRow
            'For Each table In xmlDataSet.Tables
            Me.looper = 0
            'Since the integer data is stored in separate tables, we need to create a new dataset and a new table to combine them
            Dim combinedData As New DataSet
            Dim combineTable As New DataTable
            combineTable.Columns.Add("word", Type.GetType("System.String"))
            combineTable.Columns.Add("timesSearched", Type.GetType("System.Int32"))
            combineTable.Columns.Add("result", Type.GetType("System.String"))
            'for each of the rows in our xml dataset, create a row in the new table
            For Each row In xmlDataSet.Tables(0).Rows
                Dim combineRow As DataRow = combineTable.NewRow()
                combineRow(0) = xmlDataSet.Tables(0).Rows(looper)(0)
                combineRow(1) = xmlDataSet.Tables(1).Rows(looper)(1)
                combineRow(2) = xmlDataSet.Tables(0).Rows(looper)(1)
                looper += 1
                combineTable.Rows.Add(combineRow)
            Next
            'Add the table to the dataset
            combinedData.Tables.Add(combineTable)
            For Each table In combinedData.Tables
                'declare the select set
                Dim selectedData() As DataRow
                'select
                selectedData = table.Select("", sortBy)
                For Each row In selectedData
                    printedRowsString += "               <tr>" + vbCrLf
                    printedRowsString += "                   <td class=""dBaseTableCell"">" + row(0) + "</td>" + vbCrLf
                    printedRowsString += "                   <td class=""dBaseTableCell"">" + row(1).ToString + "</td>" + vbCrLf
                    printedRowsString += "                   <td class=""dBaseTableCell""><a href=""" + row(2) + """ target=""new"">" + row(2) + "</a></td>" + vbCrLf
                    printedRowsString += "               </tr>" + vbCrLf
                    looper += 1
                Next
            Next
        End Sub





    Tuesday, August 7, 2007 6:41 PM

All replies

  • Hi,

     

    To create a DataRow, you must need to use the NewRow() method on your DataTable. This is to automatically populate the Datarow with the columns needed.

     

    Dim newRow As DataRow = myDataTable.NewRow()

     

    If what you are doing is to transfer data from your 2 datatables, then I guess you'll have to create a storage for you combined data and manually insert the rows there:

     

    Code Snippet

    Dim combinedData As New DataTable()

    'construct the needed fields for your combined data

    DIm newRow as DataRow = combinedData.NewRow()

    newRow("field1") = table1.Rows(i)("field1")

    newRow("field2") = table2.Rows(i)("field2")

    combinedData.Rows.Add(newRow)

     

     

    This is just a pseudocode so don't copy paste this in your program because this definitely will not execute.

     

    Also, just an advise, maybe you can just format your XML so that it will just be extracted on a single DataTable. You can use XSLT to transform your XML into a format that will be much friendlier.

     

    cheers,

     

    Paul June A. Domag

    Tuesday, August 7, 2007 5:40 PM
  • Thank you!  That did the trick.  Here's the full sub in case anyone is looking for this solution in the future...


    Code Snippet

    Public Sub printRows(ByVal sortBy As String)
            Dim table As New DataTable
            Dim row As DataRow
            'For Each table In xmlDataSet.Tables
            Me.looper = 0
            'Since the integer data is stored in separate tables, we need to create a new dataset and a new table to combine them
            Dim combinedData As New DataSet
            Dim combineTable As New DataTable
            combineTable.Columns.Add("word", Type.GetType("System.String"))
            combineTable.Columns.Add("timesSearched", Type.GetType("System.Int32"))
            combineTable.Columns.Add("result", Type.GetType("System.String"))
            'for each of the rows in our xml dataset, create a row in the new table
            For Each row In xmlDataSet.Tables(0).Rows
                Dim combineRow As DataRow = combineTable.NewRow()
                combineRow(0) = xmlDataSet.Tables(0).Rows(looper)(0)
                combineRow(1) = xmlDataSet.Tables(1).Rows(looper)(1)
                combineRow(2) = xmlDataSet.Tables(0).Rows(looper)(1)
                looper += 1
                combineTable.Rows.Add(combineRow)
            Next
            'Add the table to the dataset
            combinedData.Tables.Add(combineTable)
            For Each table In combinedData.Tables
                'declare the select set
                Dim selectedData() As DataRow
                'select
                selectedData = table.Select("", sortBy)
                For Each row In selectedData
                    printedRowsString += "               <tr>" + vbCrLf
                    printedRowsString += "                   <td class=""dBaseTableCell"">" + row(0) + "</td>" + vbCrLf
                    printedRowsString += "                   <td class=""dBaseTableCell"">" + row(1).ToString + "</td>" + vbCrLf
                    printedRowsString += "                   <td class=""dBaseTableCell""><a href=""" + row(2) + """ target=""new"">" + row(2) + "</a></td>" + vbCrLf
                    printedRowsString += "               </tr>" + vbCrLf
                    looper += 1
                Next
            Next
        End Sub





    Tuesday, August 7, 2007 6:41 PM