【问题标题】:Vb.net crystal report not displaying the itemsVb.net水晶报表不显示项目
【发布时间】:2017-11-29 07:33:10
【问题描述】:

我正在尝试在库存系统中显示所有购买,但它没有显示在我的水晶报表中 PS:这是我第一次使用水晶报表 这是我的代码:

    Dim show As String = String.Empty
        show &= "select * from purchase_report "
        show &= "where buildnumber=@build"

        Using conn As New SqlConnection("Server=WIN10;database=purchase_stock;user=admin_report;password=54321")
            Using cmd As New SqlCommand
                With cmd
                    .Connection = conn
                    .CommandType = CommandType.Text
                    .CommandText = show
                    .Parameters.AddWithValue("@build", report.Text)
                End With

                Try
                    conn.Open()

                    Dim da As New SqlDataAdapter(cmd)
                    Dim ds As New DataSet
                    da.Fill(ds)

                    If ds.Tables.Count > 0 Then
                        CrystalReportViewer1.ReportSource = ds.Tables.Count
                    End If
                    conn.Close()
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End Using
        End Using


    End Sub
End Class

【问题讨论】:

    标签: sql sql-server vb.net crystal-reports


    【解决方案1】:

    试试这个代码:

    根据您的需要更改它

    Friend Function ViewReport(ByVal sReportName As String, _
        Optional ByVal sSelectionFormula As String = "", _
        Optional ByVal param As String = "") As Boolean
    
        'Declaring variables
        Dim intCounter As Integer
        Dim intCounter1 As Integer
    
        'Crystal Report's report document object
        Dim objReport As New _
            CrystalDecisions.CrystalReports.Engine.ReportDocument 
    
        'object of table Log on info of Crystal report
        Dim ConInfo As New CrystalDecisions.Shared.TableLogOnInfo
    
        'Parameter value object of crystal report 
        ' parameters used for adding the value to the parameter.
        Dim paraValue As New CrystalDecisions.Shared.ParameterDiscreteValue
    
        'Current parameter value object(collection) of crystal report parameters.
        Dim currValue As CrystalDecisions.Shared.ParameterValues
    
        'Subreport object of crystal report.
        Dim mySubReportObject As _
            CrystalDecisions.CrystalReports.Engine.SubreportObject
    
        'Sub report document of crystal report.
        Dim mySubRepDoc As New _
            CrystalDecisions.CrystalReports.Engine.ReportDocument
    
        Dim strParValPair() As String
        Dim strVal() As String
        Dim index As Integer
    
        Try
    
            'Load the report
            objReport.Load(sReportName)
    
            'Check if there are parameters or not in report.
            intCounter = objReport.DataDefinition.ParameterFields.Count
    
            'As parameter fields collection also picks the selection 
            ' formula which is not the parameter
            ' so if total parameter count is 1 then we check whether 
            ' its a parameter or selection formula.
    
            If intCounter = 1 Then
                If InStr(objReport.DataDefinition.ParameterFields(_
                    0).ParameterFieldName, ".", CompareMethod.Text) > 0 Then
                    intCounter = 0
                End If    
            End If
    
            'If there are parameters in report and 
            'user has passed them then split the 
            'parameter string and Apply the values 
            'to their concurrent parameters.
    
            If intCounter > 0 And Trim(param) <> "" Then
                strParValPair = param.Split("&")
    
                For index = 0 To UBound(strParValPair)
                If InStr(strParValPair(index), "=") > 0 Then
                    strVal = strParValPair(index).Split("=")
                    paraValue.Value = strVal(1)
                    currValue = _ 
                        objReport.DataDefinition.ParameterFields(_
                        strVal(0)).CurrentValues
                    currValue.Add(paraValue)
                    objReport.DataDefinition.ParameterFields(_
                        strVal(0)).ApplyCurrentValues(currValue)
                End If
            Next
        End If
    
        'Set the connection information to ConInfo 
        'object so that we can apply the 
        'connection information on each table in the report
        ConInfo.ConnectionInfo.UserID = <User Name>
        ConInfo.ConnectionInfo.Password = <Password>
        ConInfo.ConnectionInfo.ServerName = <Server Name>
        ConInfo.ConnectionInfo.DatabaseName = <Database Name>
    
        For intCounter = 0 To objReport.Database.Tables.Count - 1
            objReport.Database.Tables(intCounter).ApplyLogOnInfo(ConInfo)
        Next
    
        ' Loop through each section of the report then look 
        ' through each object in the section
        ' if the object is a subreport, then apply login info 
        ' on each table of that subreport
    
            For index = 0 To objReport.ReportDefinition.Sections.Count - 1
                For intCounter = 0 To _
                    objReport.ReportDefinition.Sections(_
                    index).ReportObjects.Count - 1
                With objReport.ReportDefinition.Sections(index)
                    If .ReportObjects(intCounter).Kind = _
                    CrystalDecisions.Shared.ReportObjectKind.SubreportObject Then
                        mySubReportObject = CType(.ReportObjects(intCounter), _
                          CrystalDecisions.CrystalReports.Engine.SubreportObject)
                        mySubRepDoc = _ 
                 mySubReportObject.OpenSubreport(mySubReportObject.SubreportName)
                     For intCounter1 = 0 To mySubRepDoc.Database.Tables.Count - 1
                           mySubRepDoc.Database.Tables(_
                               intCounter1).ApplyLogOnInfo(_
                               ConInfo)sp;
                           mySubRepDoc.Database.Tables(_
                               intCounter1).ApplyLogOnInfo(ConInfo)
                     Next
                     End If
                 End With
             Next
         Next
        'If there is a selection formula passed to this function then use that
        If sSelectionFormula.Length > 0 Then
            objReport.RecordSelectionFormula = sSelectionFormula
        End If
        'Re setting control 
        rptViewer.ReportSource = Nothing
    
        'Set the current report object to report.
        rptViewer.ReportSource = objReport
    
        'Show the report
        rptViewer.Show()
            Return True
        Catch ex As System.Exception
            MsgBox(ex.Message)
        End Try
    End Function
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多