SystemIndexOutOfRangeException error

N

.Net Sports

I'm drawing data from xml file, putting it into datarows, from
datatable subroutine (UseXMLDocument) , and I'm getting a
"System.IndexOutOfRangeException: There is no row at position 0" at the
" Data.Append(dtaData.Rows(0)("Name") & "<BR>") " line.
''''''''''''''
<%@ Page Language="VB" debug="true"%>

<SCRIPT LANGUAGE=VB RUNAT=SERVER>
Private dtaData As DataTable = New DataTable()

Private ID As String
Private Name As String
Private NewDataRow As DataRow
Private ItemsOnlineDataURL As String

Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim PreviousCategory As String = Nothing
Dim Data As StringBuilder = New StringBuilder()
Dim SalesRepID As String = Request.QueryString("SID")
Dim PurchaseLink As String =
"http://www.shoppe.com/ItemsOnline/Purchase.aspx?"
Dim Spacer As String = "<IMG SRC=spacer.gif>"

ItemsOnlineDataURL =
"http://www.shoppe.com/ItemsOnline/Data/salesrep1.xml"

Try
dtaData.Columns.Add("ID")
dtaData.Columns.Add("Name")
dtaData.Columns.Add("PurchaseID")
dtaData.Columns.Add("PkgName")
dtaData.Columns.Add("SelectBlurb")
dtaData.Columns.Add("PkgAmount")
dtaData.Columns.Add("PkgPhone")
dtaData.Columns.Add("DailyItem")
dtaData.Columns.Add("Custom")
dtaData.Columns.Add("WeeklyAmount")
dtaData.Columns.Add("MonthlyAmount")
dtaData.Columns.Add("Category")



UseXmlDocument()
Data.Append(dtaData.Rows(0)("Name") & "<BR>")

Dim drwData As DataRow
For Each drwData In dtaData.Rows
Dim SID As String = "SID=" & Convert.ToString(drwData("ID")) & "&"
Dim PurchaseTypeID As String =
Convert.ToString(drwData("PurchaseID"))
Dim PkgAmount As String =
FormatAmount(Convert.ToSingle(drwData("PkgAmount")))
Dim PkgPhone As String = Convert.ToString(drwData("PkgPhone"))
Dim PurchaseType As String =
(Convert.ToString(drwData("DailyItem")) )
''''''''''''

.....more coding can be provided if someone can in fact help decipher
why I'm getting the error.

TIA
netsports
 
K

KJ

Hi netsports,

Since dtaData is a newly created table, it has no rows yet. Therefore,
you cannot access dtaData.Rows(0), since it does not exist. To add rows
to a new table, use the NewRow() method of DataTable to create a row.
Then, set the values of the fields of that new row. Finally, call
dtaData.Rows.Add(yourNewRowObject).

Hope this helps.

-KJ
 
N

.Net Sports

KJ, thanks for the reply -
I have declared dtaData , and I thought I have declared a new row in
the declarations below (and in my code above):
'''''''''''''''
Private dtaData As DataTable = New DataTable()


Private ID As String
Private Name As String
Private NewDataRow As DataRow
''''''''''''''''''
netsports
 
K

KJ

The code:

Data.Append(dtaData.Rows(0)("Name") & "<BR>")

fails because dtaData.Rows has no rows in it (dtaData.Rows(0) =
Nothing).

So, before this line, assign a value to NewDataRow using the NewRow()
method, for example:

'assign:
NewDataRow = dtaData.NewRow()

'then set a value:

NewDataRow("Name") = "foo"

'finally, assign the row to your table's Rows collection:
dtaData.Rows.Add(NewDataRow)

'then you can do (no error) because Rows(0) is no longer Nothing:
Data.Append(dtaData.Rows(0)("Name") & "<BR>")

Give this a try and tell me what happens.
 
N

.Net Sports

Yeah, KJ, I think that does the trick (at least for now, as I have
encountered a different error downstream) .
I had to use the ConvertC# toVB.NET program since the initial code was
in C# , so it probably didnt do a true 100% conversion.
Thanks again
netsports
 

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

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
DewittMill
Top