Generating a Crystal Report depending on the selected item in a dropdownlist

Last post 09-26-2007 5:39 AM by diana_j86. 4 replies.

Sort Posts:

  • Generating a Crystal Report depending on the selected item in a dropdownlist

    09-24-2007, 5:02 AM
    • Loading...
    • diana_j86
    • Joined on 01-05-2007, 11:10 AM
    • Posts 38

    How can I generate a Crystal Report depending on the selected item in a dropdownlist filled with items from database?

    I am using a vb code behind.

     

    thanks

  • Re: Generating a Crystal Report depending on the selected item in a dropdownlist

    09-24-2007, 7:27 AM
    • Loading...
    • aw007
    • Joined on 08-10-2007, 11:41 AM
    • Posts 37

    Diana_j86

    What version of Visual Studio do you have?

    In VS2005 you need to do the following:

    1) Right click on your solution and select add, New Item.

    2) Add a dataset and create your queries in the dataset. Make sure you save this dataset or it will not show up later.

    3) Add your reportviewer

    4) Right click on your solution and select add new Item Again and then add a crystal report

    5) In the crystal report wizard Select crystal reports wizard and the type of report you want

    6) Under the data menu you need to select project data, then ADO.NET datasets. Assuming you saved your dataset it will now show up in this field and you can make your selection as well as create your crystal report.

    7) The code behind:

    Dim dataSet As DataSet
    Me.Cursor = Cursors.WaitCursor
    Dim crReportDocument As CrystalReport1
    Dim Con As SqlConnection
    Dim Da As SqlDataAdapter
    Dim conn As String = "Server=Your Server;Database=Your DB; User ID=sa;Password=Your PW"
    Con = New SqlConnection(conn)
    Dim sqlString As String = "This is where you can place your SQL query and pass all parameters   i.e.  ComboBox1.SelectedValue"
    Dim cmSQL As SqlClient.SqlCommand
    cmSQL = New SqlClient.SqlCommand(sqlString, Con)
    Con.Open
    Da = New SqlDataAdapter(sqlString, Con)
    dataSet = New DataSet
    Da.Fill(dataSet, "Dataset Name you created in steps above")
    crReportDocument = New CrystalReport1
    crReportDocument.SetDataSource(dataSet)
    CView.ReportSource = crReportDocument
    'crReportDocument.Load("~/CrystalReport1.rpt")
    Con.Close


    Please note that CView is the name of my report viewer.

    Hope this helps

  • Re: Generating a Crystal Report depending on the selected item in a dropdownlist

    09-25-2007, 6:10 AM
    • Loading...
    • diana_j86
    • Joined on 01-05-2007, 11:10 AM
    • Posts 38

    Thanks for your response, I really appriciate that, But I have this error " Line 1: Incorrect syntax near 'Sam' ", it happens when I click on any item from the drop down list....

    my code is:

    Dim connString As String = "server=(local);database=Inventory Controller;trusted_connection=yes"

    Dim conn As New SqlConnection(connString)

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    If Not IsPostBack Then

    fillDropDownList1()

    End If

    End Sub

    Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged

    Dim dataSet As DataSet

    Dim crReportDocument As CrystalReport1

    Dim Da As SqlDataAdapter

    Dim sqlString As String = "select * from Receiptitems where Name=" & Me.DropDownList1.SelectedItem.TextDim cmSQL As SqlCommandcmSQL = New SqlCommand(sqlString, conn)

    conn.Open()

    Da = New SqlDataAdapter(sqlString, conn)

    dataSet = New DataSet

    Da.Fill(dataSet, "Dataset3.xsd")

    crReportDocument =
    New CrystalReport1

    crReportDocument.SetDataSource(dataSet)

    CrystalReportViewer1.ReportSource = crReportDocument

    conn.Close()

    End SubPrivate Sub fillDropDownList1()

    Dim sqlString As String = "select * from Receiptitems"

    Dim cmd As New SqlCommand(sqlString, conn)

    cmd.CommandType = CommandType.Text

    Dim dr As SqlDataReader

    conn.Open()

    dr = cmd.ExecuteReader

    While dr.Read

    Me.DropDownList1.Items.Add(dr(0))

    End While

    dr.Close()

    conn.Close()

    End Sub

     

    Also, ( Me.Cursor = Cursors.WaitCursor ) didn't work :(

     What is the problem, I followed all the steps, but don't know why I have this error

    Could it be because in the stored procedure the attribute (Name) I have is reserved?

  • Re: Generating a Crystal Report depending on the selected item in a dropdownlist

    09-25-2007, 10:29 AM
    Answer
    • Loading...
    • aw007
    • Joined on 08-10-2007, 11:41 AM
    • Posts 37

    O.K,

    Let start: Did you copy this code Directly? If so I see my first issue with the following line:

    Dim sqlString As String = "select * from Receiptitems where Name=" & Me.DropDownList1.SelectedItem.TextDim cmSQL As

    It needs to be:

    Dim sqlString As String = "select * from Receiptitems where Name='" & Me.DropDownList1.SelectedItem.Text & "'

    Maybe you did not copy it right.

    The second part is:

    Da.Fill(dataSet, "Dataset3.xsd"), is Dataset3.xsd your Dataset name? or the file name? Make sure that that is your dataset name. My file name for example is endofmonth.xsd, but my dataset name is "closingbalance".

    Look at that and let me know.

     

  • Re: Generating a Crystal Report depending on the selected item in a dropdownlist

    09-26-2007, 5:39 AM
    • Loading...
    • diana_j86
    • Joined on 01-05-2007, 11:10 AM
    • Posts 38

    Thanks alot, it worked Big Smile 

     
    										                
    										                
                									    
Page 1 of 1 (5 items)
Microsoft Communities
Page view counter