Creating a Web Service for a Login Page #mesameergaikwad

 

Creating a Web Service for a Login Page

Here I am creating a web service for a login check and consuming it in a web application. Follow the given steps to do it.

check out my blog post

Step1: Create a web service using the following steps.

  • Go to Visual Studio 2010 and take a New Project.
  • Select .NET Framework 3.5.
  • Take an ASP.NET Web Service Application.
  • Give it a name and click the ok button.

check out my blog post

Go to Visual Studio 2010 and take a New Project.

Select .NET Framework 3.5.

Take an ASP.NET Web Service Application.

Give it a name and click the ok button.

Step 2: Write the following code on the .asmx.cs page.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;

namespace LoginPage
{
 /// <summary>
 /// Summary description for Service1
 /// </summary>
 [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 Service1 : System.Web.Services.WebService
 {
 [WebMethod]
 public DataSet login(string uname, string pwd)
 {
 SqlDataAdapter da = new SqlDataAdapter(“select * from tbl_data where username =’” + uname + “‘ and word=’” + pwd + “‘ “, 
 @”Data Source=.\SQLEXPRESS;Initial Catalog=sameer;Integrated Security=True”);
 DataSet ds = new DataSet();
 da.Fill(ds);
 return ds;
 }
 }
}

Step 3: Run this application.

Output:

Step 4: Now create a web application to consume the service. For doing this, follow the given steps.

  • Go to Visual Studio 2010 and take a New Project.
  • Select an ASP.NET Empty Web Application.
  • Give it a name and click the ok button.

Step 5: Now add service to your web application. Go to Solution Explorer and right-click on your project. Click at Add Web Reference. A new window will open. Its screen-shot is given below.

  • Now paste the URL of your service and click on the Go button.
  • Click on Add reference. Now service has been added to your project.
  • Now go to the design page of your web application and take some user interfaces to accept the user name and password and a button.

Write the following code in the .aspx.cs file on the button Click event.

protected void btnlogin_Click(object sender, EventArgs e)
{
 localhost.Service1 obj=new localhost.Service1();
 DataSet ds = obj.login(txtname.Text, txtpass.Text);
 if (ds.Tables[0].Rows.Count > 0)
 {
 Label1.Text = “Hi ,” + ds.Tables[0].Rows[0][1].ToString();
 }
 else
 {
 Label1.Text = “Invalid UserName or Password.”;
 }
}

Step 6: Run the web application.

Output:

Write the user name and password in TextBoxes and click the ok button. In this example, the Database name is EMP which has a table Login. There is only one record in the Login table — ABC is user_name and XYZ is password. Do right input for user name and password and click the ok button.

Output:

Now do wrong input for user name and password and click the ok button.

Output:

#mesameergaikwad

Post a Comment

0 Comments