WCF With Aspx and Silverlight
First Type
In Web.Config to
set basicHttpBinding for call by silverlight xaml side otherwise wsHttpBinding
1->WCF Service
–File IService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
// NOTE: You can use the
"Rename" command on the "Refactor" menu to change the
interface name "IService" in both code and config file together.
[ServiceContract]
public interface IService
{
[OperationContract]
int save(object id1, object name1, object salary1);
}
2->
WCF Service Call By Asps Page Service.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data;
using System.Data.SqlClient;
// NOTE: You can use the
"Rename" command on the "Refactor" menu to change the class
name "Service" in code, svc and config file together.
public class Service : IService
{
SqlConnection con;
public Service()
{
con= new SqlConnection("initial catalog=nkp;data source=nkp-pc;integrated
security=yes");
}
public int save(object id1, object
name1, object salary1) //prototype
{
SqlCommand cmd = new
SqlCommand("add_chk",
con);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id", (int)id1);
SqlDataReader rd = cmd.ExecuteReader();
bool b = rd.Read();
if (b == true)
{
return 0;
}
else
{
SqlCommand cmd1 = new SqlCommand("add_data2", con);
if (con.State == ConnectionState.Open)
{
con.Close();
}
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.AddWithValue("@id",
(int)id1);
cmd1.Parameters.AddWithValue("@name",
(string)name1);
cmd1.Parameters.AddWithValue("@salary",(int)salary1);
int temp = cmd1.ExecuteNonQuery();
if (con.State == ConnectionState.Open)
{
con.Close();
}
return 1;
}
}
}
3->
On Asps Page Take WCF Service Reference file
Aspx.cs
..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
}
protected void
Button1_Click(object sender, EventArgs e)
{
ServiceReference1.ServiceClient s = new ServiceReference1.ServiceClient();
int t = s.save(106, "ram",
43000);
Response.Write(t.ToString());
//return 0 (true) or
// 1
(false)
}
}
Or
:
//Aspx.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default :
System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
}
protected void
Button1_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(TextBox1.Text);
string name = TextBox2.Text;
int salary = Convert.ToInt32(TextBox3.Text);
ServiceReference1.ServiceClient s = new ServiceReference1.ServiceClient();
int t = s.save(id, name,salary);
if (t == 1)
{
Label1.Text = "Record is saved...";
//Response.Write(t.ToString());
}
else
{
Label1.Text = "Record is exist...";
//Response.Write(t.ToString());
}
}
}
Second Type
1->Class1.cs
Add in WCF Service Side
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class Class1
{
private int id;
private string name;
private decimal
salary;
public int stuid
{
get { return id; }
set { id = value; }
}
public string stuname
{
get { return name; }
set { name = value; }
}
public decimal
stusalary
{
get { return salary;
}
set { salary = value;
}
}
}
2->WCF
Service –File IService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
// NOTE: You can use the
"Rename" command on the "Refactor" menu to change the
interface name "IService" in both code and config file together.
[ServiceContract]
public interface IService
{
[OperationContract]
// int save(object id1, object name1, object salary1);
int save(Class1
c);
}
3->WCF Service
–File Service.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data;
using System.Data.SqlClient;
// NOTE: You can use the
"Rename" command on the "Refactor" menu to change the class
name "Service" in code, svc and config file together.
public class Service : IService
{
SqlConnection con;
public Service()
{
con= new SqlConnection("initial catalog=nkp;data source=nkp-pc;integrated
security=yes");
}
public int save(Class1 c)
{
SqlCommand cmd = new SqlCommand("add_chk", con);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id",
c.stuid);
SqlDataReader rd =
cmd.ExecuteReader();
bool b = rd.Read();
if (b == true)
{
return 0;
}
else
{
SqlCommand cmd1 = new SqlCommand("add_data2", con);
if (con.State == ConnectionState.Open)
{
con.Close();
}
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.AddWithValue("@id",c.stuid);
cmd1.Parameters.AddWithValue("@name",c.stuname);
cmd1.Parameters.AddWithValue("@salary",c.stusalary);
int temp = cmd1.ExecuteNonQuery();
if (con.State == ConnectionState.Open)
{
con.Close();
}
return 1;
}
}
4->Aspx.cs
file here take WCF service Reference
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default :
System.Web.UI.Page
{
protected void
Page_Load(object sender, EventArgs e)
{
}
protected void
Button1_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(TextBox1.Text);
string name = TextBox2.Text;
int salary = Convert.ToInt32(TextBox3.Text);
ServiceReference1.ServiceClient s = new ServiceReference1.ServiceClient();
ServiceReference1.Class1 c2 = new ServiceReference1.Class1();
c2.stuid = id;
c2.stuname = name;
c2.stusalary = salary;
int t = s.save(c2);
if (t == 1)
{
Label1.Text = "Record is saved...";
//Response.Write(t.ToString());
}
else
{
Label1.Text = "Record is exist...";
//Response.Write(t.ToString());
}
}
Third Type
1->Before use WCF Service to take
Here……..Use for WCF Service but use this
service on Silverlight xaml side then change
basicHttpBinding…..
2->Use the WCF Service in
Silverlight MainPage.xaml.cs take
reference on xaml side…..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication130712
{
public partial class MainPage : UserControl
{
ServiceReference1.ServiceClient s;
ServiceReference1.Class1 c2;
public MainPage()
{
InitializeComponent();
}
private void
button1_Click(object sender, RoutedEventArgs e)
{
int id = Convert.ToInt32(textBox1.Text);
string name = textBox2.Text;
int salary = Convert.ToInt32(textBox3.Text);
s= new ServiceReference1.ServiceClient();
c2 = new ServiceReference1.Class1();
c2.stuid = id;
c2.stuname = name;
c2.stusalary = salary;
//c2.stuid = 104;
//c2.stuname = "Rahul";
//c2.stusalary = 32000;
s.saveAsync(c2);
s.saveCompleted += new
EventHandler<ServiceReference1.saveCompletedEventArgs>(s_saveCompleted);
}
void s_saveCompleted(object
sender, ServiceReference1.saveCompletedEventArgs
e)
{
//MessageBox.Show(e.Result.ToString());
int t= e.Result;
if (t == 1)
{
label4.Content = "Record is
Saved...";
}
else
{
label4.Content = "Record is
exist...";
}
}
}
}
No comments:
Post a Comment