WCF application for displaying and inserting records into database using Data Contract and Data Member Attributes – Part 3

Data Contract- is a formal agreement between a service and a client that abstractly describes the data to be exchanged.

Code for Service1.svc

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

using System.Data.SqlClient;

using System.Data;

using System.Configuration;

 

namespace WcfServiceLeaders

{

// NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.

public class Service1 : IService1

{

Leaders ldr = new Leaders();

public string GetData(int value)

{

return string.Format("You entered: {0}", value);

}

public CompositeType GetDataUsingDataContract(CompositeType composite)

{

if (composite.BoolValue)

{

composite.StringValue += "Suffix";

}

return composite;

}

 

public DataTable GetLeadersInfo(Leaders ldr)

{

 

string constr = ConfigurationManager.ConnectionStrings["blogDevtechie"].ToString();

SqlConnection con = new SqlConnection();

con.ConnectionString = constr;

con.Open();

 

SqlCommand cmd = new SqlCommand("usp_selectLeader", con);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = ldr.p_name;// name;

//***************************************************************************

SqlParameter parm2 = new SqlParameter("@returnval", SqlDbType.Int);

parm2.Size = 50;

parm2.Direction = ParameterDirection.ReturnValue;

cmd.Parameters.Add(parm2);

//**************************************************************

SqlParameter parm1 = new SqlParameter("@msg", SqlDbType.VarChar);

parm1.Size = 50;

parm1.Direction = ParameterDirection.Output;

cmd.Parameters.Add(parm1);

//**************************************************************

SqlDataAdapter adpt = new SqlDataAdapter(cmd);

DataTable dt = new DataTable("Leaders");

adpt.Fill(dt);

return dt;

 

 

}

 

 

public void AddLeader(Leaders ldr)

{

 

string constr = ConfigurationManager.ConnectionStrings["blogDevtechie"].ToString();

SqlConnection con = new SqlConnection();

con.ConnectionString = constr;

con.Open();

 

SqlCommand cmd = new SqlCommand("usp_InsertLeader", con);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = ldr.p_name;

cmd.Parameters.Add("@country", SqlDbType.VarChar).Value = ldr.p_country;

cmd.Parameters.Add("@pic", SqlDbType.VarChar).Value = ldr.p_pic;

 

//***************************************************************************

SqlParameter parm2 = new SqlParameter("@returnval", SqlDbType.Int);

parm2.Size = 50;

parm2.Direction = ParameterDirection.ReturnValue;

cmd.Parameters.Add(parm2);

//**************************************************************

SqlParameter parm1 = new SqlParameter("@msg", SqlDbType.VarChar);

parm1.Size = 50;

parm1.Direction = ParameterDirection.Output;

cmd.Parameters.Add(parm1);

//**************************************************************

int i=cmd.ExecuteNonQuery();

}

}

}

 

In the above code we are implementing our method defined in our interface.

Here, we are mainly concerned with two methods GetLeadersInfo and AddLeader

GetLeadersInfo- will fetch the leader from database and expose it to the client side.

AddLeader-will add the record to the database which is coming from client.

In both the methods I am passing parameter ldr which is object of type Leaders, so ldr will contain all the member variable and properties of class Leaders and through this ldr we will set and access the values of members defined in class Leaders.

Eg.

public void AddLeader(Leaders ldr)

public DataTable GetLeadersInfo(Leaders ldr)

 

Continue…

Jump to: Part 1Part 2, Part 4Part 5

Tags:

ASP.NET | WCF