Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dataReader skips first record 1

Status
Not open for further replies.

kizmar2

Programmer
May 25, 2004
164
US
I'm having an issue with a data reader skipping the first returned record. I've double checked the SQL statment and I know it is actually skipping the first record.

I noticed that if I remove the "If dataReader.Read Then..." statement from around my data bind, it works, but then I can't set the label when no records show up.

Here's the code, any help would be appreciated.

Code:
Private Function FillHistoryGrid(ByVal studentID)
	' Pulling all history records for this week & this studentID
	sqlQuery = "..."

	' Execute command
	myConnection.Open()
	Dim myCommand As New SqlCommand(sqlQuery, myConnection)
	dataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

	If dataReader.Read Then
		' Bind selected data to the gridview
		Me.gvMealHistory.DataSource = dataReader
		Me.gvMealHistory.DataBind()
	Else
		Me.lblGridMessage.Text = "No records found."
	End If

	' Close connection stuff
	dataReader.Close()
	myConnection.Close()
End Function

KizMar
------------
 
You are binding a datareader directly to a datagrid???
That is a new one.

Don't

But if you still want it like this

Code:
If dataReader.hasrows Then
        ' Bind selected data to the gridview
        Me.gvMealHistory.DataSource = dataReader
        Me.gvMealHistory.DataBind()
    Else
        Me.lblGridMessage.Text = "No records found."
    End If

Christiaan Baes
Belgium

"My new site" - Me
 
a datatable, a dataset, any collection you want and even arrays.

I prefer collections (generic if possible).

Christiaan Baes
Belgium

"My new site" - Me
 
Ahhh... I was declaring it as: "Dim dataReader As System.Data.IDataReader"

If I use "Dim dataReader As SqlDataReader" and "If dataReader.HasRows Then" it fixes my problem.

Like I said, I'm a n00b. :p

KizMar
------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top