Home / Web devlopment / ASP.NET / How to create “web service” by using c#

How to create “web service” by using c#

How to create “web service” by using c#


lets imagine together we are trying to develop one software working with different platforms (android-IOS-Windows -linux) and you want to connect them all together with one database!!

For example you can use SQL server with windows APP and you can use SQLite with android , in this case you will have different database  so you need to connect all platforms to one SQL server.

What we need to connect them to the same database ?

We have to create Service between them , its like man between the platforms and database , the software (E.x android) asking this man to give him some information from database , the man will take this request and bring the data from database and give them to android application.

So after finishing this article you will be able to create web service.

let’s start:

Open your visual studio then create ASP project :
On the File menu, click New and then click Project. Under Project types click Visual C# Projects, then click ASP.NET Web Service under Templates. Type “name of service” in the Location text box to change the default name.

then open your service and Click to switch to code view in the designer environment to switch to code view.

then write the code below :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Script.Serialization;
namespace WebService_XXX_R
{
//for select query
[System.Web.Script.Services.ScriptService] /// <summary>
/// Summary description for Service_XXX_Ra_SQL_
/// </summary>
/// //for insert query
[WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // 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_XXX_Ra_SQL_ : System.Web.Services.WebService
{
[WebMethod(MessageName = "OpenAccount", Description = "this method to Insert new data")] [System.Xml.Serialization.XmlInclude(typeof(Result_))] public Result_ OpenAccount(String c_user, string c_phone)
{
Result_ cr = new Result_();
try
{
using (SqlConnection opencon = new SqlConnection("Data Source=your server;Initial Catalog=your database;User ID=your username;password=yourpassword;"))
{
string QueryStaff = "insert into "table name" (Name,Phone) values(@Name,@Phone)";
using (SqlCommand QuerySaveStuff = new SqlCommand(QueryStaff))
{
QuerySaveStuff.Connection = opencon;
QuerySaveStuff.Parameters.Add("@Name", SqlDbType.NVarChar, 30).Value = c_user;
QuerySaveStuff.Parameters.Add("@Phone", SqlDbType.NVarChar, 30).Value = c_phone;
opencon.Open();
QuerySaveStuff.ExecuteNonQuery();
opencon.Close();
}
}
cr.ErrorID = 0;
cr.ErrorMessage = "Contact is added";
return cr;
}
catch (Exception ex)
{
cr.ErrorID = 1;
cr.ErrorMessage = ex.Message;
return cr;
}
}
}
}

Then create new class called “Result_” and write the code below :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebService_XXX_R
{
public class Result_
{
public int ErrorID { get; set; }
public string ErrorMessage { get; set; }
}
}

that’s all , you are ready 😀

well , start your project in browser :

 

then write the c_user and c_phone

as you can see contact is added so your data is inserted in your database

check the picuter below 😀

 

Unhandled error Team – with our love