Click here to Skip to main content
15,880,427 members
Articles / Mobile Apps / Windows Mobile

Restaurant and Waiter project! (WPF - Windows Mobile)

Rate me:
Please Sign up or sign in to vote.
4.90/5 (77 votes)
22 Jan 2012Ms-PL2 min read 241.8K   89.2K   182  
Restaurant and Waiter project! (WPF - Windows Mobile)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data;
using System.IO;
using System.Data.SqlClient;

namespace Restaurant
{
    /// <summary>
    /// Interaction logic for SettingsWindow.xaml
    /// </summary>
    public partial class SettingsWindow : Window
    {
        // These are global object which will be use to communicate with the 
        // database whithin this class
        SqlDatabase objSqlDatabase = new SqlDatabase(GlobalClass._ConStr);
        DatabaseClass objDatabaseClass = new DatabaseClass(GlobalClass._ConStr);
        string logoPath = "";

        public SettingsWindow()
        {
            InitializeComponent();
        }

        private void CloseCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            DataTable dt=GlobalClass.GetRestaurantInfo();
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    txtRestaurantName.Text = dt.Rows[0]["RestaurantName"].ToString();
                    txtPhoneNumber.Text = dt.Rows[0]["PhoneNumber"].ToString();
                    txtWebsite.Text = dt.Rows[0]["WebsiteURL"].ToString();
                    txtEmail.Text = dt.Rows[0]["Email"].ToString();
                    txtAddress.Text = dt.Rows[0]["Address"].ToString();

                    try
                    {
                        if (dt.Rows[0]["Logo"] != DBNull.Value)
                        {
                            byte[] arr = (byte[])dt.Rows[0]["Logo"];
                            MemoryStream ms = new MemoryStream(arr);
                            BitmapImage bitmap = new BitmapImage();
                            bitmap.BeginInit();
                            bitmap.StreamSource = ms;
                            bitmap.EndInit();
                            imgLogo.Source = bitmap;
                        }
                    }
                    catch { }
                }
            }
        }

        private void btnBrowseLogo_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
            ofd.Filter = "Image files|*.jpg;*.png;*.gif";
            System.Windows.Forms.DialogResult dgr = ofd.ShowDialog();
            if (dgr == System.Windows.Forms.DialogResult.OK)
            {
                BitmapImage bitmap = new BitmapImage(new Uri(ofd.FileName));
                if (bitmap.Width <= 80 && bitmap.Height <= 80)
                {
                    imgLogo.Source = bitmap;
                    logoPath = ofd.FileName;
                }
                else
                {
                    MessageBox.Show("Logo image should have the size of 80*80 or less!","", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }

        private void btnSaveChanges_Click(object sender, RoutedEventArgs e)
        {
            Cursor = Cursors.Wait;
            if (txtRestaurantName.Text != "" && imgLogo.Source != null)
            {
                string spName = "Update_Settings";
                if (objDatabaseClass.CheckConnection())
                {
                    object[] spParams = new object[6];
                    spParams[0] = txtRestaurantName.Text;
                    spParams[1] = txtAddress.Text;
                    spParams[2] = txtPhoneNumber.Text;
                    spParams[3] = txtWebsite.Text;
                    spParams[4] = txtEmail.Text;
                    if (logoPath != "")
                    {
                        FileStream fsLogo = new FileStream(logoPath, FileMode.Open, FileAccess.Read);
                        FileInfo objFileInfoLogo = new FileInfo(logoPath);
                        BinaryReader brLogo = new BinaryReader(fsLogo);
                        long lengthLogo = objFileInfoLogo.Length;
                        byte[] arrImgLogo = new byte[Convert.ToInt32(lengthLogo)];
                        arrImgLogo = brLogo.ReadBytes(Convert.ToInt32(lengthLogo));
                        fsLogo.Flush();
                        brLogo.Close();
                        fsLogo.Close();
                        spParams[5] = arrImgLogo;
                    }
                    else
                    {
                        spParams[5] = DBNull.Value;
                    }
                    try
                    {
                        int result = objSqlDatabase.ExecuteNonQuery(spName, spParams);
                        if (result > 0)
                        {
                            MessageBox.Show("Settings Saved Successfully!");
                            this.DialogResult = false;
                        }
                    }
                    catch (SqlException)
                    {
                        MessageBox.Show(ErrorMessages.Default.UnhandledException, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
                else
                {
                    MessageBox.Show(ErrorMessages.Default.NoConnection, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else
            {
                MessageBox.Show(string.Format(ErrorMessages.Default.FieldEmpty,"Restaurant Name")+"\n"+
                    string.Format(ErrorMessages.Default.FieldEmpty, "Logo"),"", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            Cursor = Cursors.Arrow;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions