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
- Webservice Application
-
Host Webservice on IIS
-
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:
- using System;
- using System.Linq;
- using System.Web;
- using System.Web.Services;
- using System.Web.Services.Protocols;
- using System.Xml.Linq;
- using System.Data.SqlClient;
- using System.Data;
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
-
-
- public class Service: System.Web.Services.WebService
- {
- SqlConnection con;
- SqlDataAdapter adp;
- DataSet ds;
- public Service()
- {
-
-
- }
- [WebMethod]
- public string HelloWorld()
- {
- return "Hello World";
- }
- [WebMethod]
- public DataSet getmember()
- {
- 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");
- con.Open();
- adp = new SqlDataAdapter("select * from codedata", con);
- ds = new DataSet();
- adp.Fill(ds, "member");
- return ds;
- }
- [WebMethod]
- public int addition(int a, int b)
- {
- int c = a + b;
- return c;
- }
- [WebMethod]
- public DataTable getdatatable(string str)
- {
- 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");
- DataTable dt = new DataTable();
- SqlCommand cmd = new SqlCommand();
- SqlDataAdapter da = new SqlDataAdapter();
- DataSet ds = new DataSet();
- cmd.Connection = con;
- cmd.CommandText = str;
- da.SelectCommand = cmd;
- da.Fill(ds, "Product_tbl");
- dt = ds.Tables["Product_tbl"];
- return dt;
- }
- }
Step 5
Now build this web service and publish this web service and then host it 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.
- namespace SmartDeviceProject2
- {
- public partial class Form1: Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- contrivedi.Service obj = new contrivedi.Service();
- private void button2_Click(object sender, EventArgs e)
- {
- DataTable dt = new DataTable();
- sqry = "SELECT id,bcode,bdetail from Sheet1 WITH(INDEX(index_fab))";
- dt = obj.getdatatable(sqry);
- dataGrid1.DataSource = dt;
- }
- string sqry = "";
- private void textBox4_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyValue == 13)
- {
- if (textBox4.Text != "")
- {
- string bcode = textBox4.Text;
- sqry = "select bcode,bdetail from Sheet1 where bcode='" + textBox4.Text + "'";
- DataTable dt = new DataTable();
- dt = obj.getdatatable(sqry);
- if (dt.Rows.Count > 0)
- {
- MessageBox.Show("Valid Barcode !!");
- string codevalue = dt.Rows[0]["bdetail"].ToString();
- label3.Text = codevalue;
- }
- else
- {
- MessageBox.Show("Invalid Barcode");
- }
- }
- }
- }
- }
- }
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.