Click here to Skip to main content
1,822 members
Articles / Security / ASP.NET
Article

Single Database use for both Web and Desktop application with Web Service

Rate me:
Please Sign up or sign in to vote.
4.60/5 (5 votes)
6 Dec 2012CPOL 28.3K   103   2   2
In this article you can access single database in both desktop and web application with web service

Background

Sometimes we want to use a single database for web and desktop version. In those case we need to work same task, same interface, class, method in web version and desktop version.In those case we may need to use a same method in web again desktop.

Again sometimes we need to work a partial part in web and another part in desktop. For example: In those case we may need to use a same method in web again desktop.

For example: Application for food industry, more exactly a software to: Delivery, Take Way, Table Reservation, POS, Accounts Payable and Receivable, Prints(receipt), Kitchen Monitors Orders, Customers Orders Control and Fiscal Area. In that case normally clients demand like this :

  1. Web Area (Basically do the follow
    • Show Catalog with the products
    • Customers Make Orders
    • Customers Pay for the Orders
    • etc ... as mentioned above
  2. Desktop Area
    • Manage Orders
    • Manage Customers
    • Manage Suppliers
    • Manage Accounts Payable and Receivable
    • etc ... as mentioned above

In the above web and desktop part we may need to use a same method in web again desktop. In those case we can use Web Service to make more reusable. But web service has different task. First I am discussion about Web Service.

  • What is Web Service?
  • Components of Web Service?
  • Why we need Web Service?
  • How it works ?
  • How Web Service works in .net web application and desktop application?

What is Web Service?

Web service is a technology framework, which allows machine-to-machine (M2M) interaction over the network. It can call application component. This protocol communicates with XML. It is not tied to any one operating system or programming language.

Components of Web Service?

The basic Web services platform is XML + HTTP. All the standard Web Services works using following components

  1. SOAP (Simple Object Access Protocol)
  2. UDDI (Universal Description, Discovery and Integration)
  3. WSDL (Web Services Description Language)

Learn more SOAPUDDIWSDL

Why we need Web Service?

The main reason to be popular of web service is because with web service, each application like PHP, C++, VB, C# etc. must adhere to a set of standardized protocols for sharing and accessing data. This way, two programs can speak to each other, regardless of operating system, database or programming language compatibility. Instead, everyone agrees on a set of rules by which these interactions will take place. This distinct feature is called Service-Oriented Architecture (SOA).

How Web Service works?

  • The client program bundles the client page information into a SOAP message.
  • This SOAP message is sent to the Web Service as the body of an HTTP POST request.
  • The Web Service unpacks the SOAP request and converts it into a command that the application can understand. The application processes the information as required and responds with a new unique data’s.
  • Next, the Web Service packages up the response into another SOAP message, which it sends back to the client program in response to its HTTP request.
  • The client program unpacks the SOAP message to obtain the results of the client page and required process

Web Services has two types of user:

  1. Reusable application-components
  2. Connect existing software

How I use Web Service in .net web application and desktop application?

First user application and web service middle ware makes SOAP message and communicate over HTTP. We can say that SOAP as a message format for sending message between applications using XML. It is independent of technology, platform and extensible also.

Discussion about code

Database: Create a database named TESTDB in sa mode with password sqladmin, then run the given script

In the solution create two project, one for web and another for desktop.

code analysis

Create a Web Service:

Create method to get data from database.

C++
[WebMethod]
        public DataSet GetSampleDataDT()
        {
            // getting connection string
            string conStr = WebConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
 
            DataTable dt = new DataTable();
            SqlDataReader dr = null;
            using (SqlConnection conn = new SqlConnection(conStr))
            {
                // Creating insert statement
                string sql = string.Format(@"select id,name,value1 from Table_1");
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;
                conn.Open();
 
                dr = cmd.ExecuteReader();
                dt.Load(dr);
 
                conn.Close();
                cmd = null;
 
            }
            DataSet dsReturn = new DataSet();
            dsReturn.Tables.Add(dt);
            return dsReturn;
        }

In the above method I just use normal query to get data from database. Again also create a method for insert data in database.

C++
 [WebMethod]
 public void WB_HR_InsertMethod(int id, string name, int salary)
        {
            // getting connection string
            string conStr = WebConfigurationManager.ConnectionStrings["CONN"].ConnectionString;
            int rowsInserted = 0;
            // Creating Sql Connection
            using (SqlConnection conn = new SqlConnection(conStr))
            {
                // Creating insert statement
                string sql = string.Format(@"INSERT INTO [TESTDB].[dbo].[Table_1]([id],[name],[value1])VALUES('"+id+"','"+name+"','"+salary+"')");
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;
                conn.Open();
                rowsInserted = cmd.ExecuteNonQuery();
                conn.Close();
                cmd = null;
            }
 
        }

In ASP.NET code behind create a method to insert and get data through webservice and store in datagridview.

C++
 protected void btnSave_Click(object sender, EventArgs e)
        {
            WebService1 objWebService = new WebService1();
            DataSet dtData = new DataSet();
 
           objWebService.WB_HR_InsertMethod(Convert.ToInt32(txtId.Text.ToString()), txtName.Text.ToString(), Convert.ToInt32(txtSalary.Text.ToString()));
           dtData = objWebService.GetSampleDataDT();
           dgv_show.DataSource = dtData;
           dgv_show.DataBind();
        }

Add WebService in Desktop Application:

Go to reference > add service reference

Now add ok button. The webserver will be include.

Add a new form. Add a new method in code behind of form1.

C++
protected void btnSave_Click(object sender, EventArgs e)
        {
            WebService1 objWebService = new WebService1();
            DataSet dtData = new DataSet();
 
           objWebService.WB_HR_InsertMethod(Convert.ToInt32(txtId.Text.ToString()), txtName.Text.ToString(), Convert.ToInt32(txtSalary.Text.ToString()));
           dtData = objWebService.GetSampleDataDT();
           dgv_show.DataSource = dtData;
           dgv_show.DataBind();
        } 

And also create show method to get data from webservice

C++
 private void Show()
        {  
            WebService1SoapClient objWebService = new WebService1SoapClient();
            DataSet dsReturn = new DataSet();
            dsReturn = objWebService.GetSampleDataDT();
            DataTable dt = new DataTable();
            dt=dsReturn.Tables[0];
            dataGridView1.DataSource = dt;
        }

The same output will show in desktop application

The same database is using for web and desktop application. 

History

20-November-2012- version_1

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralMy vote of 5 Pin
Michael_Haephrati8-Oct-13 10:43
Michael_Haephrati8-Oct-13 10:43 
GeneralMy vote of 4 Pin
Michael Haephrati8-Sep-13 6:19
Michael Haephrati8-Sep-13 6:19 
Very useful

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.