What am i doing wrong in this dataset?

  • Thread starter Thread starter Shapper
  • Start date Start date
S

Shapper

Hello,

I am trying to create a dataset by adding rows to it in a For loop:

Dim dsNews As DataSet = New DataSet()
Dim row As DataRow = dsNews.Tables(0).NewRow()

dsNews.Tables.Add
dsNews.Tables(0).Columns.Add("title", GetType(String))
dsNews.Tables(0).Columns.Add("description", GetType(String))

For x = 1 To 10

With row
.Item("title") = "Some Title"
.Item("description") = "Some Description"
End With

dsNews.Tables(0).Add(row)

Next x

I get the error:
'Add' is not a member of 'System.Data.DataTable'.

What am I doing wrong?

Thanks,
Miguel
 
Miguel:

DataTable does not have an "Add" method. You have to "Add" to the DataRowCollection of DataTable. You will also need to create "NewRow" inside your loop. See the following code snippet for an example:

Dim Table As New DataTable
Dim Count As Integer
Dim NewRow As DataRow
For Count = 1 To 10
NewRow = Table.NewRow
NewRow.Item("Title") = "Some Title" & Count.ToString
NewRow.Item("Description") = "Some Description" & Count.ToString
Table.Rows.Add(NewRow)
Next


Hope this helps,

Paul Wu
www.rulemasters.com
 
Back
Top