I have a stored procedure named "Graph" that should get a value to the parameter #Material, and I created a report in ASP.NET that should show a chart using the data from the stored procedure.
However, when I try to load the report I get:
"Data is unavailable"
Meaning, the parameter is not set correctly (with default value it works...)
The code-behind:
public partial class StatisticsPage : Page
{
string Connectionstring = "server=(local)\\SQLEXPRESS;database=PhilipsMaterials;Integrated Security=SSPI";
protected void btndisplay_Click(object sender, EventArgs e)
{
BindReport();
}
private void BindReport()
{
SSRSReport report = new SSRSReport();
SqlParameter[] sqlParams = new SqlParameter[] {
new SqlParameter("#Material","453567068441") };
string ReportDataSource = "DataSet1";
bool bind = report.CreateReport(Connectionstring, "graph", sqlParams, ref ReportViewer1, ReportDataSource);
if (bind)
{
ReportViewer1.Visible = true;
}
}
}
public class SSRSReport
{
public bool CreateReport(String Connectionstring, string StoreProcedureName , SqlParameter[] parameters, ref Microsoft.Reporting.WebForms.ReportViewer ReportViewer, string ReportDataSource)
{
bool reportbind = false;
using (SqlConnection con = new SqlConnection(Connectionstring))
{
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandType = CommandType.StoredProcedure;
com.CommandText = StoreProcedureName;
com.Parameters.AddRange(parameters);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(com);
da.Fill(ds);
ReportDataSource datasource = new ReportDataSource(ReportDataSource, ds.Tables[0]);
if ( ds.Tables[0].Rows.Count > 0)
{
ReportViewer.LocalReport.DataSources.Clear();
ReportViewer.LocalReport.DataSources.Add(datasource);
ReportParameter myParam = new ReportParameter("Material", "453567068441");
ReportViewer.LocalReport.SetParameters(myParam);
ReportViewer.ServerReport.Refresh();
reportbind = true;
}
}
return reportbind;
}
}
The markup:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="StatisticsPage.aspx.cs" Inherits="StatisticsPage" %>
<%# Register assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Test SSRS</title>
</head>
<body>
<form id="form1" runat="server">
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" Font-Size="8pt" WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt" Height="610px" Width="1179px" ShowParameterPrompts="true">
<LocalReport ReportPath="Report.rdlc" >
<DataSources>
<rsweb:ReportDataSource DataSourceId="ObjectDataSource1" Name="DataSet1" />
</DataSources>
</LocalReport>
</rsweb:ReportViewer>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetData" TypeName="PhilipsMaterialsDataSetTableAdapters.GraphTableAdapter" >
<SelectParameters>
<asp:Parameter Name="Material"/>
</SelectParameters>
</asp:ObjectDataSource>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</form>
</body>
</html>
If the error occurs without a default parameter, then the issue is that an <asp:parameter> does not supply a value unless you provide one. You can set the DefaultValue property from markup or code, which this value is passed directly to the backend. You can also switch to the ControlParameter, SessionParameter, etc. which pulls the value from somewhere else.
Related
There is a requirement of displaying both PackageID, and PackageName in a dropdown, so that customer can select the package for recharge.
As per the current code, PackageName is visbile using DataTextFiled property , which has been set in markup.
Please help to display both PackageID, and Package Name, and go through below codes-
DataBase-
CREATE TABLE [dbo].[PackageMaster](
[PackageID] [int] IDENTITY(1,1) NOT NULL,
[PackageName] [varchar](50) NULL
) ON
[PRIMARY]
Create Proc UspGetPackage
As
Begin
Select PackageID,PackageName from PackageMaster
End
--------------------------------------------------Design-------------------------------
<%# Page Language="C#" AutoEventWireup="true" CodeFile="PackageRequest.aspx.cs" Inherits="PackageRequest" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td><b>Select Package:</b></td> 
<td>
<asp:DropDownList ID="DDLPackage" runat="server" DataTextField="PackageName" DataValueField="PackageID"></asp:DropDownList>
</td>
</tr>
<tr>
<td><b>Select Sequence:</b></td> 
<td>
<asp:DropDownList ID="DDLPakcageSequence" runat="server" DataTextField="SequenceValue" DataValueField="SequenceID"></asp:DropDownList>
</td>
</tr>
<tr>
<td><asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="btnSubmit_Click" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
----------------------CodeBehind---------------
private DataSet GetData(string SPName, SqlParameter SPParameter)
{
string CS = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
SqlConnection con = new SqlConnection(CS);
SqlDataAdapter da = new SqlDataAdapter(SPName, con);
da.SelectCommand.CommandType = CommandType.StoredProcedure;
if (SPParameter != null)
{
da.SelectCommand.Parameters.Add(SPParameter);
}
DataSet DS = new DataSet();
da.Fill(DS);
return DS;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DDLPackage.DataSource = GetData("UspGetPackage",null);
DDLPackage.DataBind();
ListItem lstPkg = new ListItem("--Select Package--","-1");
DDLPackage.Items.Insert(0,lstPkg);
//DDLPakcageSequence.DataSource = GetData("UspGetPkgSequence",null);
//DDLPakcageSequence.DataBind();
//ListItem pkgsequence = new ListItem("--Select Sequence--", "-1");
//DDLPakcageSequence.Items.Insert(0, pkgsequence);
}
}
Concat two columns in stored procedure.
Select CONCAT(PackageID,' ', PackageName ) AS PackageID_PackageName, PackageID,PackageName from PackageMaster
In .aspx change DataTextField="PackageName" to DataTextField="PackageID_PackageName"
<asp:DropDownList ID="DDLPackage" runat="server" DataTextField="PackageID_PackageName" DataValueField="PackageID"></asp:DropDownList>
I'm having a project to create a website that connects my database to perform different functionalities. When I create the web form and connects it with the database, and when i click the button it's supposed that all the products will appear but it doesn't happen.
This is the SQL procedure:
CREATE PROC reviewOrders
AS
BEGIN
SELECT *
FROM Orders
END
And this is the c# code
protected void reviewOrders(object sender, EventArgs e)
{
string connStr = ConfigurationManager.ConnectionStrings["MyDbConn"].ToString();
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand("reviewOrders", conn);
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
and the HTML code:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="ReviewOrders.aspx.cs"
Inherits="GUCommerce.ReviewOrders" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="viewOrders" runat="server" OnClick ="reviewOrders" Height="45px"
Text="view orders" Width="148px" />
</div>
<p style="height: 121px">
</p>
<asp:Panel ID="x" Visible ="false" runat="server" Height="338px">
<asp:Table ID="orders" CellPadding ="4" runat="server" Height="67px" Width="316px">
</asp:Table>
</asp:Panel>
</form>
</body>
</html>
Can one please tell me what is missing?
Thanks is advance!
I would recommend using ASP Gridview instead of ASP Table. Gridviews (<asp:GridView>) are used to present data in tables. They actually get rendered as html tables. Here is how to build one using your code:
<asp:Panel ID="x" Visible="false" runat="server" Height="338px">
<%--<asp:Table ID="orders" CellPadding="4" runat="server" Height="67px" Width="316px"></asp:Table>--%>
<asp:GridView ID="gvOrders" CellPadding="4" runat="server" Height="67px" Width="316px"></asp:GridView>
</asp:Panel>
Now, in the code-behind there are a couple changes. A DataTable can be used to store the results of your query and then you can bind a DataTable to a GridView. To do this, you need a SqlDataAdapter which is shown below.
protected void reviewOrders(object sender, EventArgs e)
{
// data table variable outside of sql block
// you could also move the sql code to another method that returns a datatable
DataTable dt = null;
string connStr = ConfigurationManager.ConnectionStrings["MyDbConn"].ToString();
using (SqlConnection conn = new SqlConnection(connStr))
{
SqlCommand cmd = new SqlCommand("reviewOrders", conn);
cmd.CommandType = CommandType.StoredProcedure;
using (cmd)
{
conn.Open();
// Use SQL Data Adapter instead of Execute Non Query
using (SqlDataAdapter _Adapter = new SqlDataAdapter(cmd))
{
// Fill DataTable with results of query
dt = new DataTable();
_Adapter.Fill(dt);
}
}
}
//
gvOrders.DataSource = dt;
gvOrders.DataBind();
}
Note: I use using(SqlConnection) and using(cmd) to handle closing the connection and command for me. Give this a shot.
I have a stored procedure named "Graph" that should get a value to the parameter #Material, and I created a report in ASP.NET that should show a chart using the data from the stored procedure.
However, when I try to load the report I get:
An error has occurred during report processing.
Cannot create a connection to data source 'PhilipsMaterialsDataSet'.
ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'GetData' that has no parameters.
I tried different solutions but none of them worked. Also, I'm not sure if I should declare the parameter in the ASP code.
( by the way, GetData is not recognized here because it has one parameter (#Material-from the stored procedure) and for some reason, it is called without any parameters )
The code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
public partial class StatisticsPage : System.Web.UI.Page
{
string Connectionstring = "server=(local)\\SQLEXPRESS;database=PhilipsMaterials;Integrated Security=SSPI";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void btndisplay_Click(object sender, EventArgs e)
{
BindReport();
}
private void BindReport()
{
SSRSReport report = new SSRSReport();
SqlParameter[] sqlParams = new SqlParameter[] {
new SqlParameter("#Material","453567068441")
};
string ReportDataSource = "DataSet1";
bool bind = report.CreateReport(Connectionstring, "graph", sqlParams, ref ReportViewer1, ReportDataSource);
if (bind)
{
ReportViewer1.Visible = true;
}
}
}
public class SSRSReport
{
public SSRSReport()
{
//
// TODO: Add constructor logic here
//
}
public bool CreateReport(String Connectionstring,string StoreProcedureName ,SqlParameter[] Parameter,ref Microsoft.Reporting.WebForms.ReportViewer ReportViewer,string ReportDataSource)
{
bool reportbind = false;
using (SqlConnection con = new SqlConnection(Connectionstring))
{
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandType = CommandType.StoredProcedure;
com.CommandText = StoreProcedureName;
com.Parameters.AddRange(Parameter);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(com);
da.Fill(ds);
ReportDataSource datasource = new ReportDataSource(ReportDataSource, ds.Tables[0]);
if ( ds.Tables[0].Rows.Count > 0)
{
ReportViewer.LocalReport.DataSources.Clear();
ReportViewer.LocalReport.DataSources.Add(datasource);
//This is another solution I tried:
//List<ReportParameter> lstReportParameters = new List<ReportParameter>();
//ReportParameter objReportParameter = new ReportParameter("Material", "453567068441");
//lstReportParameters.Add(objReportParameter);
//ReportViewer.LocalReport.SetParameters(lstReportParameters);
// ReportViewer.ServerReport.Refresh();
reportbind = true;
}
}
return reportbind;
}
}
The ASP code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="StatisticsPage.aspx.cs" Inherits="StatisticsPage" %>
<%# Register assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Test SSRS</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" Font-Size="8pt" WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt" Height="610px" Width="1179px" ShowParameterPrompts="true">
<LocalReport ReportPath="Report.rdlc" >
<DataSources>
<rsweb:ReportDataSource DataSourceId="ObjectDataSource1" Name="DataSet1" />
</DataSources>
</LocalReport>
</rsweb:ReportViewer>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetData" TypeName="PhilipsMaterialsDataSetTableAdapters.GraphTableAdapter" >
</asp:ObjectDataSource>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</form>
</body>
</html>
public DataSet CreateReport(String Connectionstring,string StoreProcedureName ,SqlParameter[] Parameter,ref Microsoft.Reporting.WebForms.ReportViewer ReportViewer,string ReportDataSource) {
//return the dataset
}
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="CreateReport" TypeName="SqlHelper" />
can u try to change ur method return value to DataSet..
and in the codebehind try to databind to that method..
Maybe it might work..
I have a stored procedure named "Graph" that should get a value to the parameter #Material, and I created a report in ASP.NET that should show a chart using the data from the stored procedure.
However, when I try to load the report I get:
An error has occurred during report processing.
Cannot create a connection to data source 'PhilipsMaterialsDataSet'.
ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'GetData' that has no parameters.
I tried different solutions but none of them worked. Also, I'm not sure if I should declare the parameter in the ASP code.
( by the way, GetData is not recognized here because it has one parameter (#Material-from the stored procedure) and for some reason, it is called without any parameters )
The code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
public partial class StatisticsPage : System.Web.UI.Page
{
string Connectionstring = "server=(local)\\SQLEXPRESS;database=PhilipsMaterials;Integrated Security=SSPI";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void btndisplay_Click(object sender, EventArgs e)
{
BindReport();
}
private void BindReport()
{
SSRSReport report = new SSRSReport();
SqlParameter[] sqlParams = new SqlParameter[] {
new SqlParameter("#Material","453567068441")
};
string ReportDataSource = "DataSet1";
bool bind = report.CreateReport(Connectionstring, "graph", sqlParams, ref ReportViewer1, ReportDataSource);
if (bind)
{
ReportViewer1.Visible = true;
}
}
}
public class SSRSReport
{
public SSRSReport()
{
//
// TODO: Add constructor logic here
//
}
public bool CreateReport(String Connectionstring,string StoreProcedureName ,SqlParameter[] Parameter,ref Microsoft.Reporting.WebForms.ReportViewer ReportViewer,string ReportDataSource)
{
bool reportbind = false;
using (SqlConnection con = new SqlConnection(Connectionstring))
{
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandType = CommandType.StoredProcedure;
com.CommandText = StoreProcedureName;
com.Parameters.AddRange(Parameter);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(com);
da.Fill(ds);
ReportDataSource datasource = new ReportDataSource(ReportDataSource, ds.Tables[0]);
if ( ds.Tables[0].Rows.Count > 0)
{
ReportViewer.LocalReport.DataSources.Clear();
ReportViewer.LocalReport.DataSources.Add(datasource);
//This is another solution I tried:
//List<ReportParameter> lstReportParameters = new List<ReportParameter>();
//ReportParameter objReportParameter = new ReportParameter("Material", "453567068441");
//lstReportParameters.Add(objReportParameter);
//ReportViewer.LocalReport.SetParameters(lstReportParameters);
// ReportViewer.ServerReport.Refresh();
reportbind = true;
}
}
return reportbind;
}
}
The ASP code:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="StatisticsPage.aspx.cs" Inherits="StatisticsPage" %>
<%# Register assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Test SSRS</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" Font-Size="8pt" WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt" Height="610px" Width="1179px" ShowParameterPrompts="true">
<LocalReport ReportPath="Report.rdlc" >
<DataSources>
<rsweb:ReportDataSource DataSourceId="ObjectDataSource1" Name="DataSet1" />
</DataSources>
</LocalReport>
</rsweb:ReportViewer>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetData" TypeName="PhilipsMaterialsDataSetTableAdapters.GraphTableAdapter" >
</asp:ObjectDataSource>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</form>
</body>
</html>
The error is saying GetData requires some parameters, and you have to supply them by adding a parameters like like:
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetData" TypeName="PhilipsMaterialsDataSetTableAdapters.GraphTableAdapter" >
<SelectParameters>
<asp:Parameter Name="X" DefaultValue="Y" .. />
</SelectParameters>
</asp:ObjectDataSource>
I have a DataTable that I fill manually, ie,
newrow = dt.NewRow();
dt.Rows.Add(newrow);
and Im trying to benefit from the groupby features of LinqDataSource (as shown by Matt) by linking the LinqDataSource to the DataTable, but its just not happening.
Has anyone any experience with this?
<%# Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public class MyData
{
public MyData(){}
private DataTable dt = new DataTable("dt");
public DataTable MyTable
{
get { return dt; }
set { dt = value; }
}
}
protected void Page_Load(object sender, EventArgs e)
{
MyData mydata = new MyData();
mydata.MyTable.Columns.Add("column1");
DataRow dr = mydata.MyTable.NewRow();
dr[0] = "some data";
mydata.MyTable.Rows.Add(dr);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:LinqDataSource
ID="LinqDataSource1"
runat="server"
ContextTypeName="MyData"
TableName="MyTable">
</asp:LinqDataSource>
<asp:GridView ID="GridView1" runat="server" DataSourceID="LinqDataSource1">
</asp:GridView>
</form>
</body>
</html>
"dt" is not the name of the table try this
DataTable dt = new DataTable("dt");
and then try this would name the table as "dt".
No that just won't work at all. The ContextTypeName property needs to be that of a Context like a LinqtoSql DataContext and the TableName is the name of the database table that is mapped in your LinqToSql DBML file. Have you tried actually following the article you link to as it gives a pretty good overview of getting LinqToSQL going.