WorldofASP.NET : ASP.NET Directory, Tutorial, Hosting, and Source Code
You are 1 of 44 users


WorldofASP.NET >> Csharp Programming >> Applications

How to Build Smart Device Application With Web Services

Smart Devices Online Application
Published Date : 27 Jan 2008
Author : Marthayaputra
Language : C#
Platform : .NET,.NET Compact Framework
Technology : Visual Studio,IIS
Views : 11731
Rating : (4 votes so far)



Introduction

Smart device application with Web Services is very useful for smart device online application. It’s much faster than using Remote Data Access and easier to configure than Replication method.

Main

First we have to add new project in Visual Studio 2005. In this tutorial I’m using Pocket PC 2003 device and C# language. Then add the form with one data grid. Then we add the Web Service like picture below



After the web services project is added, u can see the service.cs class. This is the area where we code
our web service method . The sample “Hello World” method was created for an example.



Now we need to add web reference from the smart device project. I use “WsPPC” as the web service
name. So the full url is http://localhost/WsPPC/Service.asmx, put this url to the url text box in web
reference. Don’t forget “service.asmx” part, or else u cant find the reference. Once its added it will appear like this picture below. I put “WsPPC” as the web reference name, same as the web service project name.



Then hit the Add reference button and your web reference is ready. U can see the web reference on web reference folder in your smart device project. You will need to update your web reference every time you add new method in your web service project. Right click in your web reference and click update web reference. If you found an error while update your web reference, it’s probably there some error on your web services. Now we try to add method Retrieve Employees in web service. I created Company database with Employees table in it.

The Employees table contains:

EmpId varchar (5)
EmpName varchar(50)



Now add this method in service.cs :

[WebMethod] public DataSet GetEmployees() 
{ 
OpenConnection(); string query = "SELECT * FROM EMPLOYEES "; 
SqlDataAdapter da = new SqlDataAdapter(query,conn);
DataSet ds = new DataSet(); 
DataTable dt = new DataTable("Employees");
da.Fill(dt); conn.Close(); 
ds.Tables.Add(dt); return ds; 
} 
[WebMethod] public string GetEmployeeName(string empId) 
{ 
OpenConnection(); string query = "SELECT EMPNAME FROM EMPLOYEES WHERE EMPID ='" 
+ empId       +"'    ";string empName; 
SqlCommand cmd = new SqlCommand(); 
cmd.CommandText = query; 
cmd.CommandType =    CommandType.Text;
cmd.Connection = conn;
empName = cmd.ExecuteScalar().ToString();return    empName; 
} 

Don’t forget to add [WebMethod] tag to your web services method, or else it won’t know as the web service method in smart device application. Web service can return any data type such as Data Set, String, Integer, Class etc. But I haven’t figured out how to return Data Table. Now back to the Smart Device form we created before, and put these code on form load event. Don’t forget to import the web reference’s namespace :
using PPCWithWebServices.WsPPC;private void frmEmployee_Load(object sender, EventArgs e) 
{ 
Service webSevice = new Service(); 
webSevice.Url = "http://localhost/WsPPC/Service.asmx"; 
dg.DataSource = webSevice.GetEmployees().Tables[0]; 
} 

we need to point the web service’s url to the web service location same as the url we use to add the web reference. And if we run it, the result supposed to be like this



Now add another form like this



Put this code on Button employee click
private void btnEmpName_Click(object sender, EventArgs e) 
{ 
Service webService = new Service(); 
webService.Url = "http://PUTRA/WsPPC/Service.asmx"; 
txtEmployeeName.Text =webService.GetEmployeeName(txtEmpId.Text.Trim()); 
} 

When we click the Get Employee Button the result is :


Web services it very useful if we want to build an online application using smart devices. Its simple, fast and realible.

Download Source Code




Other Related and Popular Articles :


Author Profile : Marthayaputra

Click here to view Author Profile


How would you rate the quality of this content?
Poor Excellent

Comments

#"Unable to connect to the remote server"
08 May 2010 1:13 by : arun

Same error for me also
Unable to connect to the remote server

#Problmes
02 Apr 2010 11:14 by : Cristian Collazos

Hello! How are you. I am trying to development an example about How to Build Smart Device Application With Web Services but I can not to connect between smart device and web service. The error is: "Unable to connect to remote server"

Please help me! If you have an example please send me! Thanks!

Leave New Comments


Article Content copyright by Marthayaputra
Everything else Copyright © by WorldofASP.NET 2010

Category
.NET 3.5
AJAX and ATLAS
ASP.NET
C# Programming
Classic ASP
Enterprise Systems
General .NET
VB.NET Programming
Announcements
Earn Cash by writing an article or review
For more info Click here







Legend : - Within 3 Days - Within 6 Days - Within 9 Days

Home | Add Resources | Sponsored Listings | Advertise with Us | SiteMap 1 | SiteMap 2 | Link To Us | Contact Us
© 2002-2010 Worldofasp.net ASP.NET Directory, Hosting and Tutorials | All rights reserved
Our Partners : ASP.NET Web Hosting | ASP Hosting | ASP.NET Hosting | Phone Card | Calling Card |Stock Investing