ListBox on Inserting values into the Database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Esll
    New Member
    • Apr 2013
    • 5

    ListBox on Inserting values into the Database

    Hi, I have been using a listbox to insert records into the database.
    But I get this error when I try to add more than one records into the database :
    'There is already an open DataReader associated with this Command which must be closed first.'
    Can someone help me on this?

    Code:
    database.OpenConn()
    For j As Integer = 0 To ListBox1.Items.Count - 1
    Dim strSQL = "Select * FROM table1 where Name = '" & ListBox1.Items(j).ToString() & "'"
    Dim dr as SqlDataReader = db.ExecuteQuery(strSQL)
    While dr.Read()
    Dim ID = dr("ID").ToString()
    End While
    Next
    dr.Close()
    database.CloseConn()
    
    database.OpenConn()
    Dim strSQL2 = "INSERT INTO table2 Values (" & ID & ")"
    database.ExecuteUpdate(strSQL2)
    dr.Close()
    database.CloseConn()
  • vijay6
    New Member
    • Mar 2010
    • 158

    #2
    Hey Esll, open the connection to your database before line number 4 and inside the 'for' loop. And close the database connection after line number 7. Then try.

    Comment

    • Esll
      New Member
      • Apr 2013
      • 5

      #3
      Hi vijay6,
      I have solve the problem.
      Thanks for your help anyway.

      Comment

      Working...