By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,165 Members | 1,908 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,165 IT Pros & Developers. It's quick & easy.

Microsoft Report Viewer with SqlConnection

P: n/a

anyone has a code sample to connect the Microsoft Report Viewer Control
using Microsoft SQL Server stored procedure?

Jul 22 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi Jassim,

This can be a big question, and your question is a bit vague. I'm assuming
that you have a rdl file already and that has a datasource defined so the
report can process the results. Actually, the fact that it's a stored proc
is irrelevant since the report viewer doesn't query for data, you have to
get the data yourself and then assign it to the report.

First off you need to know the datasource name required. You can find these
out easily enough by.iterating over the DataSources collection.

Now assuming you have an aspx file with a ReportViewer web control on it
called ReportViewer1, and an RDL(C) Report File already loaded in it, this
code will get you the datasource names contained.

// code block 1
List<stringdataSourceNames = new List<string>();
foreach(ReportDataSource rds in ReportViewer1.LocalReport.DataSources)
{
dataSourceNames.Add(rds.Name);
}

now you can assign to the data sources by index or name. so if your report
has a single datasource called DataSet1_DataTable1 which has the columns
defined, you can simply do

DataTable data = YourMethodToLoadDataFromYourStoredProc();
// line 2
ReportViewer1.LocalReport.DataSources["DataSet1_DataTable1"].Value = data;
ReportViewer1.Refresh();

or you can avoid looking through the defined datasources first and simply
assign to ReportViewer1.LocalReport.DataSources[0] - although this will be
error prone.

now if you don't know the stored proc or query to call in advance, but it's
specified in the report, things are trickier because the ReportViewer API
doesn't appear to expose this information. You will need to parse the XML
from the RDL file and find the DataSets/DataSet/Query node for each DataSet
in the report. Among other things, this node has a child node CommandText,
the InnerText of which will be the query - either a SQL command or the
stored proc call.

If the query expects parameters it's more difficualt again. To obtain this
information you will need to parse the Query/QueryParameters node from the
XML and resolve the parameters yourself. Note that the report viewer doesn't
generate any UI for user input of report parameters so if you want this to
be automatic, you'll need to write some code to generate the UI dynamically
yourself.

if you need some help with parsing the XML let me know. it's easy enough,
but it's a little beyond beginner level if you've not done it before.

Leon

"Jassim Rahma" <jr****@hotmail.comwrote in message
news:92**********************************@microsof t.com...
>
anyone has a code sample to connect the Microsoft Report Viewer Control
using Microsoft SQL Server stored procedure?
Jul 23 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.