Scan Barcode and Verify Data From Database Using Web Service On Windows CE Portable Terminal Scanner Device

Introduction

This article shows how to scan a barcode in a Windows CE Portable Terminal Scanner Device and verify the scanned data from a SQL Server Database using a web service on a Windows CE Portable Terminal Scanner Device.
 
Modules
  1. Webservice Application
  2. Host Webservice on IIS
  3. Windows CE Application.
Step 1

First of all create a database in SQL Server.

File name : Barcode_Data
Create a table like this.
 

Now insert data into this table like this.

Ok.
 
Step 2

Now create an ASP.NET web service in Visual Studio.

Open Visual Studio the select File -> New -> Web Site then select ASP.NET Web Service.



Press OK.
 
Step 3

In Solution Explorer, open Service.cs.

Create a method depending on your requirements.
 
Open your Service.cs file in the web service website to write the code to get the user details from the database.


Step 4

Now write code in the Service.cs file.

Before writing the WebMethod in Service.cs first add the following namespaces:

 

  1. using System;  
  2. using System.Linq;  
  3. using System.Web;  
  4. using System.Web.Services;  
  5. using System.Web.Services.Protocols;  
  6. using System.Xml.Linq;  
  7. using System.Data.SqlClient;  
  8. using System.Data;  
  9. [WebService(Namespace = "http://tempuri.org/")]  
  10. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  11. // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.  
  12. // [System.Web.Script.Services.ScriptService]  
  13. public class Service: System.Web.Services.WebService  
  14. {  
  15.     SqlConnection con;  
  16.     SqlDataAdapter adp;  
  17.     DataSet ds;  
  18.     public Service()   
  19.     {  
  20.         //Uncomment the following line if using designed components  
  21.         //InitializeComponent();  
  22.     }  
  23.     [WebMethod]  
  24.     public string HelloWorld()   
  25.     {  
  26.         return "Hello World";  
  27.     }  
  28.     [WebMethod]  
  29.     public DataSet getmember()  
  30.     {  
  31.         con = new SqlConnection("Data Source=192.168.xx.xx\\SERVERR2;Initial Catalog=Barcode_Data;Connection Timeout=5000;Min Pool Size=100; Max Pool Size=2000;User ID=xxxx;Password=xxxxxxx");  
  32.         con.Open();  
  33.         adp = new SqlDataAdapter("select * from codedata", con);  
  34.         ds = new DataSet();  
  35.         adp.Fill(ds, "member");  
  36.         return ds;  
  37.     }  
  38.     [WebMethod]  
  39.     public int addition(int a, int b)  
  40.     {  
  41.         int c = a + b;  
  42.         return c;  
  43.     }  
  44.     [WebMethod]  
  45.     public DataTable getdatatable(string str)  
  46.     {  
  47.         con = new SqlConnection("Data Source=192.168.xx.xx\\SERVERR2;Initial Catalog=Barcode_Data;Connection Timeout=5000;Min Pool Size=100; Max Pool Size=2000;User ID=xxxx;Password=xxxxxx");  
  48.         DataTable dt = new DataTable();  
  49.         SqlCommand cmd = new SqlCommand();  
  50.         SqlDataAdapter da = new SqlDataAdapter();  
  51.         DataSet ds = new DataSet();  
  52.         cmd.Connection = con;  
  53.         cmd.CommandText = str;  
  54.         da.SelectCommand = cmd;  
  55.         da.Fill(ds, "Product_tbl");  
  56.         dt = ds.Tables["Product_tbl"];  
  57.         return dt;  
  58.     }  
  59. }  

 

Step 5

Now build this web service and publish this web service and then host it in IIS.

To host the website or web service on IIS read the article How to Configure ASP.Net Website in IIS.
 
Step 6

Host this published Webservice to IIS.

Run this WS in IIS.

Step 7

Now In the last module, create a Windows CE smart device application.

In Visual Studio:

File -> New -> Smart Device App -> Windows CE.

Create a design form like this.

Step 8

Now select Add Web Reference from the Solution Explorer.

Step 9

Copy your running web service path and paste it here in the URL.

Step 10

Replace “Localhost” and type that place “Your System Name” “CONtrivedi” or IP address.

Then press GO then press Add Refrence.
 
Step 11

Now call this web services function (method) in the WinCE application using Object.
  1. namespace SmartDeviceProject2  
  2. {  
  3.     public partial class Form1: Form  
  4.     {  
  5.         public Form1()  
  6.         {  
  7.             InitializeComponent();  
  8.         }  
  9.         contrivedi.Service obj = new contrivedi.Service();  
  10.         private void button2_Click(object sender, EventArgs e)  
  11.         {  
  12.             DataTable dt = new DataTable();  
  13.             sqry = "SELECT id,bcode,bdetail from Sheet1 WITH(INDEX(index_fab))";  
  14.             dt = obj.getdatatable(sqry);  
  15.             dataGrid1.DataSource = dt;  
  16.         }  
  17.         string sqry = "";  
  18.         private void textBox4_KeyDown(object sender, KeyEventArgs e)  
  19.         {  
  20.             if (e.KeyValue == 13)  
  21.             {  
  22.                 if (textBox4.Text != "")  
  23.                 {  
  24.                     string bcode = textBox4.Text;  
  25.                     sqry = "select bcode,bdetail from Sheet1 where bcode='" + textBox4.Text + "'";  
  26.                     DataTable dt = new DataTable();  
  27.                     dt = obj.getdatatable(sqry);  
  28.                     if (dt.Rows.Count > 0)  
  29.                     {  
  30.                         MessageBox.Show("Valid Barcode !!");  
  31.                         string codevalue = dt.Rows[0]["bdetail"].ToString();  
  32.                         label3.Text = codevalue;  
  33.                     }   
  34.                     else  
  35.                     {  
  36.                         MessageBox.Show("Invalid Barcode");  
  37.                     }  
  38.                 }  
  39.             }  
  40.         }  
  41.     }  
  42. }  

Now run this application on a Portable Terminal Scanner device.

It will scan Barcode Data and then validate from the SQL Server database and then display detailed data related to the Barcode.
 
Thanks.
 
If you have any query regarding this feel free to post your comment.

Next Recommended Readings