Simon
Simon

Reputation: 504

Filling datagrids from webservices

I'm trying to program a silverlight application (using Silverlight 5.1, .Net 4.5) where I want to have 3 data grids.

The first datagrid is showing a list of symptoms, the second datagrid shall list subsymptoms of the selected symptom in datagrid1 and the third datagrid shall show a kind of "salt" that shall heal the subsymptoms. The datagrids shall receive their data via WCF Webservices. The first datagrid gets all data from table symptoms, the second datagrids gets only subsymptoms for the selected symptom in datagrid1 from the webservice and the "salt" datagrid shall only receive the salts that go with the selected symptom and subsymptom from the webservice.

As soon as I select a symptom, the subsymtpoms appear, as soon as I select subsymptoms, the according salts appear. Everything fine.

The problem I have: When I select a subsymptom and than select again a symptom in Datagrid1 all the datagrids dissapear.

Here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ServiceModel;
using Rebat.SymptomeService;


namespace Rebat
{
    public partial class MainPage : UserControl
    {
        ServiceClient client = new ServiceClient();

        public MainPage()
        {
           InitializeComponent();
           ServiceClient client = new ServiceClient();
           client.SymptomeListCompleted += new EventHandler<SymptomeListCompletedEventArgs>(client_SymptomeListCompleted);
           client.SymptomeListAsync();
        }

        void client_SymptomeListCompleted(object sender, SymptomeListCompletedEventArgs e)
        {
            SymptomeGrid.ItemsSource = e.Result;
        }
        void client_CustomerListCompleted(object sender, Symptome2ListCompletedEventArgs e)
        {
           Symptome2Grid.ItemsSource = e.Result;
        }
        void client_SalzListCompleted(object sender, SalzListCompletedEventArgs e)
        {
            SalzGrid.ItemsSource = e.Result;
        }

        private void SymptomeGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Symptome sympt = SymptomeGrid.SelectedItem as Symptome;
            client.Symptome2ListCompleted +=  new EventHandler<Symptome2ListCompletedEventArgs>(client_CustomerListCompleted);
            client.Symptome2ListAsync(sympt.sId.ToString());
        }

        private void Symptome2Grid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Symptome2 sympt2 = Symptome2Grid.SelectedItem as Symptome2;
            client.SalzListCompleted += new EventHandler<SalzListCompletedEventArgs>(client_SalzListCompleted);
            //if i remove the next line, datagrids don't dissapear anymore... so this might be the problem
            client.SalzListAsync(sympt2.sy1.ToString(), sympt2.sy2.ToString());
        }

    }
}

Here the .xaml code

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Class="Rebat.MainPage"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White" Margin="-13,0,-543,-335" RenderTransformOrigin="0.499,0.573">
        <Grid.RowDefinitions>
            <RowDefinition Height="0*"/>
            <RowDefinition Height="516*"/>
            <RowDefinition Height="119*"/>
        </Grid.RowDefinitions>

        <sdk:DataGrid x:Name="SymptomeGrid" Margin="20,10,0,0" HorizontalAlignment="Left" Width="249" Grid.RowSpan="2" SelectionChanged="SymptomeGrid_SelectionChanged" Height="227" VerticalAlignment="Top"/>
        <sdk:DataGrid x:Name="Symptome2Grid" HorizontalAlignment="Left" Height="227" Margin="293,10,0,0" VerticalAlignment="Top" Width="244" Grid.RowSpan="2" SelectionChanged="Symptome2Grid_SelectionChanged"/>
        <sdk:DataGrid x:Name="SalzGrid" HorizontalAlignment="Left" Height="227" Margin="20,275,0,0" Grid.Row="1" VerticalAlignment="Top" Width="401"/>

    </Grid>
</UserControl>

and here the webservice code:

using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;

namespace Rebat.Web
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service
    {
        [OperationContract]
        public List<Symptome> SymptomeList()
        {
           var custList = new List<Symptome>();
            using (OleDbConnection conn = new OleDbConnection(
               @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\simon\Documents\Visual Studio 2012\Projects\WpfApplication1\WpfApplication1\rebat.mdb"))
            {
                const string sql = @"SELECT Feld1, Feld2 FROM Symptome";
                conn.Open();
                using (OleDbCommand cmd = new OleDbCommand(sql, conn))
                {
                    OleDbDataReader dr = cmd.ExecuteReader(
                        CommandBehavior.CloseConnection);
                    if (dr != null)
                        while (dr.Read())
                        {
                            var cust = new Symptome
                            {
                                sId = dr.GetInt16(0),
                                symptom = dr.GetString(1)
                            };
                            custList.Add(cust);
                        }
                    return custList;
                }
            }
        }

        // Add more operations here and mark them with [OperationContract]
        [OperationContract]
        public List<Symptome2> Symptome2List(string s1)
        {
            var custList = new List<Symptome2>();
            using (OleDbConnection conn = new OleDbConnection(
               @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\simon\Documents\Visual Studio 2012\Projects\WpfApplication1\WpfApplication1\rebat.mdb"))
            {

                string sql = @"SELECT ID, sy1, sy2, symptom2 FROM Symptome2 where sy1="+s1;
                conn.Open();
                using (OleDbCommand cmd = new OleDbCommand(sql, conn))
                {
                    OleDbDataReader dr = cmd.ExecuteReader(
                        CommandBehavior.CloseConnection);
                    if (dr != null)
                        while (dr.Read())
                        {
                            var cust = new Symptome2
                            {
                                ID = dr.GetInt32(0),
                                sy1 = dr.GetInt16(1),
                                sy2 = dr.GetInt16(2),
                                symptom2 = dr.GetString(3)
                            };
                            custList.Add(cust);
                        }
                    return custList;
                }
            }
        }
         // Add more operations here and mark them with [OperationContract]
        [OperationContract]
        public List<Salz> SalzList(string sym1, string sym2)
        {
            var salzList = new List<Salz>();
            using (OleDbConnection conn = new OleDbConnection(
               @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\simon\Documents\Visual Studio 2012\Projects\WpfApplication1\WpfApplication1\rebat.mdb"))
            {

                string sql = @"SELECT sId, salz FROM Salze where sId in (SELECT saId from Rezept where sy1Id= " + sym1 + " and sy2Id=" +sym2 + ")";

                conn.Open();
                using (OleDbCommand cmd = new OleDbCommand(sql, conn))
                {
                    OleDbDataReader dr = cmd.ExecuteReader(
                        CommandBehavior.CloseConnection);
                    if (dr != null)
                        while (dr.Read())
                        {
                            var cust = new Salz
                            {
                                SID = dr.GetInt32(0),
                                Name = dr.GetString(1)
                            };
                            salzList.Add(cust);
                        }
                    return salzList;
                }
            }
        }
    }
}

and finally the helping classes

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Rebat.Web
{
    public class Symptome
    {
        public int sId { get; set; }
        public string symptom { get; set; }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Rebat.Web
{
    public class Symptome2
    {
        public int ID { get; set; }
        public int sy1 { get; set; }
        public int sy2 { get; set; }
        public string symptom2 { get; set; }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Rebat.Web
{
    public class Salz
    {
        public int SID { get; set; }
        public string Name { get; set; }
    }
}

Upvotes: 1

Views: 216

Answers (1)

Sajeetharan
Sajeetharan

Reputation: 222682

Here is the complete source code, try this

Code behind.cs :

  private void SymptomeGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Symptome2Grid.ItemsSource = null;
        SalzGrid.ItemsSource = null;
        svcSymptom.Symptome sympt = SymptomeGrid.SelectedItem as svcSymptom.Symptome;
        if (sympt != null)
        {
            svcSymptom.Service1Client SymptomsClient = new svcSymptom.Service1Client();

                SymptomsClient.Symptome2ListCompleted += (a, ae) =>
                {
                    if (ae.Result.Count != 0)
                    {
                        Symptome2Grid.ItemsSource = ae.Result.ToList();
                    }
                };
                SymptomsClient.Symptome2ListAsync(sympt.symptom.ToString());

        }

    }

    private void Symptome2Grid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        SalzGrid.ItemsSource = null;
        svcSymptom.Symptome2 sympt2 = Symptome2Grid.SelectedItem as svcSymptom.Symptome2;
        if (sympt2 != null)
        {
            svcSymptom.Service1Client SymptomsClient = new svcSymptom.Service1Client();
            SymptomsClient.SalzListCompleted += (a, ae) =>
               {
                   if (ae.Result.Count != 0)
                   {
                       SalzGrid.ItemsSource = ae.Result.ToList();
                   }
               };
            SymptomsClient.SalzListAsync(sympt2.sy1.ToString(), sympt2.sy2.ToString());

        }
    }     

Upvotes: 1

Related Questions