Home All Groups Group Topic Archive Search About

Adding rows to a data table: Rows do not show up

Author
9 Oct 2006 9:49 PM
vvenk
Hello:

I defined a datatable with two columns:

        dtTable = New DataTable("Names")

        ' Add three column objects to the table.
        idColumn = New DataColumn()
        idColumn.DataType = System.Type.GetType("System.String")
        idColumn.ColumnName = "AER Number"
        idColumn.MaxLength = 20
        dtTable.Columns.Add(idColumn)

        idColumn = New DataColumn()
        idColumn.DataType = System.Type.GetType("System.String")
        idColumn.ColumnName = "Case Version"
        idColumn.MaxLength = 3
        dtTable.Columns.Add(idColumn)

I am trying to insert rows into the table:

        Dim row As DataRow

        row = dtTable.NewRow()
        row.BeginEdit()
        row("AER Number") = "123"
        row("Case Version") = "V3"

        row.AcceptChanges

The last tow raises an error, "Cannot perform this operation on a row not in
the table."

And if I do not have this line, the row count remains at 0.

What am I doing wrong?

Venki

Author
9 Oct 2006 10:51 PM
Kerry Moorman
Venki,

You need to add the row to the datatable:

        Dim row As DataRow

        row = dtTable.NewRow()

        row.BeginEdit()
        row("AER Number") = "123"
        row("Case Version") = "V3"
        row.EndEdit()

        dtTable.Rows.Add(row)

        dtTable.AcceptChanges()

Kerry Moorman


Show quoteHide quote
"vvenk" wrote:

> Hello:
>
> I defined a datatable with two columns:
>
>         dtTable = New DataTable("Names")
>
>         ' Add three column objects to the table.
>         idColumn = New DataColumn()
>         idColumn.DataType = System.Type.GetType("System.String")
>         idColumn.ColumnName = "AER Number"
>         idColumn.MaxLength = 20
>         dtTable.Columns.Add(idColumn)
>
>         idColumn = New DataColumn()
>         idColumn.DataType = System.Type.GetType("System.String")
>         idColumn.ColumnName = "Case Version"
>         idColumn.MaxLength = 3
>         dtTable.Columns.Add(idColumn)
>
> I am trying to insert rows into the table:
>
>         Dim row As DataRow
>
>         row = dtTable.NewRow()
>         row.BeginEdit()
>         row("AER Number") = "123"
>         row("Case Version") = "V3"
>
>         row.AcceptChanges
>
> The last tow raises an error, "Cannot perform this operation on a row not in
> the table."
>
> And if I do not have this line, the row count remains at 0.
>
> What am I doing wrong?
>
> Venki
>
Author
9 Oct 2006 11:07 PM
vvenk
Kerry:

Thanks. I realized my mistake.

Show quoteHide quote
"Kerry Moorman" wrote:

> Venki,
>
> You need to add the row to the datatable:
>
>         Dim row As DataRow
>
>         row = dtTable.NewRow()
>
>         row.BeginEdit()
>         row("AER Number") = "123"
>         row("Case Version") = "V3"
>         row.EndEdit()
>
>         dtTable.Rows.Add(row)
>
>         dtTable.AcceptChanges()
>
> Kerry Moorman
>
>
> "vvenk" wrote:
>
> > Hello:
> >
> > I defined a datatable with two columns:
> >
> >         dtTable = New DataTable("Names")
> >
> >         ' Add three column objects to the table.
> >         idColumn = New DataColumn()
> >         idColumn.DataType = System.Type.GetType("System.String")
> >         idColumn.ColumnName = "AER Number"
> >         idColumn.MaxLength = 20
> >         dtTable.Columns.Add(idColumn)
> >
> >         idColumn = New DataColumn()
> >         idColumn.DataType = System.Type.GetType("System.String")
> >         idColumn.ColumnName = "Case Version"
> >         idColumn.MaxLength = 3
> >         dtTable.Columns.Add(idColumn)
> >
> > I am trying to insert rows into the table:
> >
> >         Dim row As DataRow
> >
> >         row = dtTable.NewRow()
> >         row.BeginEdit()
> >         row("AER Number") = "123"
> >         row("Case Version") = "V3"
> >
> >         row.AcceptChanges
> >
> > The last tow raises an error, "Cannot perform this operation on a row not in
> > the table."
> >
> > And if I do not have this line, the row count remains at 0.
> >
> > What am I doing wrong?
> >
> > Venki
> >