AJAX AutoCompleteExtender help needed - c#

I have two text boxes that should be getting filled with Ajax AutoCompleteExtender information. One text done with a Web Service and the other is a code behind Web Method. The Web Service one is for Customer IDs; If I start my page with the web service and put in a number for the ID it works and gives me all of the information but if I try to do it in my actual asp.net form then nothing happens when I type in a number to for my search criteria. Also, for the Web Method from the code behind doesn't seem to find anything for me when I type a letter...... I'm wondering if I am missing a reference or something that is making my actual web page not fetch the data or am I just doing it wrong as this is my first time using this.
I am using Visual Studio 2012
asp.net webform
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Orders.aspx.cs" Inherits="TropicalServer.UI.Orders" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<link type="text/css" rel="stylesheet" href="~/AppThemes/TropicalStyles/Orders.css" />
<title>Orders Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<!-- Criteria Bar -->
<div>
<table>
<tr>
<td>
<asp:Label ID="lblOrderDate" runat="server" Text="Order Date: "></asp:Label>
</td>
<td>
<asp:DropDownList ID="ddlOrderDate" runat="server"></asp:DropDownList>
</td>
<td>
<asp:Label ID="lblCustID" runat="server" Text="Customer ID: "></asp:Label>
</td>
<td>
<asp:TextBox ID="tbCustID" runat="server"></asp:TextBox>
<ajaxToolkit:AutoCompleteExtender ID="aceCustID" runat="server"
ServicePath="wsOrders.asmx"
TargetControlID="tbCustID"
MinimumPrefixLength="1"
CompletionInterval="100"
CompletionSetCount="1"
ServiceMethod="GetCustomerID"
UseContextKey="true"
EnableCaching="true"> </ajaxToolkit:AutoCompleteExtender>
</td>
<td>
<asp:Label ID="lblCustName" runat="server" Text="Customer Name: "></asp:Label>
</td>
<td>
<asp:TextBox ID="tbCustName" runat="server"></asp:TextBox>
<ajaxToolkit:AutoCompleteExtender ID="aceCustName" runat="server"
TargetControlID="tbCustName"
MinimumPrefixLength="1"
EnableCaching="true"
CompletionInterval="1000"
CompletionSetCount="1"
UseContextKey="True"
ServiceMethod="GetCustomerName">
</ajaxToolkit:AutoCompleteExtender>
</td>
<td>
<asp:Label ID="lblSalesManager" runat="server" Text="Sales Manager: "></asp:Label>
</td>
<td>
<asp:DropDownList ID="ddlSalesManager" runat="server"></asp:DropDownList>
</td>
</tr>
</table>
</div>
<!-- End Criteria -->
Code Behind asp.net
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using TropicalServer.DAL;
namespace TropicalServer.UI
{
public partial class Orders : System.Web.UI.Page
{
#region Declerations
DALConnection TropConnection;
#endregion
#region Constructor
public Orders()
{
TropConnection = new DALConnection();
}
#endregion
protected void Page_Load(object sender, EventArgs e)
{
}
#region WebMethod
[System.Web.Script.Services.ScriptMethod()]
[System.Web.Services.WebMethod]
public List<string> GetCustomerName(string prefixText)
{
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "spCustName";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#CustName", prefixText);
cmd.Connection = TropConnection.GetConnection();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
List<string> CustomerNames = new List<string>();
for (int i = 0; i < dt.Rows.Count; i++)
{
CustomerNames.Add(dt.Rows[i]["CustName"].ToString());
}
return CustomerNames;
}
#endregion
}
}
Web Service
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using TropicalServer.DAL;
namespace TropicalServer
{
/// <summary>
/// Summary description for wsOrders
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class wsOrders : System.Web.Services.WebService
{
#region Declerations
DALConnection TropConnection;
#endregion
#region Constructor
public wsOrders()
{
TropConnection = new DALConnection();
}
#endregion
//"SELECT * FROM tblOrder WHERE OrderCustomerNumber LIKE #CustID+'%'"
[WebMethod]
public List<string> GetCustomerID(string prefixText)
{
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "spCustID";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#CustID", prefixText);
cmd.Connection = TropConnection.GetConnection();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
List<string> CustomerIDs = new List<string>();
for (int i = 0; i < dt.Rows.Count; i++)
{
CustomerIDs.Add(dt.Rows[i]["OrderCustomerNumber"].ToString());
}
return CustomerIDs;
}
}
}
WEB CONFIG
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="TropicalServerConnectionString" value="Initial Catalog=TropicalServer;Data Source=Nicolas-PC\SQLEXPRESS;Integrated Security=true;" />
</appSettings>
<connectionStrings>
<add name="TropicalServerConnectionString" providerName="System.Data.SqlClient" connectionString="Data Source=Nicolas-PC;Initial Catalog=TropicalServer;Integrated Security = true" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
<add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
</assemblies>
</compilation>
<authentication mode="Forms">
<!--<forms loginUrl="~/Account/Login.aspx" timeout="2880" />-->
</authentication>
<pages>
<controls>
<add tagPrefix="ajaxToolkit" assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" />
</controls>
</pages></system.web>
</configuration>

I think your problem is with your method signature. If you have UseContextKey="True", the method should be:
public static string[] GetCustomerID(string prefixText, int count, string contextKey)
{
}
If UseContextKey="False", the method should be:
public static string[] GetCustomerID(string prefixText, int count)
{
}

Related

Adding data to SQL database with C# with no result

I try to add data from a form to one of my SQL Server database table. I was reading all the materials on stack but it seems I am doing something wrong I can't see.
Simple code to add data, no luck
Web form
<%# Page Language="C#" AutoEventWireup="true"
CodeBehind="Reports.aspx.cs"
Inherits="WAPReview.Reports" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label CssClass="label" ID="Label7" runat="server" Text="Name" />
<asp:TextBox CssClass="textbox" ID="TextBox1" runat="server" />
<p>
<asp:Button ID="Button" runat="server" Text="Save Data" />
</p>
</div>
</form>
</body>
</html>
C# code:
using System;
using System.Configuration;
using System.Data.SqlClient;
namespace WAPReview
{
public partial class Reports : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button_Click(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
SqlConnection conn = new SqlConnection(connectionString);
string sqlInsert = "INSERT INTO tstTable (Name) VALUES (#Name)";
using (SqlCommand command = new SqlCommand(sqlInsert, conn))
{
command.Parameters.AddWithValue("#Name", TextBox1.Text);
conn.Open();
command.ExecuteNonQuery();
conn.Close();
}
}
}
}
web.config for connection
<connectionStrings>
<add name="ConnString"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\WAPReview.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
I think you did not generate onclick event of button properly replace button code by below
<asp:Button ID="Button" OnClick="Button_Click" runat="server" Text="Save Data" />
and check it using breakpoint on the click event if code is executing or not.
pls check with
string connectionString = ConfigurationManager.ConnectionStrings["ConnString"].tostring();
also add parathasis to webconfig connection string
<add name="ConnString" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\WAPReview.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />

GETTING ERROR IN SIGN UP PAGE for inserting values in SQL server DATABASE table through webform TEXTBOX

hi I am trying to make a log in page as well as sign up page in asp.net using 3 tier architecture by using sql server architecture. I am able to fetch data from sql server data base which I have manually inserted during table creation in database and I am able to use it in my log in page.
I have also created a sign up page but I am not able to get the values from sign up webform textbox to sqlserver database I am getting some error kindly help me with this.
I have given the connection string of sql server in web.config
my sql server table creation code
CREATE TABLE LOGINDETAILS
(USERID VARCHAR(50),
PASSWORD VARCHAR (50)
);
INSERT INTO LOGINDETAILS (USERID,PASSWORD) values( 'sam', 'pass');
web.config connection string code
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="DBcon" connectionString="Data Source=P3A-B1YH882\SQLSERVER;Initial Catalog=master;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
</configuration>
my business layer /middle layer code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using DataAcess;
using System.Data.SqlClient;
using System.Data.Sql;
namespace middlelayer
{
public class UserBO
{
private string _UserName = " ";
public string UserName
{
get { return _UserName; }
set { _UserName = value; }
}
private string _Password = " ";
public string Password
{
get { return _Password; }
set { _Password = value; }
}
DataA da = new DataA();
public bool getUser()
{
if (da.IsValid(UserName, Password).Tables[0].Rows.Count == 0)
{
return false;
}
else
{
return true;
}
}
}
}
my datAccess layer code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;
namespace DataAcess
{
public class DataA
{
string conString = ConfigurationManager.ConnectionStrings["DBcon"].ToString();
public DataSet IsValid(string UserName, string Password)
{
SqlConnection con = new SqlConnection(conString);
con.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM LOGINDETAILS WHERE USERID ='" + UserName + "' and PASSWORD= '" + Password + "'", con);
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(ds);
return ds;
}
}
}
MY LOGIN PAGE CODE
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="login.aspx.cs" Inherits="WebApplication4.login" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body style="height: 277px">
<form id="form1" runat="server">
<div>
<asp:Label ID="lbluserid" runat="server" BackColor="#FFFF99" BorderStyle="Ridge" Height="17px" Text="User ID" Width="52px"></asp:Label>
<asp:TextBox ID="txtuserid" runat="server" BackColor="#99FFCC" style="margin-left: 122px"></asp:TextBox>
<br />
</div>
<p>
<asp:Label ID="lblpassword" runat="server" BackColor="#FFFF99" BorderStyle="Ridge" Text="Password"></asp:Label>
<asp:TextBox ID="txtpassword" TextMode="Password" runat="server" BackColor="#99FFCC" style="margin-left: 110px" ></asp:TextBox>
</p>
<p>
</p>
<asp:Button ID="btnlogin" runat="server" BackColor="#33CCFF" BorderStyle="Ridge" OnClick="btnlogin_Click" style="margin-left: 78px" Text="Login" Width="107px" />
<p>
</p>
<asp:Label ID="Label1" runat="server" Text="NOT REGISTERED ??"></asp:Label>
<asp:HyperLink ID="HyperLink1" runat="server" BorderStyle="Outset" NavigateUrl="~/sign_up.aspx">SIGN UP</asp:HyperLink>
</form>
</body>
</html>
* MY SIGN UP PAGE CODE*
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="sign_up.aspx.cs" Inherits="WebApplication4.sign_up" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Label ID="lblssignup" runat="server" BackColor="#FF99CC" Text="SIGN UP"></asp:Label>
<br />
<br />
<p>
<asp:Label ID="lblsuserid" runat="server" Text="ENTER USER ID"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" style="margin-bottom: 0px"></asp:TextBox>
</p>
<asp:Label ID="lblspassword" runat="server" Text="ENTER PASSWORD"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<p>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" Width="66px" />
</p>
</form>
</body>
</html>
SIGN UP PAGE BUTTON CODE FOR ENTERING DATA INTO SQL SERVER DATABASE ON BUTTON CLICK
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using middlelayer;
namespace WebApplication4
{
public partial class sign_up : System.Web.UI.Page
{
string conString = ConfigurationManager.ConnectionStrings["DBcon"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(conString);
con.Open();
string ins= "Insert into [LOGINDETAILS](USERID, PASSWORD) VALUES ('" +TextBox1.Text+ "' , '" +TextBox2.Text+ "')";
SqlCommand com = new SqlCommand(ins,con);
DataSet du = new DataSet();
SqlDataAdapter sdi = new SqlDataAdapter(com);
sdi.Fill(du);
con.Close();
}
}
}
I AM getting error in this last code only of sign up button it is not able to insert values of SIGN UP webform Textbox to sql server databse table and also not reflecting the real values which I want to add in sql server TABLE using sign up webform and also noty saving it. It is sending some error values . kindly help me with this.
BELOW ARE THE IMAGES OF LOG IN AS WELL AS SIGN UP PAGE FOR REFERENCE
LOGIN PAGE WEB FORM
SIGN UP PAGE WEBFORM
KINDLY HELP IN RESOLVING THIS ISSUE
Try this:
SqlConnection con = new SqlConnection(conString);
con.Open();
string ins= "Insert into [LOGINDETAILS](USERID, PASSWORD) VALUES (#param1 , #param2)";
SqlCommand cmd = new SqlCommand(ins,con);
cmd.Parameters.Add("#param1", SqlDbType.Varchar, 50).value = TextBox1.Text;
cmd.Parameters.Add("#param2", SqlDbType.Varchar, 50).value = TextBox2.Text;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
con.Close()

Ajax AutoCompleteExtender not hitting code behind in userControl C#

I'm trying to use the Ajax autoCompleteExtender to auto complete my textboxes using a sql query in my code behind. The problem I'm having is that my code behind isn't being hit at all.
What happens instead is my autocomplete options display as the code from my masterpage.
I've done some research and the problem might be that my textboxes are in an ascx file called from my aspx file but I can't figure out a way around it. I've even tried pointing the ServicePath to a webService and it still doesn't hit.
Any input would help, thank you!
Here's my relevant code:
Aspx File:
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage/Admin.Master" AutoEventWireup="true" EnableEventValidation="false" %>
<%# Register TagPrefix="AdminSearch" TagName="AdminSearch" Src="AdminSearch.ascx" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<AdminSearch:AdminSearch runat="server" ID="AdminSearch" />
</asp:Content>
Ascx File:
<%# Control Language="C#" ClassName="WebUserControl" AutoEventWireup="true" EnableViewState="true" Inherits="AdminSearch" CodeBehind="AdminSearch.ascx.cs" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<link href="../Style/Search/AdminSearch.css" rel="stylesheet" />
<script src="../Scripts/js/Search/AdminSearch.js"></script>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods = "true">
</asp:ScriptManager>
<td class="g3">
<br />
<asp:TextBox ID="TextBoxTierProfile" CssClass="g3" runat="server"></asp:TextBox><br />
<cc1:AutoCompleteExtender ServiceMethod="SearchProfiles" ServicePath="~/AutoComplete.asmx"
MinimumPrefixLength="2"
CompletionInterval="1" EnableCaching="true" CompletionSetCount="10"
TargetControlID="TextBoxTierProfile"
ID="AutoCompleteExtender2" runat="server" FirstRowSelected="false">
</cc1:AutoCompleteExtender>
</td>
Asmx File:
using System.Collections.Generic;
using System.Web.Services;
using System.Configuration;
using System.Data.SqlClient;
using System.Web;
namespace InternalTools.Admin.Search
{
/// <summary>
/// Summary description for AutoComplete
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService
{
static string sConnection;
public AutoComplete()
{
if (HttpContext.Current.Request.ServerVariables["LOCAL_ADDR"].ToString().Contains("127.0.0.1"))
{
sConnection = ConfigurationManager.ConnectionStrings["Reporting"].ToString();
}
else
{
sConnection = ConfigurationManager.ConnectionStrings["live"].ToString();
}
}
[System.Web.Script.Services.ScriptMethod]
[WebMethod]
public static string[] SearchProfiles(string prefixText, int count)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = sConnection;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = (I've cut out the query for security reasons)
cmd.Parameters.AddWithValue("#SearchText", prefixText);
cmd.Connection = conn;
conn.Open();
List<string> profiles = new List<string>();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
profiles.Add(sdr["PROFILENAME"].ToString());
}
}
conn.Close();
return profiles.ToArray();
}
}
}
}
}

ASP.NET report with parameter

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

ASP.NET report with a parameter

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>

Categories

Resources