Solved

VB.net Loop problem with executereader

Posted on 2009-04-08
2
547 Views
Last Modified: 2013-11-27
I import a csv file into a dataset and then I want to take this information from this dataset and import it to my database. It runs over the first row but then it states:

"Error: System.Data: There is already an open DataReader associated with this Command which must be closed first"

I added cmdstudent.executereader.close() in the loop but it still does not work.

Any suggestions?
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
 
        Dim conn As SqlConnection
        Dim cmdStudent As New SqlCommand
        Dim cmdStudent1 As New SqlCommand
        Dim daStudent As New SqlDataAdapter
        Dim dsStudent As New DataSet
        Dim dtStudent As New DataTable
 
        Dim ds1 As New DataSet
        Dim sCon As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\;Extended Properties=Text;"
        Dim objConn As New OleDbConnection(sCon)
 
        objConn.Open()
 
        Dim objCmdSelect As New OleDbCommand("SELECT * FROM daily.csv", objConn)
 
        Dim objAdapter1 As New OleDbDataAdapter
 
        objAdapter1.SelectCommand = objCmdSelect
 
        objAdapter1.Fill(ds1, "daily")
 
        DGV1.DataSource = ds1.Tables(0).DefaultView()
 
        objConn.Close()
 
        Dim i As Integer
 
        Try
            conn = GetConnect()
            conn.Open()
 
 
            For i = 0 To (ds1.Tables(0).Rows.Count - 1)
                cmdStudent = conn.CreateCommand
                cmdStudent.CommandText = "INSERT INTO dailyrounds (date,name,price,no,url,high,highnum,mid,midnum,low,lownum)" & _
                   "VALUES ('" & Date.Today & "','" & _
                   ds1.Tables(0).Rows(i)("name") & "'," & _
                   ds1.Tables(0).Rows(i)("price") & "," & _
                   ds1.Tables(0).Rows(i)("no") & ",'" & _
                   ds1.Tables(0).Rows(i)("url") & "','" & _
                   ds1.Tables(0).Rows(i)("high") & "'," & _
                   ds1.Tables(0).Rows(i)("highnum") & ",'" & _
                   ds1.Tables(0).Rows(i)("mid") & "'," & _
                   ds1.Tables(0).Rows(i)("midnum") & ",'" & _
                   ds1.Tables(0).Rows(i)("low") & "'," & _
                   ds1.Tables(0).Rows(i)("lownum") & ") "
 
                cmdStudent.ExecuteReader.Read()
                cmdStudent.ExecuteReader.Close()
            Next i
 
            Refresh_Form()
            conn.Close()
 
        Catch ex As Exception
            MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly, "Connection Error !!")
        End Try
 
    End Sub

Open in new window

0
Comment
Question by:ralphcarter2008
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Accepted Solution

by:
sunithnair earned 500 total points
ID: 24099265
I think your line number 50 should be cmdStudent.ExecuteNonQuery() since you are trying to insert the values into a database table. ExecuteReader is just for reading the data and ExecuteNonQuery is used for DML queries.
0
 
LVL 1

Author Closing Comment

by:ralphcarter2008
ID: 31568123
Good catch!!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question