Ans:-
Here get data into RadComboBox from DB using WCF web services. In below UI of the application..
For this create one Website as->File->New Website->Give Website Name as TestingWithTellerikControls->ok
->select Wcf Services from new template
->automatically 4 files will create in Solution Explorer
->as I) Service.cs and IService.cs in App_Code folder
II)App_Data folder
III)Service and Web.config file.
Then write code in
IService.cs:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
// NOTE: If you change the interface name "IService" here, you must also update the reference to "IService" in Web.config.
[ServiceContract]
public interface IService
{
[OperationContract]
List<StateDetails> GetStateData();
}
public class StateDetails
{
int sid;
string sname = string.Empty;
[DataMember]
public int Sid
{
get { return sid; }
set { sid = value; }
}
public string Sname
{
get { return sname; }
set { sname = value; }
}
}
Web.Config:-
<connectionStrings>
<add name="conStr" connectionString="server=.;database=subsdb;user id=sa;pwd=123"/></connectionStrings>
Service.cs:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
// NOTE: If you change the class name "Service" here, you must also update the reference to "Service" in Web.config.
public class Service : IService
{
private string strConnection = ConfigurationManager.ConnectionStrings["conStr"].ToString();
public List<StateDetails> GetStateData()
{
List<StateDetails> statedetails = new List<StateDetails>();
using (SqlConnection con = new SqlConnection(strConnection))
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from state", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
StateDetails testInfo = new StateDetails();
testInfo.Sid = Convert.ToInt32(dt.Rows[i]["sid"].ToString());
testInfo.Sname = dt.Rows[i]["sname"].ToString();
statedetails.Add(testInfo);
}
}
con.Close();
}
return statedetails;
}
}
For consuming above services into this website,Right click and select Add Service References and then Paste above copied link and then clicked go.....and then give service name as....ServiceReference1.And then add this ServiceReference1 to Namespace.
RadComboBoxWithWcfServices.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RadComboBoxWithWcfServices.aspx.cs" Inherits="RadComboBoxWithWcfServices" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!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 runat="server">
<title>RadComboBoxWithWcfServices Page</title>
</head>
<body>
<form id="form1" runat="server">
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
</telerik:RadScriptManager>
<div>
<center>
Select State : <telerik:RadComboBox ID="RadComboBoxState" runat="server" EnableLoadOnDemand="true" EnableVirtualScrolling="true" ShowMoreResultsBox="true" EmptyMessage="Type Here..">
<WebServiceSettings Path="Services.svc" Method="GetStateData" />
</telerik:RadComboBox>
<br />
</center>
</div>
</form>
</body>
</html>
Here get data into RadComboBox from DB using WCF web services. In below UI of the application..
For this create one Website as->File->New Website->Give Website Name as TestingWithTellerikControls->ok
->select Wcf Services from new template
->automatically 4 files will create in Solution Explorer
->as I) Service.cs and IService.cs in App_Code folder
II)App_Data folder
III)Service and Web.config file.
Then write code in
IService.cs:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
// NOTE: If you change the interface name "IService" here, you must also update the reference to "IService" in Web.config.
[ServiceContract]
public interface IService
{
[OperationContract]
List<StateDetails> GetStateData();
}
public class StateDetails
{
int sid;
string sname = string.Empty;
[DataMember]
public int Sid
{
get { return sid; }
set { sid = value; }
}
public string Sname
{
get { return sname; }
set { sname = value; }
}
}
Web.Config:-
<connectionStrings>
<add name="conStr" connectionString="server=.;database=subsdb;user id=sa;pwd=123"/></connectionStrings>
Service.cs:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
// NOTE: If you change the class name "Service" here, you must also update the reference to "Service" in Web.config.
public class Service : IService
{
private string strConnection = ConfigurationManager.ConnectionStrings["conStr"].ToString();
public List<StateDetails> GetStateData()
{
List<StateDetails> statedetails = new List<StateDetails>();
using (SqlConnection con = new SqlConnection(strConnection))
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from state", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
StateDetails testInfo = new StateDetails();
testInfo.Sid = Convert.ToInt32(dt.Rows[i]["sid"].ToString());
testInfo.Sname = dt.Rows[i]["sname"].ToString();
statedetails.Add(testInfo);
}
}
con.Close();
}
return statedetails;
}
}
For consuming above services into this website,Right click and select Add Service References and then Paste above copied link and then clicked go.....and then give service name as....ServiceReference1.And then add this ServiceReference1 to Namespace.
RadComboBoxWithWcfServices.aspx:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RadComboBoxWithWcfServices.aspx.cs" Inherits="RadComboBoxWithWcfServices" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!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 runat="server">
<title>RadComboBoxWithWcfServices Page</title>
</head>
<body>
<form id="form1" runat="server">
<telerik:RadScriptManager ID="RadScriptManager1" runat="server">
</telerik:RadScriptManager>
<div>
<center>
Select State : <telerik:RadComboBox ID="RadComboBoxState" runat="server" EnableLoadOnDemand="true" EnableVirtualScrolling="true" ShowMoreResultsBox="true" EmptyMessage="Type Here..">
<WebServiceSettings Path="Services.svc" Method="GetStateData" />
</telerik:RadComboBox>
<br />
</center>
</div>
</form>
</body>
</html>
RadComboBoxWithWcfServices.aspx.cs:-
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using ServiceReference1;
public partial class RadComboBoxWithWcfServices : System.Web.UI.Page
{
ServiceReference1.ServiceClient proxy = new ServiceClient();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindTestDetails();
}
}
void BindTestDetails()
{
List<StateDetails> testdetails = new List<StateDetails>();
RadComboBoxState.DataTextField = "sname";
RadComboBoxState.DataValueField = "sid";
RadComboBoxState.DataSource = proxy.GetStateData();
RadComboBoxState.DataBind();
}
}
Create Table:-
create table state(sid int,sname varchar(30))
create table state(sid int,sname varchar(30))
No comments:
Post a Comment