Wednesday, March 27, 2013

Connecting Windows 8 Store Application with SQL Server using WCF - Part 2

This post is all about creating the application


First we need to add the service reference

Give the Namespace as DataProvider


Now we'll go to the coding Here i have added the coding for the buttons Both button click events will have to be async.


   private async void btnGetStudent_Click(object sender, RoutedEventArgs e)
    {
        //Create a client object to access the service
        DataProvider.DataProviderClient serviceClient = new DataProvider.DataProviderClient();

        //Get the student id and convert it to integer
        int ID = Convert.ToInt32(txtStudentID.Text);

        //Get the student via the service
        DataProvider.Student student = await serviceClient.getStudentAsync(ID);

        //The service will return null if the student is not available
        //If student is not found show a message
        if (student==null)
        {
            Windows.UI.Popups.MessageDialog message = new Windows.UI.Popups.MessageDialog("Student not found");
            message.ShowAsync();
            return;
        }

        //Set the values to the appropriate labels
        lblFirstName.Text = student.FirstName;
        lblLastName.Text = student.LastName;
    }

    private async void btnGetAllStudents_Click(object sender, RoutedEventArgs e)
    {
        //Create a client object to access the service
        DataProvider.DataProviderClient serviceClient = new DataProvider.DataProviderClient();

        //Get the student objects from the service
        ObservableCollection students = await serviceClient.getStudentsAsync();

        //Add the objects to the list view
        foreach (DataProvider.Student student in students)
        {
            lstStudents.Items.Add(student.FirstName+" "+student.LastName);
        }
    }

Download Code

Please Leave a comment about the post.

No comments:

Post a Comment