4 Replies - 5996 Views - Last Post: 13 March 2012 - 10:09 AM Rate Topic: -----

#1 redtoshiba   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-March 12

There is already an open DataReader associated with this Command ...

Posted 13 March 2012 - 08:29 AM

There is already an open DataReader associated with this Command which must be closed first


 Dim staffid = TextBox1.Text
    Dim conn As New SqlConnection
    conn.ConnectionString = SqlDataSource1.ConnectionString

    Dim command1 As New SqlCommand("SELECT StaffDetails.StaffID, SUM(HolidayRequests.RequestTotalDays) AS Expr1 FROM HolidayRequests INNER JOIN StaffDetails ON HolidayRequests.Username = StaffDetails.UserName WHERE (StaffDetails.StaffID = staffID) GROUP BY StaffDetails.StaffID, HolidayRequests.ApprovalStatus HAVING (HolidayRequests.ApprovalStatus = N'approved')", conn)

    conn.Open()
    Dim rdr As SqlDataReader
    rdr = command1.ExecuteReader
    Dim UpdateQuery As String
    While (rdr.Read())

        UpdateQuery = "UPDATE HolidayEntitlement set Holiday_Taken = @Expr1"
        command1.Parameters.AddWithValue("@Expr1", rdr("Expr1").ToString())

        'run update query

        command1.CommandText = UpdateQuery
        command1.ExecuteNonQuery()

         conn.close()
    End While 


It stops at command1.ExecuteNonQuery()

I've tried changing the where the connections/commands open and close and had had no success

Any Suggestions

Is This A Good Question/Topic? 0
  • +

Replies To: There is already an open DataReader associated with this Command ...

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: There is already an open DataReader associated with this Command ...

Posted 13 March 2012 - 08:34 AM

How about rediming the command object somewhere around line 9 or 10? You know breaking that command-reader relationship.
Was This Post Helpful? 0
  • +
  • -

#3 trevster344   User is offline

  • The Peasant
  • member icon

Reputation: 225
  • View blog
  • Posts: 1,526
  • Joined: 16-March 11

Re: There is already an open DataReader associated with this Command ...

Posted 13 March 2012 - 09:25 AM

What is it the last few weeks with everyone declaring variables like this:
Dim staffid = TextBox1.Text  



What kind of variable is that? I want to know so bad. ;P

Dim staffid as String = TextBox1.Text


Was This Post Helpful? 0
  • +
  • -

#4 nK0de   User is offline

  • Catch me As Exception
  • member icon

Reputation: 206
  • View blog
  • Posts: 823
  • Joined: 21-December 11

Re: There is already an open DataReader associated with this Command ...

Posted 13 March 2012 - 10:06 AM

View Posttrevster344, on 13 March 2012 - 09:55 PM, said:

What is it the last few weeks with everyone declaring variables like this:
Dim staffid = TextBox1.Text  



What kind of variable is that? I want to know so bad. ;P

Dim staffid as String = TextBox1.Text



Actually it is possible to declare variables like that in VB.NET without defining its type. But I don't think its good practice though.
Was This Post Helpful? 0
  • +
  • -

#5 trevster344   User is offline

  • The Peasant
  • member icon

Reputation: 225
  • View blog
  • Posts: 1,526
  • Joined: 16-March 11

Re: There is already an open DataReader associated with this Command ...

Posted 13 March 2012 - 10:09 AM

A terribly lazy and fatal practice. My point exactly. ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1