Restful API For Android Using ASP - Net and SQL Server Part 1 Tutorial
Restful API For Android Using ASP - Net and SQL Server Part 1 Tutorial
Restful API for Android and IOS using ASP.NET Web API 2
Thank you
In this tutorial Ill show you how to create a restful API for android (JSON
based API)application using ASP.net and Microsoft SQL Server Database. You
can use MySQL or other databases instead of SQL server. Ill use SQL server
because it is easy to use with ASP.net (Both are Microsoft Products). Ill try to
make this tutorial simple and clear to understand. If you nd any issue please
feel free to ask. You can use the comment section to communicate with me.
Demo Video
[quote_box_center]
This tutorial is quite long; to make it simple I divided it to two parts. In this
tutorial (Part 1) Ill cover the Creation of the API using ASP.net and SQL Server.
InRestful API for Android Part 2Ill cover how to implement this API in
Android Application.
[/quote_box_center]
[/quote_box_center]
Now you need to add those dll les in JsonServices (0.3.4) folder in
downloaded resource to the.NET Web Application. Follow below steps to add
dll le to the project.
1. Right click on JSONWebAPI project in Solution Explorer
2. From the menu click on Add Reference item to open the Reference
Manager
3. Click Browse from the left panel
4. Then click Browse button at bottom of Reference Manager
5. Then go to the location where you extract the downloaded Resource Files
6. Then select all three .dll les and click Add button
7. Then click OK button (Make sure to checked all three le)
Now you need to add a Generic Handler to your Solution Explore. Follow
below steps.
1. Go to PROJECT menu in Visual Studio 2012 then click Add New Item
(Ctrl+Shift+A)
2. From Add New Item window select Web category from left panel
3. From center panel select Generic Handler
4. Keep the default Name Handler1.ashx
5. Then click Add button
Firstly open the SQL Server Management Studio and connect to the server.
Please keep the server name in your mind. Later you need that Server Name to
connect to SQL Server from API. In my case it is AHAMEDISHAK
Press Connect button to connect your Server. Now Ill do the tutorial part by
part.
Now Execute below sql queries to create table. Ill create two tables.
Web.config updates
1
2 ConString" connectionString="Data Source=AHAMEDISHAK; Initial
Next Open the DBConnect.cs class. This class is used to connect with the
database. Update your class according to below code. Dont forget to add
correct header les. Otherwise it will show you errors.
DBConnect.cs
1
2 using System.Configuration;
3 using System.Data.SqlClient;
4
5 namespace JSONWebAPI
6 {
7 ///
8 /// This class is used to connect to sql server database
9 ///
10 public class DBConnect
11 {
12
13 p
private static SqlConnection NewCon;
14 p
private static string conStr = ConfigurationManager.
15
16 p
public static SqlConnection getConnection()
17 {
18 NewCon = new SqlConnection(conStr);
19 r
return NewCon;
20
21 }
22 p
public DBConnect()
23 {
24
25 }
26
27 }
}
IServiceAPI.cs
1
2 namespace JSONWebAPI
3 {
4 p
public interface IServiceAPI
5 {
6
7 }
}
ServiceAPI.cs
1
2 namespace JSONWebAPI
3 {
4 p
public class ServiceAPI : IServiceAPI
5 {
6
7 }
}
[quote_box_center]
Interface is similar to class le. Main dierent is we only declare methods in
interface. We will not implement any method in interface. Interface is extends
by a class. Class which extends the interface should implement all the methods
in interface.
[/quote_box_center]
Now open the IServiceAPI.cs interface le. Add following method
declarations according to below code.
IServiceAPI.cs
1
2 using System.Data;
3
4 namespace JSONWebAPI
5 {
6 ///
7 /// This interface declare the methods need to be implement.
8 ///
9 p
public interface IServiceAPI
10 {
11 v
void CreateNewAccount(s
string firstName, string
12 DataTable GetUserDetails(sstring userName);
13 b
bool UserAuthentication(sstring userName, string
14 DataTable GetDepartmentDetails();
15 }
}
ServiceAPI.cs
1
2 using System;
3 using System.Data;
4 using System.Data.SqlClient;
5
6 namespace JSONWebAPI
7 {
8
9 p
public class ServiceAPI : IServiceAPI
10 {
11 SqlConnection dbConnection;
12
13 p
public ServiceAPI()
14 {
15 dbConnection = DBConnect.getConnection();
16 }
17
18 p
public void CreateNewAccount(s
string firstName,
19 {
if (dbConnection.State.ToString() == "Closed"
20 {
21 dbConnection.Open();
22 }
23
24 s
string query = "INSERT INTO UserDetails VALUES
25
26 SqlCommand command = new SqlCommand(query, dbCo
27 command.ExecuteNonQuery();
28
29 dbConnection.Close();
30 }
31
32 p
public DataTable GetUserDetails(s
string userName)
33 {
34 DataTable userDetailsTable = new DataTable();
35 userDetailsTable.Columns.Add(nnew DataColumn(
36 userDetailsTable.Columns.Add(nnew DataColumn(
37
38 i
if (dbConnection.State.ToString() == "Closed"
39 {
40 dbConnection.Open();
41 }
42
43 s
string query = "SELECT firstName,lastName FROM
44
45 SqlCommand command = new SqlCommand(query, dbCo
46 SqlDataReader reader = command.ExecuteReader();
47
48 i
if (reader.HasRows)
49 {
50 w
while (reader.Read())
51 {
52 userDetailsTable.Rows.Add(reader[
53 }
54 }
55
56 reader.Close();
57 dbConnection.Close();
58 r
return userDetailsTable;
59
60 }
61
62 p
public bool UserAuthentication(s
string userName,
63 {
64 b
bool auth= false;
65
66 i
if (dbConnection.State.ToString() == "Closed"
67 {
68 dbConnection.Open();
69 }
70
71 s
string query = "SELECT id FROM UserDetails WHER
72
73 SqlCommand command = new SqlCommand(query, dbCo
74 SqlDataReader reader = command.ExecuteReader();
75
76 i
if (reader.HasRows)
77 {
78 auth = true;
79 }
80
81 reader.Close();
82 dbConnection.Close();
83
84 r
return auth;
85
86 }
87
88 p
public DataTable GetDepartmentDetails()
89 {
90
91 DataTable deptTable = new DataTable();
92 deptTable.Columns.Add("no", typeof(String));
93 deptTable.Columns.Add("name", typeof(String));
94
95 i
if (dbConnection.State.ToString() == "Closed"
96 {
97 dbConnection.Open();
98 }
99
100 s
string query = "SELECT no,name FROM Dept;"
101 SqlCommand command = new SqlCommand(query, dbCo
102 SqlDataReader reader = command.ExecuteReader();
103
104 i
if (reader.HasRows)
105 {
106 w
while (reader.Read())
107 {
108 deptTable.Rows.Add(reader["no"], reader
109 }
110 }
111
112 reader.Close();
113 dbConnection.Close();
114
115 r
return deptTable;
116
117 }
118 }
119 }
As you can see in the output you can use this restful API with dierent
technologies. But Ill only cover how to use this in android environment. To use
this restful API with Android you need to download the client interface le from
Server. To do that enter ?ANDROID to end of the URL in browser. In my case
URL is http://localhost:7541/Handler1.ashx?ANDROID. Then it will pop up
a Save As dialog box to save the le in your PC. Change the le name to
APIClient.zip.
Share this tutorial with your friends and give a support to my website. Thank
you very much for visiting TuteCentral.
Video Demonstration
Downloads
Download Sample Code
Enter your email address to subscribe to this blog and receive notications of
new posts by email.
Email Address
Subscribe
Share this:
Ahamed Ishak
http://www.tutecnetral.com
I'm a self motivated person who love to learn new stu and share them with others.