Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. I don't know why I have this error

I don't know why I have this error

Scheduled Pinned Locked Moved Visual Basic
helpquestionannouncement
9 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Curious 2009
    wrote on last edited by
    #1

    What I try to do is reading any existed row in the table 'Info' (normally only 1 row existed) then update that row immediately, but I have an error when execute at cmd2.ExecuteNonQuery() - However, if I move the If room_N > 0 Then out of the While loop & place it under lrd.Close() then it works OK! Can some one know the reason?

        Dim ra As Integer
        Dim cmd As New SqlCommand
    
        Dim cnt\_del As Integer
        Dim room\_N As Integer
        Dim str As String
    
        con.ConnectionString = strOpen        
        con.Open()
    
        cmd.Connection = con
        cmd.CommandText = "SELECT \* FROM Info"
        Dim lrd As SqlDataReader = cmd.ExecuteReader()
    
        If lrd.HasRows Then
    
            Try
                'Dim lwr As SqlDataReader
    
                While lrd.Read()
                    ListBox1.Items.Clear()
    
                    str = lrd.GetValue(0).ToString() + ","
                    str = str + lrd.GetValue(1).ToString() + ","
                    str = str + lrd.GetValue(2).ToString() + ","
                    str = str + lrd.GetValue(3).ToString() + ","
                    str = str + lrd.GetValue(4).ToString() + ","
                    str = str + lrd.GetValue(5).ToString()+ ","
                    str = str + lrd.GetValue(6).ToString()
    
                    ListBox1.Items.Insert(cnt\_del, str)
                    cnt\_del = cnt\_del + 1
    
                    ListBox\_Available.Items.Clear()
                    room\_N = SearchAvailableRooms(lrd.GetValue(2), lrd.GetValue(3), lrd.GetValue(4), lrd.GetValue(0), lrd.GetValue(1))
    
                    If room\_N > 0 Then
                        cmd.CommandText = "UPDATE Reservations SET AvailRoomsRates = 'Trials'"
                        cmd.ExecuteNonQuery()
                    End If
    
                End While
    
                lrd.Close()
    
    
            Catch ex As Exception
                cnt\_error = cnt\_error + 1
                TextBox\_Error.Text = cnt\_error.ToString()
            End Try
        End If
    
        con.Close() 'Whether there is error or not. Close the connection.
    
    D L 2 Replies Last reply
    0
    • C Curious 2009

      What I try to do is reading any existed row in the table 'Info' (normally only 1 row existed) then update that row immediately, but I have an error when execute at cmd2.ExecuteNonQuery() - However, if I move the If room_N > 0 Then out of the While loop & place it under lrd.Close() then it works OK! Can some one know the reason?

          Dim ra As Integer
          Dim cmd As New SqlCommand
      
          Dim cnt\_del As Integer
          Dim room\_N As Integer
          Dim str As String
      
          con.ConnectionString = strOpen        
          con.Open()
      
          cmd.Connection = con
          cmd.CommandText = "SELECT \* FROM Info"
          Dim lrd As SqlDataReader = cmd.ExecuteReader()
      
          If lrd.HasRows Then
      
              Try
                  'Dim lwr As SqlDataReader
      
                  While lrd.Read()
                      ListBox1.Items.Clear()
      
                      str = lrd.GetValue(0).ToString() + ","
                      str = str + lrd.GetValue(1).ToString() + ","
                      str = str + lrd.GetValue(2).ToString() + ","
                      str = str + lrd.GetValue(3).ToString() + ","
                      str = str + lrd.GetValue(4).ToString() + ","
                      str = str + lrd.GetValue(5).ToString()+ ","
                      str = str + lrd.GetValue(6).ToString()
      
                      ListBox1.Items.Insert(cnt\_del, str)
                      cnt\_del = cnt\_del + 1
      
                      ListBox\_Available.Items.Clear()
                      room\_N = SearchAvailableRooms(lrd.GetValue(2), lrd.GetValue(3), lrd.GetValue(4), lrd.GetValue(0), lrd.GetValue(1))
      
                      If room\_N > 0 Then
                          cmd.CommandText = "UPDATE Reservations SET AvailRoomsRates = 'Trials'"
                          cmd.ExecuteNonQuery()
                      End If
      
                  End While
      
                  lrd.Close()
      
      
              Catch ex As Exception
                  cnt\_error = cnt\_error + 1
                  TextBox\_Error.Text = cnt\_error.ToString()
              End Try
          End If
      
          con.Close() 'Whether there is error or not. Close the connection.
      
      D Offline
      D Offline
      DJ Matthews
      wrote on last edited by
      #2

      What is the error you are getting? Just telling us you have an error won't help.

      C 1 Reply Last reply
      0
      • C Curious 2009

        What I try to do is reading any existed row in the table 'Info' (normally only 1 row existed) then update that row immediately, but I have an error when execute at cmd2.ExecuteNonQuery() - However, if I move the If room_N > 0 Then out of the While loop & place it under lrd.Close() then it works OK! Can some one know the reason?

            Dim ra As Integer
            Dim cmd As New SqlCommand
        
            Dim cnt\_del As Integer
            Dim room\_N As Integer
            Dim str As String
        
            con.ConnectionString = strOpen        
            con.Open()
        
            cmd.Connection = con
            cmd.CommandText = "SELECT \* FROM Info"
            Dim lrd As SqlDataReader = cmd.ExecuteReader()
        
            If lrd.HasRows Then
        
                Try
                    'Dim lwr As SqlDataReader
        
                    While lrd.Read()
                        ListBox1.Items.Clear()
        
                        str = lrd.GetValue(0).ToString() + ","
                        str = str + lrd.GetValue(1).ToString() + ","
                        str = str + lrd.GetValue(2).ToString() + ","
                        str = str + lrd.GetValue(3).ToString() + ","
                        str = str + lrd.GetValue(4).ToString() + ","
                        str = str + lrd.GetValue(5).ToString()+ ","
                        str = str + lrd.GetValue(6).ToString()
        
                        ListBox1.Items.Insert(cnt\_del, str)
                        cnt\_del = cnt\_del + 1
        
                        ListBox\_Available.Items.Clear()
                        room\_N = SearchAvailableRooms(lrd.GetValue(2), lrd.GetValue(3), lrd.GetValue(4), lrd.GetValue(0), lrd.GetValue(1))
        
                        If room\_N > 0 Then
                            cmd.CommandText = "UPDATE Reservations SET AvailRoomsRates = 'Trials'"
                            cmd.ExecuteNonQuery()
                        End If
        
                    End While
        
                    lrd.Close()
        
        
                Catch ex As Exception
                    cnt\_error = cnt\_error + 1
                    TextBox\_Error.Text = cnt\_error.ToString()
                End Try
            End If
        
            con.Close() 'Whether there is error or not. Close the connection.
        
        L Offline
        L Offline
        loyal ginger
        wrote on last edited by
        #3

        When you use a reader, the connection is not closed while the reading is going on. Therefore you cannot use a different command associated with that connection to do the update. Try to create a different connection and command to do the update.

        C 1 Reply Last reply
        0
        • D DJ Matthews

          What is the error you are getting? Just telling us you have an error won't help.

          C Offline
          C Offline
          Curious 2009
          wrote on last edited by
          #4

          I forgot to display the error message, here it is: Error while connecting SQL Server. There is already an open DataRedaer associate with this command which must be close first! However, since I have to update that row before scan the next row (while loop), I can't close it ... may I need another cmd2.ExecuteNonQuery()? But then I need to connect database again? Is there better command to avoid the abobe problems?

          1 Reply Last reply
          0
          • L loyal ginger

            When you use a reader, the connection is not closed while the reading is going on. Therefore you cannot use a different command associated with that connection to do the update. Try to create a different connection and command to do the update.

            C Offline
            C Offline
            Curious 2009
            wrote on last edited by
            #5

            You are right, however my database is already open & connect ... now I have open & connect anothe one with the same database?

            con.ConnectionString = strOpenSQLDatabase
            con.Open()

            con2.ConnectionString = strOpenSQLDatabase
            con2.Open()

            Is there other way around?

            L 1 Reply Last reply
            0
            • C Curious 2009

              You are right, however my database is already open & connect ... now I have open & connect anothe one with the same database?

              con.ConnectionString = strOpenSQLDatabase
              con.Open()

              con2.ConnectionString = strOpenSQLDatabase
              con2.Open()

              Is there other way around?

              L Offline
              L Offline
              loyal ginger
              wrote on last edited by
              #6

              Since nobody else offers any new ideas, I will post the modified version here, just for your information:

                  Dim ra As Integer
                  Dim cmd As New SqlCommand
              
                  Dim cnt\_del As Integer
                  Dim room\_N As Integer
                  Dim str As String
              
                  con.ConnectionString = strOpen        
                  con.Open()
              
                  cmd.Connection = con
                  cmd.CommandText = "SELECT \* FROM Info"
              
              **'con2 should be declared somewhere
                  con2.ConnectionString = strOpen        
                  con2.Open()
              
              'cmd2 should be declared somewhere
                  cmd2.Connection = con2**
                  Dim lrd As SqlDataReader = cmd.ExecuteReader()
              
                  If lrd.HasRows Then
              
                      Try
                          'Dim lwr As SqlDataReader
              
                          While lrd.Read()
                              ListBox1.Items.Clear()
              
                              str = lrd.GetValue(0).ToString() + ","
                              str = str + lrd.GetValue(1).ToString() + ","
                              str = str + lrd.GetValue(2).ToString() + ","
                              str = str + lrd.GetValue(3).ToString() + ","
                              str = str + lrd.GetValue(4).ToString() + ","
                              str = str + lrd.GetValue(5).ToString()+ ","
                              str = str + lrd.GetValue(6).ToString()
              
                              ListBox1.Items.Insert(cnt\_del, str)
                              cnt\_del = cnt\_del + 1
              
                              ListBox\_Available.Items.Clear()
                              room\_N = SearchAvailableRooms(lrd.GetValue(2), lrd.GetValue(3), lrd.GetValue(4), lrd.GetValue(0), lrd.GetValue(1))
              
                              If room\_N > 0 Then
                                  cmd.CommandText = "UPDATE Reservations SET AvailRoomsRates = 'Trials'"
                                  cmd.ExecuteNonQuery()
                                  cmd2.CommandText = "UPDATE Reservations SET AvailRoomsRates = 'Trials'"
                                  cmd2.ExecuteNonQuery()
                              End If
              
                          End While
              
                          lrd.Close()
              
              
                      Catch ex As Exception
                          cnt\_error = cnt\_error + 1
                          TextBox\_Error.Text = cnt\_error.ToString()
                      End Try
                  End If
              
                  con.Close() 'Whether there is error or not. Close the connection.
              **con2.Close()**
              

              I really don't know if there is an alternative to this. Sorry!

              C 1 Reply Last reply
              0
              • L loyal ginger

                Since nobody else offers any new ideas, I will post the modified version here, just for your information:

                    Dim ra As Integer
                    Dim cmd As New SqlCommand
                
                    Dim cnt\_del As Integer
                    Dim room\_N As Integer
                    Dim str As String
                
                    con.ConnectionString = strOpen        
                    con.Open()
                
                    cmd.Connection = con
                    cmd.CommandText = "SELECT \* FROM Info"
                
                **'con2 should be declared somewhere
                    con2.ConnectionString = strOpen        
                    con2.Open()
                
                'cmd2 should be declared somewhere
                    cmd2.Connection = con2**
                    Dim lrd As SqlDataReader = cmd.ExecuteReader()
                
                    If lrd.HasRows Then
                
                        Try
                            'Dim lwr As SqlDataReader
                
                            While lrd.Read()
                                ListBox1.Items.Clear()
                
                                str = lrd.GetValue(0).ToString() + ","
                                str = str + lrd.GetValue(1).ToString() + ","
                                str = str + lrd.GetValue(2).ToString() + ","
                                str = str + lrd.GetValue(3).ToString() + ","
                                str = str + lrd.GetValue(4).ToString() + ","
                                str = str + lrd.GetValue(5).ToString()+ ","
                                str = str + lrd.GetValue(6).ToString()
                
                                ListBox1.Items.Insert(cnt\_del, str)
                                cnt\_del = cnt\_del + 1
                
                                ListBox\_Available.Items.Clear()
                                room\_N = SearchAvailableRooms(lrd.GetValue(2), lrd.GetValue(3), lrd.GetValue(4), lrd.GetValue(0), lrd.GetValue(1))
                
                                If room\_N > 0 Then
                                    cmd.CommandText = "UPDATE Reservations SET AvailRoomsRates = 'Trials'"
                                    cmd.ExecuteNonQuery()
                                    cmd2.CommandText = "UPDATE Reservations SET AvailRoomsRates = 'Trials'"
                                    cmd2.ExecuteNonQuery()
                                End If
                
                            End While
                
                            lrd.Close()
                
                
                        Catch ex As Exception
                            cnt\_error = cnt\_error + 1
                            TextBox\_Error.Text = cnt\_error.ToString()
                        End Try
                    End If
                
                    con.Close() 'Whether there is error or not. Close the connection.
                **con2.Close()**
                

                I really don't know if there is an alternative to this. Sorry!

                C Offline
                C Offline
                Curious 2009
                wrote on last edited by
                #7

                I will use your update codes There is an alternative, but I don't know which one is better regarding of process speed (considering there are many rows existed) 1. I move the update If room_N > 0 Then outside the while loop, in the loop I will put LOOP's str values into a string array, when Loop done ---> lrd.Close() 2. Then I use a While Loop to Update all rows from the array *) This will avoid Open/Connect twice & Close twice ... but might it be a clumsy programming? Thanks for help :)

                L 1 Reply Last reply
                0
                • C Curious 2009

                  I will use your update codes There is an alternative, but I don't know which one is better regarding of process speed (considering there are many rows existed) 1. I move the update If room_N > 0 Then outside the while loop, in the loop I will put LOOP's str values into a string array, when Loop done ---> lrd.Close() 2. Then I use a While Loop to Update all rows from the array *) This will avoid Open/Connect twice & Close twice ... but might it be a clumsy programming? Thanks for help :)

                  L Offline
                  L Offline
                  loyal ginger
                  wrote on last edited by
                  #8

                  If you use transactions on the second connection, the performance will be improved. I don't know how much improvement you will get. On a different database engine, I once improved the performance of a bunch of updates by over 100 times just by putting them into transactions. Your solution of saving the stuff into an array and updating after the loop is also good. Good luck!

                  C 1 Reply Last reply
                  0
                  • L loyal ginger

                    If you use transactions on the second connection, the performance will be improved. I don't know how much improvement you will get. On a different database engine, I once improved the performance of a bunch of updates by over 100 times just by putting them into transactions. Your solution of saving the stuff into an array and updating after the loop is also good. Good luck!

                    C Offline
                    C Offline
                    Curious 2009
                    wrote on last edited by
                    #9

                    Then I will try both methods, the latter is just for curiously Thanks & ;)

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups