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

Client side:

Now as our client is a completely different application domain, we will have to select new website from visual studio template and add the service reference to the client side which I have already discussed in my previous article.

 

Below is the screen shot:

In the above screen shot when we write the name of the leader and click on "Show Leader" button the service will search the for required information and display the result. Now to add leader we have button "Add New Leader" once you click on it a popup window will appear so that you can write the info and click on "SAVE and Close " button, the record will be inserted to database and will appear on the page.

Screen shot is below:

Once you click on "Save and Close" button the popup window will get closed and newly added leader will appear on page.

Below is screen shot:

At client side we have used Ajax Toolkit model popup extender and Asynchronous File Upload control to show the popup box to add records and to add the picture respectively.

Below is the code.

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

 

ServiceReference1.IService1 clientproxy = new ServiceReference1.Service1Client();

ServiceReference1.Leaders ldr = new ServiceReference1.Leaders();

DataTable dt = new DataTable();

ldr.p_name="Barak Obama";

 

dt = clientproxy.GetLeadersInfo(ldr);

GridView1.DataSource = dt;

GridView1.DataBind();

Image1.ImageUrl = "http://localhost:53137/images/"+Convert.ToString(dt.Rows[0]["Pic"]);

}

protected void btnAddLeader_Click(object sender, EventArgs e)

{

 

}

protected void btnSaveClose_Click(object sender, EventArgs e)

{

string filename="";

ServiceReference1.IService1 clientprxy = new ServiceReference1.Service1Client();

 

if (AsyncFileUpload1.HasFile)

{

var scheme = Request.Url.Scheme; // will get http, https, etc.

 

var host = Request.Url.Host; // will get www.mywebsite.com

 

var port = Request.Url.Port; // will get the port

 

var path = Request.Url.AbsolutePath;

 

string picpath = scheme.ToString() + "://" + host.ToString() + ":" + port.ToString() + Request.ApplicationPath + "/" + "images/";

 

// AsyncFileUpload1.SaveAs("F:\\programs\\dot net\\blog-webapp\\images\\" + AsyncFileUpload1.FileName);

AsyncFileUpload1.SaveAs("F:\\wcf programs\\Leaders Info Insert Update DELETE\\WcfServiceLeaders\\WcfServiceLeaders\\images\\" + AsyncFileUpload1.FileName);

filename = Convert.ToString(AsyncFileUpload1.FileName);

}

ServiceReference1.Leaders ldr = new ServiceReference1.Leaders();

ldr.p_name = txtname.Text;

ldr.p_country = txtCountry.Text;

ldr.p_pic = filename;

 

//clientprxy.AddLeader(txtname.Text, txtCountry.Text,filename);

clientprxy.AddLeader(ldr);

txtenterleader.Text = txtname.Text;

fillLeader();

// Panel1.Visible = false;

 

}

protected void btnsearch_Click(object sender, EventArgs e)

{

ServiceReference1.IService1 clientproxy = new ServiceReference1.Service1Client();

ServiceReference1.Leaders ldr = new ServiceReference1.Leaders();

ldr.p_name = txtenterleader.Text;

DataTable dt = new DataTable();

dt = clientproxy.GetLeadersInfo(ldr);

GridView1.DataSource = dt;

GridView1.DataBind();

Image1.ImageUrl = "http://localhost:53137/images/" + Convert.ToString(dt.Rows[0]["Pic"]);

}

 

protected void fillLeader()

{

ServiceReference1.IService1 clientproxy = new ServiceReference1.Service1Client();

ServiceReference1.Leaders ldr = new ServiceReference1.Leaders();

ldr.p_name = txtenterleader.Text;

 

DataTable dt = new DataTable();

 

dt = clientproxy.GetLeadersInfo(ldr);

GridView1.DataSource = dt;

GridView1.DataBind();

Image1.ImageUrl = "http://localhost:53137/images/" + Convert.ToString(dt.Rows[0]["Pic"]);

}

}

 

 

Continue...

Jump to: Part 1Part 2Part 3Part 5

Tags:

ASP.NET | WCF