none
cant add rows to my dataset datatable RRS feed

  • Question

  • I am new to dataset and this is essentially what I am trying to do

    1. read data.xml file into a data table (suceed)
    2. create a dataset
    3. put datatable into dataset (so i can add rows, edit, ect...)
    4. add a row to the datatable in the dataset (error occurs here,  it saids my column "Field Name" does not exist, But I am sure it exist because the datatable works fine when i put it in datagridview)  I know I am accessing the table correctly but I cant figure out where I went wrong.  Here is the code:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


            Dim truefalse As Boolean
            truefalse = File.Exists("I:\Training\data.xml")
            If (truefalse = True) Then
                Dim ds As New DataTable
                ds.TableName = "table"
                ds.ReadXml("I:\Training\data.xml")
                Dim thedataset As DataSet = New DataSet("thedataset")
                thedataset.Tables.Add(ds.TableName.ToString)

                Dim newRow As DataRow = thedataset.Tables(ds.TableName.ToString).NewRow()
                newRow("Field Name") = TextBox1.Text.ToString    <======I get the ERROR Message here!
                newRow("Description") = TextBox2.Text.ToString
                newRow("Verify or Update?") = TextBox3.Text.ToString
                newRow("Link") = TextBox4.Text.ToString
                thedataset.Tables("table").Rows.Add(newRow)

                thedataset.Tables("tabe").AcceptChanges()

                thedataset.Tables("table").WriteXml("I:\Training\data.xml")
            Else
                MessageBox.Show("Cant find xml file")

            End If
        End Sub

     

     

    • Edited by acatmpf Thursday, January 28, 2010 1:01 AM hard to read at first
    Thursday, January 28, 2010 12:59 AM

All replies

  • You can try to write out the columns in the table  and see if they really exists.

                foreach (DataColumn c in dt.Columns)
                {
                    Console.WriteLine("Name: {0} Caption: {1}", c.ColumnName, c.Caption);
                }
    or it can be that you need to enclose the columnname in brackets: [Field Name]

    HTH
    //Michael
    This posting is provided "AS IS" with no warranties.
    Thursday, January 28, 2010 9:15 AM