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

In this article, we will see how we can insert data and display records using WCF. We will be using data contract and data member attributes, we will also be using two completely different application domains for client and service respectively.

So first we will make a table in database.

Below is the code.

CREATE TABLE [dbo].[blog_Leaders](

    [Name] [nvarchar](50) NOT NULL,

    [Country] [nvarchar](50) NOT NULL,

    [Pic] [nvarchar](max) NULL,

    [ID] [int] IDENTITY(1,1) NOT NULL,

CONSTRAINT [PK_blog_Leaders] PRIMARY KEY CLUSTERED

(

    [ID] ASC

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

) ON [PRIMARY]

 

Stored Procedure for Inserting Leaders:

CREATE PROCEDURE [dbo].[usp_InsertLeader]

(

@Name nvarchar(100),

@Id int=0,

@pic nvarchar(200),

@country nvarchar(50),

@msg varchar(100) output

)

    -- Add the parameters for the stored procedure here

    

AS

BEGIN try

 

insert into blog_Leaders(Name,Country,Pic)

values(@Name,@country,@pic)

--select 'successfull'

return 0

END try

begin catch

select @msg='Error!!!Problem Occured During Process'

return 1

end catch

 

Stored Procedure for Displaying records

CREATE PROCEDURE [dbo].[usp_selectLeader]

(

@Name nvarchar(100),

@Id int=0,

@msg varchar(100) output

)

    -- Add the parameters for the stored procedure here

    

AS

BEGIN try

 

select * from blog_Leaders where name like @Name+'%';

--select 'successfull'

return 0

END try

begin catch

select @msg='Error!!!Problem Occured During Process'

return 1

end catch:

 

Second, we need to select WCF service from templates which I have already shown in my previous articles which are here

Once we have selected WCF service from template you will be provided with two files in App_Code Directory

 

Continue …

Jump to: Part 2Part 3Part 4Part 5

Tags:

ASP.NET | WCF