Dataset Error:IndexOutofRangeException

J

JenHu

Hi experts,

I have a dataadapter and datasets. I debugged it and gave me an error
on

LName = DS.Tables("AcctInfoTble").Rows(1)("LASTNAME")

error: There is no row at position 1.

WHAT DID I DO WRONG at the following code?
Thanks.
------------------------------------------------------------------------------------------------

Public Function GenerateA(ByRef strEmpID As String, ByRef
strClientAcct As String, ByRef strAcctStatus As String, ByRef
strAddressFlag As String, ByRef strNameFlag As String)
Dim strResult As New System.Text.StringBuilder
Dim GPConnection As SqlConnection
Dim dr As SqlDataReader
Dim GPDataset As New DataSet
GPConnection = New SqlConnection("workstation id=ADMIN;packet
size=4096;user id=MaintenanceHD;data source=AMKSQL2;p" & _
"ersist security info=True;initial catalog=HFC;password=maintainme")
Dim selectCMD As SqlCommand = New SqlCommand("SELECT DDACTNUM,
LASTNAME,FRSTNAME,BRTHDATE FROM UPR00102 where EmployID=' " +
strEmpID + "'", GPConnection)
Dim GPDataAdapter As SqlDataAdapter = New SqlDataAdapter
GPDataAdapter.SelectCommand = selectCMD

Dim EfundAcct As String
Dim LName As String
Dim FName As String
Dim MName As String
Dim DOB As String
Dim Loc As String

Try
GPConnection.Open()
Dim DS As DataSet = New DataSet
GPDataAdapter.Fill(DS, "AcctInfoTble")
EfundAcct = DS.Tables("AcctInfoTble").Rows(0)("DDACTNUM")
LName = DS.Tables("AcctInfoTble").Rows(1)("LASTNAME")
FName = DS.Tables("AcctInfoTble").Rows(2)("LASTNAME")
.......more codes
--------------------------------------------------------------------------------------------

*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
 
E

Earl

Still working on the "expert" part, but I think the answer here is within my
grasp ...

You are assigning your lastname from row 1, your firstname from row 2, etc.
I doubt that's what you mean to do, but just as importantly, you may not
even have a row 0, 1, 2, etc.

You appear to be pulling back one row to your dataset, so you'll just want
to assign all those values from row(0). To ensure that you actually returned
a row at all, you'll want to make a check for recordcount before trying to
assign the values.
 

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

Top