mentalhard
Well-known member
- Joined
- Aug 7, 2006
- Messages
- 123
- Programming Experience
- Beginner
How do i close the datareader object if i have used it like following:
then i want to use the same datareader for another query but i get an error
usually i assign the cmd.ExecuteReader to the datareader and use a loop to read but this approach is totally new for me.
Thanks
P.S. Currently i have solved it with closing and opening the connection for each query.
It works but, i am more than sure that there is a better approach than this as i have 10+ repeater controls and i believe that open/close connection 10 times for only few records per command is not the most effective way to go for.
VB.NET:
Me.Repeater1.DataSource = cmd.ExecuteReader()
then i want to use the same datareader for another query but i get an error
There is already an open DataReader associated with this Command which must be closed first
usually i assign the cmd.ExecuteReader to the datareader and use a loop to read but this approach is totally new for me.
Thanks
P.S. Currently i have solved it with closing and opening the connection for each query.
VB.NET:
cmd.Connection.Open()
Me.Repeater1.DataSource = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
Me.Repeater1.DataBind()
cmd = New SqlCommand("SELECT COMMAND", connection)
cmd.Connection.Open() ' HERE I OPEN THE CONN AGAIN
Me.Repeater2.DataSource = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
Me.Repeater2.DataBind()
' ETC.
It works but, i am more than sure that there is a better approach than this as i have 10+ repeater controls and i believe that open/close connection 10 times for only few records per command is not the most effective way to go for.