DataRow Collection?

A

Arpan

The following code creates a DataSet (with only one column named
"EmployeeName") in the Page_Load sub & populates a DataList:

<script runat="server">
'Build the DataSource
Function CreateDataSource() As ICollection
Dim dTable As New DataTable()
Dim dRow As DataRow

dTable.Columns.Add(New DataColumn("EmployeeName",
GetType(String)))
Dim i As Integer

For i = 0 To 9
dRow = dTable.NewRow()
dRow(0) = "SomeName" & i.ToString()
dTable.Rows.Add(dRow)
Next i

Dim dView As New DataView(dTable)
Return dView
End Function

Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
If Not (Page.IsPostBack) Then
'Bind 'DataView' to the DataSource
dList.DataSource = CreateDataSource()
dList.DataBind()
End If
End Sub
</script>

The DataList code hasn't been shown here. As such, the above code works
fine & populates the DataList with items starting from "SomeName0" to
"SomeName9".

Note the second line in the For...Next loop which says

dRow(0) = "SomeName" & i.ToString()

Now if the above line is changed to

dRow(i) = "SomeName" & i.ToString()

then the following error gets generated:

System.IndexOutOfRangeException: Cannot find column 1.

pointing to the

dRow(i) = "SomeName" & i.ToString

line. What's causing this error?

Thanks,

Arpan
 
G

Gozirra

You defined a table with a single column. When you retrieve a
reference to a new row it also has only a single column. dRow(0)
refers to the first column in the row. If you replace it with i, the
second iteration through the loop now points to dRow(1) which is not a
valid column in the row.

You should always assign your string value to dRow(0).

Hope this helps.
 
A

Arpan

Couldn't exactly understand your explanation, Gozirra. How does dRow(0)
refer to the first column in the row? The line

dTable.Columns.Add(New DataColumn("EmployeeName", GetType(String)))

already has created a column after which the line

dRow = dTable.NewRow()

within the For....Next loop instantiates 10 rows (one by one) using
"NewRow()" which simultaneously get added to the DataTable. So
replacing 0 with i - doesn't that mean that as i gets iterated, one by
one each row get added to the DataTable? In other words, doesn't
dRow(0) refer to the first row, dRow(1) refer to the second row,
dRow(2) refer to the third row so on & so forth?

I know I am wrong but could you please explain the concept more
lucidly?

Thanks,

Regards,

Arpan
 
G

Guest

dRow(0) refers to the first column in the row dRow, dRow(1) the second column
etc.

If you leave you code as

dRow(0) = "SomeName" & i.ToString()

it will add a row containing SomeName0 to the first column (coulumn 0) on
the first run through, SomeName1 to the first column (coulumn 0) on the
second run through etc.

If you change it to

dRow(i) = "SomeName" & i.ToString()

it will add a row containing SomeName0 the the first column (coulumn 0) on
the first run through, SomeName1 to the second none existant column (coulumn
1) on the second run through which gives you your error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top