By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,219 Members | 2,361 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,219 IT Pros & Developers. It's quick & easy.

ListBox on Inserting values into the Database

P: 5
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?

Expand|Select|Wrap|Line Numbers
  1. database.OpenConn()
  2. For j As Integer = 0 To ListBox1.Items.Count - 1
  3. Dim strSQL = "Select * FROM table1 where Name = '" & ListBox1.Items(j).ToString() & "'"
  4. Dim dr as SqlDataReader = db.ExecuteQuery(strSQL)
  5. While dr.Read()
  6. Dim ID = dr("ID").ToString()
  7. End While
  8. Next
  9. dr.Close()
  10. database.CloseConn()
  11.  
  12. database.OpenConn()
  13. Dim strSQL2 = "INSERT INTO table2 Values (" & ID & ")"
  14. database.ExecuteUpdate(strSQL2)
  15. dr.Close()
  16. database.CloseConn()
  17.  
Apr 30 '13 #1
Share this Question
Share on Google+
2 Replies


100+
P: 158
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.
Apr 30 '13 #2

P: 5
Hi vijay6,
I have solve the problem.
Thanks for your help anyway.
May 17 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.