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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

first row of datareader results not showing in datagrid 1

Status
Not open for further replies.

JGALEY

IS-IT--Management
May 21, 2003
105
US
I have simple ASP.NET app (.NET 1.1) that allows the user to build a SQL query and show the results in a datagrid. It works OK, except that the first row does not show in the datagrid. If I run the query directly in SQL Server Query Analyzer, I get 17 rows. When I run it through the app, I get 16 rows.

Code:
Dim cn As New SqlConnection(Application("cnString"))
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
Dim strStatement As String = Me.txtQuery.Text

cmd.CommandText = "SELECT columns FROM table WHERE base condition AND " & strStatement

cmd.CommandType = CommandType.Text
cmd.Connection = cn
cn.Open()
dr = cmd.ExecuteReader(CommandBehavior.Default)
dr.Read()

Me.dgResults.DataSource() = dr
Me.dgResults.DataBind()

cn.Close()
dr.Close()

I know it has to be something pretty simple I am doing wrong. Can somebody throw me a bone? :)

Thanks,

Jason
 
Sure enough, that does the trick. I have never understood why sometimes you need dr.read() and other times you do not. I guess the act of binding it to the datagrid causes it to read?

Thanks!

Jason
 
I think what happens is doing dr.read reads the first record. Since datareaders are forward read only, when you binded the datareader after dr.read it couldn't see the first record anymore.

You would need dr.read if you were looping through the rows of the datareader to get its values.
 
Yes, ecannizzo is correct. Check out faq855-5662 on when and how the Read method would be implemented.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top