We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Saturday, March 21, 2015

How to create Login page using web service



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. 

Step1: Create a web service using the following steps.
  • Go to V.S 2010,2012,13  and take a New Project.
  • Select .NET Framework 3.5. and above
  • Take a ASP.NET Web Service Application.
  • Give it a name and click 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;
using System.Data.SqlClient;

 
/// <summary>

/// Summary description for WebService

/// </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 WebService :System.Web.Services.WebService {


 
[WebMethod]

public DataSet login(string uname, string pwd)

{
 
SqlDataAdapter da=new SqlDataAdapter("select * from login where UserName ='"

+ uname +"' and Password ='"+ pwd +"' ",@"Data Source=mithilesh;dataBase=WebService;User Id=sa;Password=12345");
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 VS and take a New Project.
  • Select a ASP.NET Empty Web Application.
  • Give it a name and click ok button.
Step 5: Now add service in our web application. Go to Solution Explorer and right-click at our project. Click at Add Web Reference. A new window will open. Its screen-shot is given below.  




Step 6: Go to Advanced

Click Ok

Step 7: Go to Add Web Reference
Now paste the URL of our service and click at go button.



Click Add Reference

Now go to the design page of our web application and take some user interfaces to accept user name and password and a button.
Write the following code in the .aspx.cs file on buton Click event.


  • using System.Data;
  • protected void btnLogin_Click(object sender, EventArgs e)

    {
     
    localhost.WebService mk = new localhost.WebService();

    DataSet ds = mk.login(txtUname.Text, txtPassword.Text);

    if (ds.Tables[0].Rows.Count > 0)
    {
     

    lblStatus.Text = "<b style='color:green'>Login Sucessfully</b> " + "<b style='color:green'>and Your Name and Password are</b>:-" + ds.Tables[0].Rows[0][0].ToString() +","+ ds.Tables[0].Rows[0][1].ToString();
    txtUname.Text = string.Empty;
    txtPassword.Text = string.Empty;
    }
     

    else
    {
    lblStatus.Text = "<b style='color:red'>Invalid UserName or Password</b>";
    }

    }
    -----------------------------X----------------------------------X-------------------------------------

    No comments: