Hello, I need help.
I'm doing a window phone 7 aplication project.
I'm using Microsoft Visual Studio 2010, Window Phone Application and WCF.
I using Microsoft SQL Server database to store my data and I had linked the Window Phone Application and WCF, everything work good.
I'm stuck in the part where I need to sum up all the value in the column "dtServingWeight" in my database and get the total sum.
What I should type in my Service1.svc.cs, IService1.cs and my xaml page?
Hope you can help me, Thanks in advance!
Loading
Felipe RamosPosted Feb 15, 2011, 11:29 AM
Felipe RamosPosted Feb 16, 2011, 7:50 AM
Suthish NairPosted Feb 16, 2011, 3:23 AM
Next time attach your code as attachment.
Christine TanPosted Feb 15, 2011, 10:56 PM
The code is working! Thanks for your answer!!
Christine TanPosted Feb 15, 2011, 11:19 AM
Can you give me a sample example?
Christine TanPosted Feb 15, 2011, 11:16 AM
This is the coding for Service1.svc.cs:
// Diet Tracker
// Inserting Data
public bool AddDetail(string meal, int foodId, string foodname, decimal weight, decimal energy, decimal protein, decimal calories, decimal carbohydrate, decimal cholesterol)
{
try
{
DataClasses2DataContext context = new DataClasses2DataContext();
DietTracker det = new DietTracker();
det.dtCat = meal;
det.dtFoodID = foodId;
det.dtFoodName = foodname;
det.dtServingWeight = weight;
det.dtEnergy = energy;
det.dtProtein = protein;
det.dtCalories = calories;
det.dtCarbohydrate = carbohydrate;
det.dtCholesterol = cholesterol;
context.DietTrackers.InsertOnSubmit(det);
context.SubmitChanges();
return true;
}
catch
{
return false;
}
}
//Delete Database
public bool DeleteDetail(int id)
{
try
{
DataClasses2DataContext context = new DataClasses2DataContext();
var detail = from i in context.DietTrackers where i.dtID == id select i;
context.DietTrackers.DeleteAllOnSubmit(detail);
context.SubmitChanges();
return true;
}
catch
{
return false;
}
}
public List<DietTracker> ShowCatMeal1(string m)
{
DataClasses2DataContext context = new DataClasses2DataContext();
var meal = from i in context.DietTrackers where i.dtCat == m select i;
return meal.ToList();
}
public List<DietTracker> ShowCatMeal2(string m)
{
DataClasses2DataContext context = new DataClasses2DataContext();
var meal = from i in context.DietTrackers where i.dtCat == m select i;
return meal.ToList();
}
public List<DietTracker> ShowCatMeal3(string m)
{
DataClasses2DataContext context = new DataClasses2DataContext();
var meal = from i in context.DietTrackers where i.dtCat == m select i;
return meal.ToList();
}
public List<DietTracker> ShowDT()
{
DataClasses2DataContext context = new DataClasses2DataContext();
var dt = context.DietTrackers;
return dt.ToList();
}
Service1.cs
// Diet Tracker Database
[OperationContract]
bool AddDetail(string meal, int foodId, string foodname, decimal weight, decimal energy, decimal protein, decimal calories, decimal carbohydrate, decimal cholesterol);
[OperationContract]
bool DeleteDetail(int id);
[OperationContract]
List<DietTracker> ShowCatMeal1(string m);
[OperationContract]
List<DietTracker> ShowCatMeal2(string m);
[OperationContract]
List<DietTracker> ShowCatMeal3(string m);
[OperationContract]
List<DietTracker> ShowDT();
xaml page
<phone:PhoneApplicationPage
x:Class="HealthyEatingSystem.HealthyDietTracker2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True">
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
Grid.RowDefinitions>
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">StackPanel>
<Grid x:Name="ContentPanel" Grid.RowSpan="2">
<Grid.Background>
<ImageBrush ImageSource="/HealthyEatingSystem;component/Images/TrackerBackground.jpg" />
Grid.Background>
<TextBlock FontFamily="Comic Sans MS" FontSize="22" Foreground="#FF59250D" Height="36" HorizontalAlignment="Left" Margin="6,123,0,0" Name="textBlock1" Text="Meal categories:" VerticalAlignment="Top" Width="194" />
<TextBlock Height="42" HorizontalAlignment="Left" Margin="6,183,0,0" Name="textBlock2" Text="Food categories:" VerticalAlignment="Top" Foreground="#FF59250D" FontFamily="Comic Sans MS" FontSize="22" Width="186" />
<ComboBox Margin="182,108,0,605" Name="comboBox1" FontFamily="Comic Sans MS" FontSize="24" Height="55" Foreground="#FF5A270E" HorizontalAlignment="Left" Width="292">
<TextBlock Text="Breakfast" Foreground="#FF5A270E" FontFamily="Comic Sans MS" FontSize="26" />
<TextBlock Text="Lunch" Foreground="#FF5A270E" FontFamily="Comic Sans MS" FontSize="26" />
<TextBlock Text="Dinner" Foreground="#FF5A270E" FontFamily="Comic Sans MS" FontSize="26" />
ComboBox>
<ComboBox Margin="182,170,0,543" Name="comboBox2" FontFamily="Comic Sans MS" FontSize="24" Height="55" Foreground="#FF5A270E" HorizontalAlignment="Left" Width="292" SelectionChanged="comboBox2_SelectionChanged">
<TextBlock Text="Beverages" Foreground="#FF5A270E" FontFamily="Comic Sans MS" FontSize="20" />
<TextBlock Text="Bread, Cereals & Dairy Product" Foreground="#FF5A270E" FontFamily="Comic Sans MS" FontSize="20" />
<TextBlock Text="Egg & Vegetable Dishes" Foreground="#FF5A270E" FontFamily="Comic Sans MS" FontSize="20" />
<TextBlock Text="Fast Food" Foreground="#FF5A270E" FontFamily="Comic Sans MS" FontSize="20" />
<TextBlock Text="Fish & Seafood Dishes" Foreground="#FF5A270E" FontFamily="Comic Sans MS" FontSize="20" />
<TextBlock Text="Fruits" Foreground="#FF5A270E" FontFamily="Comic Sans MS" FontSize="20" />
<TextBlock Text="Meat & Poultry Dishes" Foreground="#FF5A270E" FontFamily="Comic Sans MS" FontSize="20" />
<TextBlock Text="Noodles Dishes" Foreground="#FF5A270E" FontFamily="Comic Sans MS" FontSize="20" />
<TextBlock Text="Pastries & Cakes" Foreground="#FF5A270E" FontFamily="Comic Sans MS" FontSize="20" />
<TextBlock Text="Rice Dishes" Foreground="#FF5A270E" FontFamily="Comic Sans MS" FontSize="20" />
<TextBlock Text="Snack & Dessert" Foreground="#FF5A270E" FontFamily="Comic Sans MS" FontSize="20" />
<TextBlock Text="Soup" Foreground="#FF5A270E" FontFamily="Comic Sans MS" FontSize="20" />
ComboBox>
<ListBox Height="523" HorizontalAlignment="Left" Margin="6,239,0,0" Name="listBox1" VerticalAlignment="Top" Width="468" SelectionChanged="listBox1_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FoodName}" Foreground="#FF58260C" FontFamily="Comic Sans MS" FontSize="26" />
StackPanel>
DataTemplate>
ListBox.ItemTemplate>
ListBox>
Grid>
Grid>
phone:PhoneApplicationPage>
xaml.cs page
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 Microsoft.Phone.Controls;
using HealthyEatingSystem.ServiceReference1;
namespace HealthyEatingSystem
{
public partial class HealthyDietTracker2 : PhoneApplicationPage
{
public HealthyDietTracker2()
{
InitializeComponent();
}
private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string select = comboBox2.SelectedIndex.ToString();
if (select == "0")
{
string fn = "Beverages";
Service1Client proxy = new Service1Client();
proxy.FindFoodNameCompleted += new EventHandler<FindFoodNameCompletedEventArgs>(proxy_FindFoodNameCompleted);
proxy.FindFoodNameAsync(fn);
}
else if (select == "1")
{
string fn = "BreadCereals&DairyProduct";
Service1Client proxy = new Service1Client();
proxy.FindFoodNameCompleted += new EventHandler<FindFoodNameCompletedEventArgs>(proxy_FindFoodNameCompleted);
proxy.FindFoodNameAsync(fn);
}
else if (select == "2")
{
string fn = "Egg&Vegetable";
Service1Client proxy = new Service1Client();
proxy.FindFoodNameCompleted += new EventHandler<FindFoodNameCompletedEventArgs>(proxy_FindFoodNameCompleted);
proxy.FindFoodNameAsync(fn);
}
else if (select == "3")
{
string fn = "FastFood";
Service1Client proxy = new Service1Client();
proxy.FindFoodNameCompleted += new EventHandler<FindFoodNameCompletedEventArgs>(proxy_FindFoodNameCompleted);
proxy.FindFoodNameAsync(fn);
}
else if (select == "4")
{
string fn = "Fish&Seafood";
Service1Client proxy = new Service1Client();
proxy.FindFoodNameCompleted += new EventHandler<FindFoodNameCompletedEventArgs>(proxy_FindFoodNameCompleted);
proxy.FindFoodNameAsync(fn);
}
else if (select == "5")
{
string fn = "Fruits";
Service1Client proxy = new Service1Client();
proxy.FindFoodNameCompleted += new EventHandler<FindFoodNameCompletedEventArgs>(proxy_FindFoodNameCompleted);
proxy.FindFoodNameAsync(fn);
}
else if (select == "6")
{
string fn = "Meat&Poultry";
Service1Client proxy = new Service1Client();
proxy.FindFoodNameCompleted += new EventHandler<FindFoodNameCompletedEventArgs>(proxy_FindFoodNameCompleted);
proxy.FindFoodNameAsync(fn);
}
else if (select == "7")
{
string fn = "Noodle";
Service1Client proxy = new Service1Client();
proxy.FindFoodNameCompleted += new EventHandler<FindFoodNameCompletedEventArgs>(proxy_FindFoodNameCompleted);
proxy.FindFoodNameAsync(fn);
}
else if (select == "8")
{
string fn = "Pastries&Cakes";
Service1Client proxy = new Service1Client();
proxy.FindFoodNameCompleted += new EventHandler<FindFoodNameCompletedEventArgs>(proxy_FindFoodNameCompleted);
proxy.FindFoodNameAsync(fn);
}
else if (select == "9")
{
string fn = "Rice";
Service1Client proxy = new Service1Client();
proxy.FindFoodNameCompleted += new EventHandler<FindFoodNameCompletedEventArgs>(proxy_FindFoodNameCompleted);
proxy.FindFoodNameAsync(fn);
}
else if (select == "10")
{
string fn = "Snacks&Desserts";
Service1Client proxy = new Service1Client();
proxy.FindFoodNameCompleted += new EventHandler<FindFoodNameCompletedEventArgs>(proxy_FindFoodNameCompleted);
proxy.FindFoodNameAsync(fn);
}
else if (select == "11")
{
string fn = "Soup";
Service1Client proxy = new Service1Client();
proxy.FindFoodNameCompleted += new EventHandler<FindFoodNameCompletedEventArgs>(proxy_FindFoodNameCompleted);
proxy.FindFoodNameAsync(fn);
}
}
void proxy_FindFoodNameCompleted(object sender, FindFoodNameCompletedEventArgs e)
{
listBox1.ItemsSource = e.Result;
}
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
FoodList click = listBox1.SelectedItem as FoodList;
string foodId = click.FoodID.ToString();
string foodname = click.FoodName.ToString();
string weight = click.FoodServingWeight.ToString();
string energy = click.FoodEnergy.ToString();
string protein = click.FoodProtein.ToString();
string calories = click.FoodCalories.ToString();
string carbohydrate = click.FoodCarbohydrate.ToString();
string cholesterol = click.FoodCholesterol.ToString();
if (comboBox1.SelectedIndex == 0)
{
string meal = "Breakfast";
Service1Client proxy = new Service1Client();
proxy.AddDetailCompleted += new EventHandler<AddDetailCompletedEventArgs>(proxy_AddDetailCompleted);
proxy.AddDetailAsync(meal, Convert.ToInt32(foodId), foodname, Convert.ToDecimal(weight), Convert.ToDecimal(energy), Convert.ToDecimal(protein), Convert.ToDecimal(calories), Convert.ToDecimal(carbohydrate), Convert.ToDecimal(cholesterol));
NavigationService.Navigate(new Uri("/HealthyDietTracker.xaml", UriKind.Relative));
}
else if (comboBox1.SelectedIndex == 1)
{
string meal = "Lunch";
Service1Client proxy = new Service1Client();
proxy.AddDetailCompleted += new EventHandler<AddDetailCompletedEventArgs>(proxy_AddDetailCompleted);
proxy.AddDetailAsync(meal, Convert.ToInt32(foodId), foodname, Convert.ToDecimal(weight), Convert.ToDecimal(energy), Convert.ToDecimal(protein), Convert.ToDecimal(calories), Convert.ToDecimal(carbohydrate), Convert.ToDecimal(cholesterol));
NavigationService.Navigate(new Uri("/HealthyDietTracker.xaml", UriKind.Relative));
}
else if (comboBox1.SelectedIndex == 2)
{
string meal = "Dinner";
Service1Client proxy = new Service1Client();
proxy.AddDetailCompleted += new EventHandler<AddDetailCompletedEventArgs>(proxy_AddDetailCompleted);
proxy.AddDetailAsync(meal, Convert.ToInt32(foodId), foodname, Convert.ToDecimal(weight), Convert.ToDecimal(energy), Convert.ToDecimal(protein), Convert.ToDecimal(calories), Convert.ToDecimal(carbohydrate), Convert.ToDecimal(cholesterol));
NavigationService.Navigate(new Uri("/HealthyDietTracker.xaml", UriKind.Relative));
}
}
private void proxy_AddDetailCompleted(object sender, AddDetailCompletedEventArgs e)
{
bool bb = Convert.ToBoolean(e.Result);
}
}
}
Felipe RamosPosted Feb 15, 2011, 11:03 AM
Jaganathan BantheswaranPosted Feb 15, 2011, 10:31 AM
Can you post the piece of code which you are using. so that we can know how you are doing.