2017-05-22 6 views
0

Ich muss für jeden Kunden eine PDF-Datei erstellen, die als Ergebnis einer Auswahl aus SQL-Abfrage kommt.Erstellen Sie ein PDF-Dokument für jeden Kunden in VB.net

Zum Beispiel für "Kunde 1" sollten alle Informationen dieses Kunden zu einer Datei namens "Customer1.pdf" gehen. Dann "Kunde 2" mit allen Informationen in "Customer2.pdf" usw. bis zum letzten Kunden aus der SQL-Abfrage.

Der Code funktioniert, das Problem ist die Schleife, um jedes Dokument für jeden Kunden zu erstellen: es erstellt nur ein Dokument mit allen Informationen.

Beispiel meines VB.net Code:

Dim customer_SQL As String 

Using connObj As New SqlConnection(sql conection) 
    Using cmdObj As New SqlClient.SqlCommand("Select Distinct(customer_ID) from my table Where custmerid is not null", connObj) 
     connObj.Open() 
     Using readerObj As SqlClient.SqlDataReader = cmdObj.ExecuteReader 
      ' This will loop through all returned records ' 
      While readerObj.Read 
       Customer_SQL = readerObj("Customer_ID").ToString 
       ' To see if it return the value I want 
       'MessageBox.Show(Customer_SQL.ToString) 

       Try 
        Dim CrExp As ExportOptions 
        Dim CrDiskFileDest As New DiskFileDestinationOptions() 
        Dim crFormatTypeopt As New PdfRtfWordFormatOptions() 
        For Li_count As Integer = 0 To Customer_SQL 
         CrDiskFileDest.DiskFileName = "C:\Report" & Customer_SQL.ToString & ".pdf" 
         CrExp = cryRpt.ExportOptions 
         With CrExp 
          .ExportDestinationType = ExportDestinationType.DiskFile 
          .ExportFormatType = ExportFormatType.PortableDocFormat 
          .DestinationOptions = CrDiskFileDest 
          .FormatOptions = crFormatTypeopt 

         End With 
         cryRpt.Export() 
        Next 
       Catch ex As Exception 
        MsgBox(ex.ToString) 
       End Try 
      End While 
     End Using 
     connObj.Close() 
    End Using 
End Using 
+0

Danke Tim ... um es zu korrigieren – Luis64

+1

Wenn Sie die angezeigten Probleme mit [Option Strict On] korrigieren (https://docs.microsoft.com/en-us/dotnet/articles/visual-basic/language-reference/ Statements/Option-Strict-Statement) haben Sie vielleicht ein Arbeitsprogramm. Außerdem ist "custmerid" in der SQL-Abfrage die richtige Schreibweise? –

+0

Normalerweise würde ich den Inhalt des Codes nicht ändern, wenn ich sein Format bearbeite, aber die Zeile 'CmdObj As New SqlClient.SqlCommand (" Select Distinct (customer_ID) aus meiner Tabelle where candidmer ist nicht null, connObj) "fehlte ein essentielles Closing Ich nehme an, das war ein Übertragungsfehler –

Antwort

0
Using connObj As New SqlConnection(sql conection) 
    Using cmdObj As New SqlClient.SqlCommand("Select Distinct(customer_ID) from my table Where custmerid is not null, connObj) 
      connObj.Open() 
      Using readerObj As SqlClient.SqlDataReader = cmdObj.ExecuteReader 
       'This will loop through all returned records 
       While readerObj.Read 
        Customer_SQL = readerObj("Customer_ID").ToString 
        ' To see if it return the value I want 
        'MessageBox.Show(Customer_SQL.ToString) 

        Try 
         Dim CrExp As ExportOptions 
         Dim CrDiskFileDest As New DiskFileDestinationOptions() 
         Dim crFormatTypeopt As New PdfRtfWordFormatOptions() 
         For Li_count As Integer = 0 To Customer_SQL 

          CrDiskFileDest.DiskFileName = "C:\Report" & Li_count & ".pdf" 

          CrExp = cryRpt.ExportOptions 
          With CrExp 
           .ExportDestinationType = ExportDestinationType.DiskFile 
           .ExportFormatType = ExportFormatType.PortableDocFormat 
           .DestinationOptions = CrDiskFileDest 
           .FormatOptions = crFormatTypeopt 

          End With 
          cryRpt.Export() 
         Next 
        Catch ex As Exception 
         MsgBox(ex.ToString) 
        End Try 
       End While 
      End Using 
      connObj.Close() 
     End Using 
    End Using 
End Sub 

Ich denke, der Fehler auf 16. Zeile ist

+0

Hallo Ricardo, ich ändere es und erstelle das gleiche Dokument immer wieder. Ich möchte für jeden Kunden ein anderes Dokument haben – Luis64

+0

Haben Sie meine Lösung versucht? 'CrDiskFileDest.DiskFileName =" C: \ Bericht "& Li_count &" .pdf "' –

+0

Ja Ricardo, hat nicht funktioniert – Luis64

0

ich das Problem mit dieser Lösung, die alle Vielen Dank für Hilfe mich von: Auch Wenn jemand eine PDF-Datei oder eine Datei aus Visual Studio mithilfe von Crystal Report platzen lassen muss. Dies ist die Firmula

Dim Customer_id As String 
     Using connObj As New SqlConnection("SQL conection") 
      Using cmdObj As New SqlClient.SqlCommand("Select Distinct(Customer_ID) from Customer_DIM", connObj) 
       connObj.Open() 
       Using readerObj As SqlClient.SqlDataReader = cmdObj.ExecuteReader 
        'This will loop through all returned records 
        While readerObj.Read 
         Customer_id = readerObj("Customer_ID").ToString 
         'Parameters 
         cryRpt.SetParameterValue("Prompts_Customer_id", Customer_id) 

         Dim List As New List(Of String) From {Customer_id} 

         'handle returned value before next loop here 

         Try 
          Dim CrExp As ExportOptions 
          Dim CrDiskFileDest As New DiskFileDestinationOptions() 
          Dim crFormatTypeopt As New PdfRtfWordFormatOptions() 
          For Each Customer_id In List 
           CrDiskFileDest.DiskFileName = "C:\Report\R" & Customer_id & ".pdf" 
           CrExp = cryRpt.ExportOptions 
           With CrExp 
            .ExportDestinationType = ExportDestinationType.DiskFile 
            .ExportFormatType = ExportFormatType.PortableDocFormat 
            .DestinationOptions = CrDiskFileDest 
            .FormatOptions = crFormatTypeopt 

           End With 
           cryRpt.Export() 
          Next Customer_id 
         Catch ex As Exception 
          MsgBox(ex.ToString) 
         End Try 
        End While 
       End Using 
       connObj.Close() 
      End Using 
     End Using 
    End Sub 
Verwandte Themen