Showing posts with label control. Show all posts
Showing posts with label control. Show all posts

Wednesday, March 7, 2012

Problem viewing report with Report Viewer

Hi,

I am using the Report Viewer Control to display the Reports. My query is generated dynamically so that output the query is a datatable. My application gives the user the flexibility to select the tables & the table-fields on which he wants to run the report. So I cannot have a fixed report file as I do not know what will the user select.

I tried adding the result datatable as the datasource for the report dynamically.

protected void RunReport_Click(object sender, EventArgs e){string constg ="server=localhost;Database=techapp;user id=sa;password=admin;";SqlConnection con =new SqlConnection(constg);//query is the query string that is generated at runtimeSqlDataAdapter da =new SqlDataAdapter(query,con);DataSet ds =new DataSet();da.Fill(ds,"ReportDataTable"); con.Close();ViewReport(ds.Tables["ReportDataTable"]);}protected void ViewReport(DataTable dt){ ReportViewer1.ProcessingMode = ProcessingMode.Local; ReportDataSource rd =new ReportDataSource(); rd.Name ="ReportData_Data"; rd.Value = dt; ReportViewer1.LocalReport.DataSources.Add(rd); ReportViewer1.DataBind();}

My problem is that the entire code gets executed properly without any exception but the report is not displayed. I checked the ReportDataSource & it shows the proper value. In all the samples that I went through, I found that they have mentioned a report file. I don't know if this is the cause of the problem, but if it is then In mycase how can I create a report design file if I wouldn't know what to add in that file at design time.

I do not have an expertise in this. can anyone please guide me through the right way & let me know what is it that I'm not doing correct?

I guess its the problem with height of report

Check whether you have set the height of report as 100%. Change that to some fixed size let say 800px.

Let me know if you need any further help

|||

Hi Ashok,

I tried adjusting the height of the report viewer . This is the changed code but it still doesn't show any thing.

 ReportViewer1.ProcessingMode = ProcessingMode.Local; ReportViewer1.Height = Unit.Pixel(500); ReportDataSource rd =new ReportDataSource(); rd.Name ="ReportData_Data"; rd.Value = ds.Tables["ReportDataTable"]; ReportViewer1.LocalReport.DataSources.Add(rd); ReportViewer1.DataBind();

Could the problem be the absence of a report design file ie an rdlc file. I downloaded a sample code from net where the report file is created dynamically & it seemed to work this way.

Is a report design file by default required for the report viewer control to display the report? What is the complexity involved in creating such design files on your own?? Can you shed some light on these points ?

Thanks

Saturday, February 25, 2012

Problem using parameter on SQL source data control

I have an ASP page which uses an SQL data source control, as shown below:

<asp:SqlDataSource ID="mySQL" runat="server"
ConnectionString="<%$ ConnectionStrings:mySQLConnectionString %>"
SelectCommand="SELECT device FROM device_table WHERE (device_id IN (@.devicelist))">
<SelectParameters>
<asp:Parameter DefaultValue="" Name="devicelist" Type="String" />
</SelectParameters>
</asp:SqlDataSource
The problem is getting the "IN" clause of the select statement to work via the ASP page with code behind in C#. If I test the query in the page designer when configuring the SQL control by inserting 'device-1','device-2','device-3' as the value for the "devicelist" parameter, everything works as expected. However, when that same value is written programmatically into the DefaultValue of the parameter, the resulting query contains no results (as if the string contents are invalid). If I alter my program to supply a single entry (with no commas) it seems to work as well. I suspect the comma (and possibly the single-quote) characters in my string when multiple entries are involved are causing the issue.

Is there a proper technique for getting the requisite quote-delimited, comma-separated list into the SQL control command parameter correctly? Should I be using some sort of escape sequence for the single-quote and comma characters? Should I be using another "type" for the parameter; one that accepts an array of entries? I have not been able to find any examples or documentation that provides an answer.

A slight adjustment to the statements made in my earlier post: Using the "Test query" in the page design view for the control does NOT work. What does work is using SQL Server 2005 to run an equivalent query. So the page design view test query functionality and the ASP page functionality do indeed match, unlike what I stated earlier. I apologize for any confusion this may have caused.

But the problem still remains: how can I get the list of values passed into the control via the parameter?

Monday, February 20, 2012

Problem use sqldatasource to access stored procedure and get data bind to label control

Hi every experts

I have a exist Stored Procedure in SQL 2005 Server, the stored procedure contain few output parameter, I have no problem to get result from output parameter to display using label control by using SqlCommand in Visual Studio 2003. Now new in Visual Studio 2005, I can't use sqlcommand wizard anymore, therefore I try to use the new sqldatasource control. When I Configure Datasource in Sqldatasource wizard, I assign select field using exist stored procedure, the wizard control return all parameter in the list with auto assign the direction type(input/ouput...), after that, whatever I try, I click on Test Query Button at last part, I always get error messageThe Query did not return any data table.

My Question is How can I setup sqldatasource to access Stored Procedure which contain output parameter, and after that How can I assign the output parameter value to bind to the label control's Text field to show on web?

Thanks anyone, who can give me any advice.

Satoshi

Here's an example for you:

ASPX

<asp:label id="Label1" runat="server" /><asp:gridview id="GridView1" runat="server" datakeynames="ProductID" datasourceid="SqlDataSource1"></asp:gridview><asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString%>"onselected="SqlDataSource1_Selected" selectcommand="SELECT * FROM [Products]; SELECT @.count = COUNT(*) FROM [Products];"><selectparameters><asp:parameter direction="Output" name="count" type="int32" /></selectparameters></asp:sqldatasource>

CODE-BEHIND

protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e){if (e.Exception ==null){Label1.Text = String.Concat("Records returned:", e.Command.Parameters["@.count"].Value);}}
|||

Hi ecbruck

Thanks very much your help, I'm able to make it work now.