Link to home
Create AccountLog in
Avatar of xuanthinh
xuanthinh

asked on

Passing parameter to Crytal report in ASP .NET C#

Hi all,

I intend to build report by Crystal Report in ASP .NET C#. But I don't know how to passing parameter to report. For example: I want to select condition in the web form, and then depend that condition was selected I will make sql command and passing to the report.

And good more if I can make dynamic report (design report at runtime).

Please help me

Thank you very much
Avatar of Spykair
Spykair
Flag of South Africa image

ASKER CERTIFIED SOLUTION
Avatar of dylanyee
dylanyee

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Mike McCracken
Mike McCracken

Avatar of xuanthinh

ASKER

Hi all,

Thank for your help. Now I can use the below code to passing parameter to Crystal report.
But my sql is complex command that select data from more than one tables and the adoOleDbDataAdapter.Fill require srcTable. I don't want to create a table have structure like a sql command (that table have not any data and do not use in any job and each sql command I have a table like that). Is there any way to solve this problem? Please help me!

                  string connectionString= "";
                  connectionString = "Provider=SQLOLEDB;";
            connectionString += "Server=TH_NGANV;Database=pubs;";
                  connectionString += "User ID=sa;Password=";

                  //Create and open a connection using the connection string
                  adoOleDbConnection = new OleDbConnection(connectionString);

                  //Build a SQL statement to query the datasource
                  string sqlString = "";
            sqlString = "Select *  From authors where state = 'CA' and phone like '41%'";

                  //Retrieve the data using the SQL statement and existing connection
                  adoOleDbDataAdapter = new OleDbDataAdapter(sqlString, adoOleDbConnection);

                  //Create a instance of a Dataset
                  dataSet = new DataSet();

                  //Fill the dataset with the data retrieved.  The name of the table
                  //in the dataset must be the same as the table name in the report.
                  adoOleDbDataAdapter.Fill(dataSet, "authors");
                  
            
                  //Create an instance of the strongly-typed report object
                  crReportDocument = new CrystalReport1();

                  //Pass the populated dataset to the report
                  crReportDocument.SetDataSource(dataSet);

                  //Set the viewer to the report object to be previewed.
                  CrystalReportViewer1.ReportSource = crReportDocument;
xuanthinh, to design the CR, you don't have to create a physical table that have the same structure/field that returned by your sql query. instead, you could design your report by the sql query.
In your Crystal Report's database expert, after connected to your database, you should be able to see a "Add Command" object, double click that and you could put your sql query in, it will then create a "table" in your "field explorer->databases fields".

dylan
hi dylanyee,

the problem is I don't Add command at design time. I have to create complex sql that select from more than one table at runtime. If you know please give me an example. I am new to CR in .NET C#

Thank you very much!
How did you design your report now? Create a table that has exact fields return from your complex sql then link that to your report?

dylan
hi dylanyee,

I have already make my report now. Thank you very much
sorry dylanyee,

I press wrong button accept answer. Thank you very much for your help anyway.

Thank mlmcc also
thats all right, glad we we could help you up :)

dylan