/Interface
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]
[FaultContract(typeof(MyReason))]
string GetData(int value);
}
public class MyReason
{
private string Reason;
public string MyCause { get; set; }
}
//class to implement interface
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.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;
string ret;
public string GetData(int r)
{
try
{
con = new SqlConnection("initial catalog=cac;data source=windows-pc\\sqlexpress;integrated security=yes");
SqlCommand cmd = new SqlCommand("select name from student where seat_no=" + r + "", con);
con.Open();
SqlDataReader rd = cmd.ExecuteReader();
bool b = rd.Read();
if (b == false)
{
ret = null;
}
else
{
ret = rd["name"].ToString();
}
}
catch (Exception e1)
{
MyReason m = new MyReason();
m.MyCause = "he has internal pain in stomach";
throw new FaultException<MyReason>(m);
}
finally
{
con.Close();
}
return ret;
}
}
//At Client Side
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceModel;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
int s1 = Convert.ToInt32(TextBox1.Text);
ServiceReference1.ServiceClient s = new ServiceReference1.ServiceClient();
string y = s.GetData(s1);
if (y == null)
{
Label1.Text = "Rec not found";
}
else
{
Label1.Text = y;
}
}
catch (FaultException<ServiceReference1.MyReason> m)
{
Label1.Text = m.Detail.MyCause;
}
}
}
/
No comments:
Post a Comment