From the past few days, I have been working on an Android code to call a local web service. I am using ksoap libraries for Android to call my SOAP web service created in .NET. My web service is an .asmx web service.
First I will teach you how to create the web service:
Tools required : Visual Studio 2008 having .NET framework 3.5 or higher, SQL Server Management Studio 2008
Step 1: Create a new project in Visual Studio by selecting the ASP.NET web service application category. I will be using C# for writing the code.
Step 2: Give a name to your application which will be your namespace. I have given the name as "LoginDetails". After you are done creating the project , we now start with writing our method to connect to SQL Server database.
Important note: In my code "UserName" and "Password" are my column names in the database that I have created. You can give any name as you wish. Also I have used "sa" authentication mode for SQL server. Please use "sa" authentication and not default Windows Authentication.
You can download the entire code from here
Part 2 which involves creating the Android app for calling this web service will be discussed in my next post.
First I will teach you how to create the web service:
Tools required : Visual Studio 2008 having .NET framework 3.5 or higher, SQL Server Management Studio 2008
Step 1: Create a new project in Visual Studio by selecting the ASP.NET web service application category. I will be using C# for writing the code.
Step 2: Give a name to your application which will be your namespace. I have given the name as "LoginDetails". After you are done creating the project , we now start with writing our method to connect to SQL Server database.
Here is how I have written the code for the method :
This code will return a message "success" or "bad username or password" depending on whether you have entered the right credentials (username and password) . You can now run the application by hitting F5 on the keyboard.
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public String GetLoginDetails(string UserName, string Password)
{
try
{
using (SqlConnection myConnection = new SqlConnection(@"Data Source= .\SQLEXPRESS;Initial Catalog=yourdatabasename;User ID=sa;Password=yourpassword"))
{
myConnection.Open();
using (SqlCommand myCommand = new SqlCommand())
{
myCommand.Connection = myConnection;
myCommand.CommandText = "SELECT COUNT(*) FROM Login WHERE UserName = @UserName AND Password = @Password";
myCommand.Parameters.Add("@UserName", SqlDbType.VarChar).Value = UserName; myCommand.Parameters.Add("@Password", SqlDbType.VarChar).Value = Password;
return (int)myCommand.ExecuteScalar() == 1 ? "success" : "bad username or password";
}
}
}catch (Exception ex)
{
Console.WriteLine(ex.Message); return "an error occurred.";
}
}
}
This code will return a message "success" or "bad username or password" depending on whether you have entered the right credentials (username and password) . You can now run the application by hitting F5 on the keyboard.
Important note: In my code "UserName" and "Password" are my column names in the database that I have created. You can give any name as you wish. Also I have used "sa" authentication mode for SQL server. Please use "sa" authentication and not default Windows Authentication.
You can download the entire code from here
Part 2 which involves creating the Android app for calling this web service will be discussed in my next post.
Enjoy !! Feel free to comment ..Any small doubts you have please share ..
Thanks !!
Parth
Thanks !!
Parth