Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

tdrclan

Programmer
Sep 18, 2007
52
US
Im getting the following error
There is already an open DataReader associated with this Command which must be closed first.
with this code at the second ExecuteReader()
(is it the way I coded it or is it the sql 2000 engine?)

Code:
   Sub PrintBillCarriers(ByVal docType As String)

        Dim jeff As New Word.Application
        Dim d As Word.Document
        Dim PrinterName As String
        Dim strconn As String

        Dim sqlConn As New SqlConnection("Data Source=triton;Integrated Security=SSPI;Initial Catalog=CHPSPFORCHPINS;")
        Dim sqlCmdform As SqlCommand
        Dim sqlCmdDoc As SqlCommand
        Dim sqlReaddoc As SqlDataReader
        Dim sqlReadform As SqlDataReader

        sqlConn.Open()

        ' get document to merge
        sqlCmdform = New SqlCommand("SELECT FormPrintQueueDocID, FormPrintQueueBundleType, FormPrintQueueDescription FROM TDocgenFormPrintQueue where FormPrintQueueBundleType = '" & docType & "' order BY FormPrintQueueDocID, FormPrintQueueBundleType", sqlConn)
        sqlReadform = sqlCmdform.ExecuteReader()

        ' get word document and path 
        sqlCmdDoc = New SqlCommand("select docpath, docname from TDocgenBundles where BundleType = '" & docType & "' and carrier = '201' order BY BundleOrder", sqlConn)
        sqlReaddoc = sqlCmdDoc.ExecuteReader()

        While sqlReadform.Read()

            'd = jeff.Documents.Open("\\triton\igen\Documents\Consolidated Health Plans\Sports App - Rewrite.doc")
            d = jeff.Documents.Open(sqlReaddoc("docpath").ToString & sqlReaddoc("docname").ToString)

            jeff.ActiveDocument.Merge(FileName:="\\triton\igen\temp\Agent Name Coversheet.doc")

            ' strconn = "Data Source=OMEGA\OMEGA;Integrated Security=SSPI;Initial Catalog=CHP_DW_SOURCE"
            strconn = "DSN=CHPSPFORCHPINS;uid=HEALTHPN\administrator;pwd=;"

            PrinterName = jeff.ActivePrinter

            While sqlReaddoc.Read()

                d.MailMerge.OpenDataSource(Name:="", Connection:=strconn, SQLStatement:="SELECT PolicyHeaderPolicyRef as PolicyNumber, " & _
                "PolicyHeadercarrierName as AgentName, " & _
                "PolicyHeadercarrierAddr as AgentAddress, " & _
                "PolicyHeadercarrierAddr2 as AgentAddress2, " & _
                "PolicyHeadercarrierCity as AgentCity, " & _
                "PolicyHeadercarrierState as AgentState, " & _
                "PolicyHeadercarrierZip as AgentZip, " & _
                "policyGPBillCodeBillCode as BillClassCode1, " & _
                "policyGPBillDesc as EligibleBillCode, " & _
                "policyGPBillUnitRate as UnitRate1, " & _
                "policyBillingEffDate as BillingEffDate, " & _
                "policyBillingExpDate as BillingExpriationDate, " & _
                "policyBillingInvNum as InvoiceNumber, " & _
                "CompanyName as MailNameTypeB, " & _
                "CompanyAddr1 as AddressTypeB, " & _
                "CompanyAddr2 as Address2TypeB, " & _
                "CompanyCity as CityTypeB, " & _
                "CompanyState as StateIDTypeB, " & _
                "CompanyZip as ZipTypeB, " & _
                "FormPrintQueueBundleType as BillType " & _
                "FROM (TDocgenPolicyHeader, TDocgenPolicyGp, TDocgenPolicyBilling, TDocgenCompany, TDocgenFormPrintQueue) " & _
                "where TDocgenPolicyHeader.PolicyHeaderDocID = TDocgenPolicyGp.PolicyGpDocID and " & _
                "TDocgenPolicyHeader.PolicyHeaderDocID = TDocgenPolicyBilling.PolicyBillingDocID and " & _
                "TDocgenPolicyHeader.PolicyHeaderDocID = TDocgenCompany.CompanyDocID " & _
                "TDocgenPolicyHeader.PolicyHeaderDocID = TDocgenCompany.FormPrintQueueDocID " & _
                "where PolicyHeaderDocID = '" & sqlReadform("FormPrintQueueDocID") & "' ")

                'd.MailMerge.OpenDataSource(Name:="", Connection:=strconn, SQLStatement:="SELECT * FROM iDocPrintRewriteBills WHERE DocId = '" & sqlRead("DocId").ToString & "'")
                d.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument
                d.MailMerge.Execute()

                Rcount = Rcount + 1
                Console.WriteLine("Generating " & Rcount & " of " & rewritecount & " Rewrite PDF files..." & sqlReadform("FormPrintQueueDocID") & " " & FormatPercent(Rcount / rewritecount) & " total completed")
                jeff.ActiveDocument.ExportAsFixedFormat(OutputFileName:=My.Settings.pdfPath & sqlReadform("FormPrintQueueDocID"), ExportFormat:=Word.WdExportFormat.wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:=Word.WdExportOptimizeFor.wdExportOptimizeForPrint, BitmapMissingFonts:=True, UseISO19005_1:=True)
                jeff.ActiveDocument.Close(False)
            End While
            'reset sqlReaddoc to first record
            sqlReaddoc.Close()

        End While
        sqlReadform.Close()

        d.Close(False)
        jeff.Quit(False)
        sqlConn.Close()

    End Sub


I need to get all of the forms that I need to print and the list of word documents I need to print them on.

TIA

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top