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.
- Webservice Application
- Host Webservice on IIS
- Windows CE Application.
First of all create a database in SQL Server.
Now insert data into this table like this.
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.
In Solution Explorer, open Service.cs.
Now write code in the Service.cs file.
- 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)]
- // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
- // [System.Web.Script.Services.ScriptService]
- public class Service: System.Web.Services.WebService
- {
- SqlConnection con;
- SqlDataAdapter adp;
- DataSet ds;
- public Service()
- {
- //Uncomment the following line if using designed components
- //InitializeComponent();
- }
- [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.
Host this published Webservice to IIS.
Now In the last module, create a Windows CE smart device application.
File -> New -> Smart Device App -> Windows CE.
Create a design form like this.
Now select Add Web Reference from the Solution Explorer.
Copy your running web service path and paste it here in the URL.
Replace “Localhost” and type that place “Your System Name” “CONtrivedi” or IP address.
Then press GO then press Add Refrence.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.