1
votes

Scenario:

I have a Gridview with some records which read from SQL Database, each record of Gridview has a button that use CommandName and CommandArgument with sending '<%#Eval("UserId")%>' to detecting each record with calling them in code behind (CS).

Goal:

I want to use entity command to when i click on the button (DO_PRINT(LinkButton)), the crystal report pop up showing content of just same record.

Gridview like this:

UserId    Name   LastName   OfficeId    Print
100       Hassan Hosseini      1        DO_PRINT(LinkButton)
200       Brad   Pitt          2        DO_PRINT(LinkButton)

Thank you in advance

1
Just set the href of the link to a page hosting a crystal report viewer showing a report and in page load of the page, setup data source using the entity Id that you receive from query string or route.Reza Aghaei
Thanks, my main problem is how to showing print of the record that selected link button. In fact, how to add query for my report wich calling it with entity, it just showing content of the selected record. can you answer with an example?Hassan Hosseini
You get the whole list using something like this: db.Users.ToList() you get a single user using something like this: db.Users.Where(x=>x.UserId == id) where the id is something that you receive from query string or route values when someone clicks on the link.Reza Aghaei
So as your comment, i should: Step1: create report template in crystal report by linking with sql database, step2: add a gridveiw wich has records with link button for each one, step3: using command name to calling argumant of each records by switch case command and add href for showing report that created in step 1, step4 is my question: where should i add entity query for calling the records, in the switch case command or crystal report code behind, thanks for explaining.Hassan Hosseini

1 Answers

1
votes

You just need to set the href of the link to a page hosting a crystal report viewer showing a report and in page load of the page, setup data source using the entity Id that you receive from query string or route.

Crystal Reports with Entity Framework - Pass data to report

  1. Create a ASP.NET WebForms Project
  2. Add a new Database to App_Data folder, name it SampleDatabase
  3. Add a new table to the sample database, name it Products:

    CREATE TABLE [dbo].[Prodcts]
    (
        [Id] INT NOT NULL PRIMARY KEY IDENTITY(1,1),
        [Name] NVARCHAR(50) NOT NULL, 
        [Price] INT NOT NULL,
        [Description] NVARCHAR(500) NULL
    )
    
  4. Add a few records to table. (Otherwise the GridView will not be shown).

  5. Add a new entity data model to your project and name it SampleDatabase
  6. Create a new page names Products.aspx and add a new GridView to the page using the following configs:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField  DataField="Name" HeaderText="Name"/>
            <asp:BoundField  DataField="Price" HeaderText="Price"/>
            <asp:BoundField  DataField="Description" HeaderText="Description"/>
            <asp:HyperLinkField
                DataNavigateUrlFields="Id"
                DataNavigateUrlFormatString="~\Report.aspx?Id={0}"
                HeaderText="Report"
                Text="Report" />
        </Columns>
    </asp:GridView>
    
  7. Load data into the grid, in the code behind:

    protected void Page_Load(object sender, EventArgs e)
    {
        using (var db = new SampleDatabaseEntities())
        {
            var data = db.Prodcts.ToList();
            GridView1.DataSource = data;
            GridView1.DataBind();
        }
    }
    
  8. Add a new Crystal Report to the project, name it ProductReport:

    • Add New Item → Crystal Reports → Using the Report Wizard / Standard
    • Then from Available Data Sources: → Project Data → .NET Objects → Find and choose the Product and add it to the right panel
    • Then follow the wizard
  9. Add a new page named Report.aspx and drop an instance of the ReportViwer to the page, using the default name CrystalReportViewer1.

  10. In the code behind of the Report.aspx, get the report and the data to show in report:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (int.TryParse(Request.QueryString["id"], out int id))
        {
            using (var db = new SampleDatabaseEntities())
            {
                var report = new ProductReport();
                var data = db.Prodcts.Where(x => x.Id == id).ToList();
                report.SetDataSource(data);
                CrystalReportViewer1.ReportSource = report;
                CrystalReportViewer1.RefreshReport();
            }
        }
    }
    

Note: In case you had some script error preventing the report viewer from showing, you can copy C:\inetpub\wwwroot\aspnet_client folder to your project.