locked
Bind Datatable to report viewer RRS feed

  • Question

  • User-313791273 posted

    Hi all, i had using the following code (VB) to build a datatable to bind the gridview:

    Using conn2 As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("XXXX").ConnectionString)
    conn2.Open()
    cmd.Connection = conn2
    
    sql = "select XXX"
    cmd.CommandText = sql
    reader = cmd.ExecuteReader()
    
    Dim dt As New DataTable()
    
    dt.Columns.Add(New DataColumn("Col1_StaffKey", GetType(Int32)))
    dt.Columns.Add(New DataColumn("Col1_PostKey", GetType(Int32)))
    
    Dim dr As DataRow
    
    While reader.Read()    '---For each row
            dr = dt.NewRow()
            dr("Col1_StaffKey") = reader("staff_key")
            dr("Col1_PostKey") = reader("post_key")
    	dt.Rows.Add(dr)
    End While

    The above code works fine and can bind the gridview, now i want to use the above datatable in report viewer (crystal report)

    but when i design the report, it ask me to provide the datasource.

    How can I pass and use the existing datatable in report viewer as datasource ?

    Thanks
    Joe

    Wednesday, June 15, 2011 3:54 AM

Answers

  • User-269404413 posted

    joe,

    at design time we just design the report that is no need to give any other credientials by code.

    just open server explorer and coneect your database then drag and drop it that it enough

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 15, 2011 7:24 AM

All replies

  • User-269404413 posted

    Joe,

    before run the report we should design the report, that means just connect with your database then select your table then just drag and drop your columns to the report so, at run time you have to bind the same columns with data using datatable.

    its very simple only. have a look at this

    ref0

    ref1

    ref2

     

    Wednesday, June 15, 2011 4:03 AM
  • User-313791273 posted

    Thanks for your reply.

    That means I build up a dataset -> datatable with same column name and then drag and drop to the report first ?

    but how can i connect and pass data to this datasource in code ?

    Joe

    Wednesday, June 15, 2011 4:53 AM
  • User-269404413 posted

    joe,

    at design time we just design the report that is no need to give any other credientials by code.

    just open server explorer and coneect your database then drag and drop it that it enough

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 15, 2011 7:24 AM
  • User3866881 posted

    Hello:)

    For reportViewer, I think you should use SqlDataAdapter, with which you can fetch data contents into the DataTable and bind to the ReportViewer.

    Here's the sample——You can download and have a run to play with fun....

    http://www.codeproject.com/KB/webforms/ReportViewer.aspx

    Thursday, June 16, 2011 10:40 PM