Pages

Saturday, 7 December 2013

Web Services Beginners Tutorials

Building ASP.NET web services


 Create  an ASP.NET Web Application and name it WebServiceBasics.

Right Click on it and add new item "Web Service" name it as you want DemoWebServices.asmx



 Now after clicking that it VS 2012 will create some code for us



lets get into some important things in this:

1. [WebService(Namespace = "http://tempuri.org/")]
 
               http://tempuri.org this will represent our site which is holding these web services

2.  [WebMethod]

                Each of our classes or methods will be decorated with [WebMethod] to specify thats  a web method

Now we get into some coding part

In this i will just create a web service for log-in in asp.net  using entity framework

NOTE: i have created tblPersons and added that to our solution using entity Framework (in case u missed see my first post, in that i explained about doing it)


write a method

[WebMethod]
        public bool Login(string Email, string pass)
        {
            using (var persontable = new Entities())
            {

                var UserDetails = (from persons in persontable.tblPersons
                                   where (persons.Email == Email && persons.Password == pass)
                                   select persons).ToList();
                if (UserDetails.Count() != 0)
                {
                    return true;
                }
                else
                {
                    return false;

                }

            }
        }




Now we are all set run this web service and the output will looks like


       
whoo that's our desired output see its way easy and now click on Login link in that now we'll be redirected to another page




.

Now we have to open another VS 2012 project/Solution and we can use these services (where ever you are )  all you need is just the url: http://localhost:5056/DemoWebServices.asmx

So open another visual studio project(you know what i mean)

And add a new project WebServiceUsers






And add a new form name it as login.aspx and add two text boxes and a login Button you can use toolbox or u can write it on ur own

 <table>
            <tr>
                <td>
                    Email-ID
                </td>
                <td>
                    <asp:TextBox ID="txtEmail" runat="server" ></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Password
                </td>
                <td>
                    <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
                </td>
            </tr>
            <tr >
                
                <td  style="text-align:center" >
                    <asp:Button ID="btnLogin" runat="server" Text="Sign In" OnClick="btnLogin_Click" ></asp:Button>
                </td>
            </tr>
        </table>


Now add Button click code in code-behind file :

1st thing you have to do is add our already created WEB SERVICE to our project.

To do that right click on our project name and select "add a service reference"  and select advanced tab located down the box


and select web reference in the next window



Now add our webservice home page url which is (http://localhost:5056/DemoWebServices.asmx ) in the URL field in the next window and  click go to view our service



whoa  finally there's our webservice and now click Add reference

First create a new instance to our webservice and then add some code to create our login page

localhost1.DemoWebServices objWebService = new localhost1.DemoWebServices();

and now add this code in button click event in our aspx(code behind) page

 protected void btnLogin_Click(object sender, EventArgs e)
        {

            localhost1.DemoWebServices objWebService = new localhost1.DemoWebServices();
            
            string localEmail = txtEmail.Text;
            string localPassword = txtPassword.Text;
            if (objWebService.Login(localEmail, localPassword))
            {
                Response.Redirect("Home1.aspx");
            }
            else
            {
                Response.Write("Enter Correct Details");

            }
            

        }



Now ya'll set to run the program keep it in mind your web service has to be running in order to get use of that service 

Enter an email and password which you have already added in tblPersons  and sign in 



 Hey that worked.


That's all for the day. Hope you enjoyed it



No comments:

Post a Comment