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
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

>> 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
Are you saying that you want to build the sql query based on user's input then bind the result data to report? You could find some example from here:

>> And good more if I can make dynamic report (design report at runtime).
You could use RDC (Report Design COmponent) to do so. but it won't be easy. Some example here:


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

xuanthinhAuthor Commented:
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

                  //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".

xuanthinhAuthor Commented:
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?

xuanthinhAuthor Commented:
hi dylanyee,

I have already make my report now. Thank you very much
xuanthinhAuthor Commented:
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 :)

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Crystal Reports

From novice to tech pro — start learning today.