Click here to Skip to main content
15,843,593 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I am Beginner winrt and sql lite , I am creating app that Customer table
I want how to add data to the Table, edit the data, delete selected data, and delete all
Please help me Any One
Here code maybe wrong please right code provide me
C#
using SQLite;

namespace MVVM.Models
{
    public class Customer
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
        public string Contact { get; set; }
    }
}

/////  Relay Command

/// <summary>
    /// A command whose sole purpose is to relay its functionality 
    /// to other objects by invoking delegates. 
    /// The default return value for the CanExecute method is 'true'.
    /// <see cref="RaiseCanExecuteChanged"/> needs to be called whenever
    /// <see cref="CanExecute"/> is expected to return a different value.
    /// </summary>
    public class RelayCommand : ICommand
    {
        private readonly Action _execute;
        private readonly Func<bool> _canExecute;

        /// <summary>
        /// Raised when RaiseCanExecuteChanged is called.
        /// </summary>
        public event EventHandler CanExecuteChanged;

        /// <summary>
        /// Creates a new command that can always execute.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        public RelayCommand(Action execute)
            : this(execute, null)
        {
        }

        /// <summary>
        /// Creates a new command.
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        /// <param name="canExecute">The execution status logic.</param>
        public RelayCommand(Action execute, Func<bool> canExecute)
        {
            if (execute == null)
                throw new ArgumentNullException("execute");
            _execute = execute;
            _canExecute = canExecute;
        }

        /// <summary>
        /// Determines whether this <see cref="RelayCommand"/> can execute in its current state.
        /// </summary>
        /// <param name="parameter">
        /// Data used by the command. If the command does not require data to be passed, this object can be set to null.
        /// </param>
        /// <returns>true if this command can be executed; otherwise, false.</returns>
        public bool CanExecute(object parameter)
        {
            return _canExecute == null ? true : _canExecute();
        }

        /// <summary>
        /// Executes the <see cref="RelayCommand"/> on the current command target.
        /// </summary>
        /// <param name="parameter">
        /// Data used by the command. If the command does not require data to be passed, this object can be set to null.
        /// </param>
        public void Execute(object parameter)
        {
            _execute();
        }

        /// <summary>
        /// Method used to raise the <see cref="CanExecuteChanged"/> event
        /// to indicate that the return value of the <see cref="CanExecute"/>
        /// method has changed.
        /// </summary>
        public void RaiseCanExecuteChanged()
        {
            var handler = CanExecuteChanged;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }
    }


/// ViewModel Base

class ViewModelBase
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void RaisePropertyChanged(string propertyName)
        {
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

///MainPageViewModel

class MainPageViewModel : ViewModelBase
    {
        #region Properties

        private int id = 0;
        public int Id
        {
            get
            { return id; }

            set
            {
                if (id == value)
                { return; }

                id = value;
                RaisePropertyChanged("Id");
            }
        }

        private string name = string.Empty;
        public string Name
        {
            get
            { return name; }

            set
            {
                if (name == value)
                { return; }

                name = value;
                isDirty = true;
                RaisePropertyChanged("Name");
            }
        }

        private string city = string.Empty;
        public string City
        {
            get
            { return city; }

            set
            {
                if (city == value)
                { return; }

                city = value;
                isDirty = true;
                RaisePropertyChanged("City");
            }
        }

        private string contact = string.Empty;
        public string Contact
        {
            get
            { return contact; }

            set
            {
                if (contact == value)
                { return; }

                contact = value;
                isDirty = true;
                RaisePropertyChanged("Contact");
            }
        }

        private bool isDirty = false;
        public bool IsDirty
        {
            get
            {
                return isDirty;
            }

            set
            {
                isDirty = value;
                RaisePropertyChanged("IsDirty");
            }
        }

        #endregion "Properties"

        public ObservableCollection<Customer> _customerlist { get; set; }
 public Person CustomerToAdd { get; set; }

        /// <summary>
        /// Add Customer data to the table
        /// </summary>
        public RelayCommand AddCusomerCommand { get; set; }
        private void addCustomer()
        {
        }
        /// <summary>
        /// Delete Selected Customer data from the table
        /// </summary>
        public RelayCommand DeleteSelectedCustomerCommand { get; set; }
        private void deleteSelectedCustomer()
        {

        }
        /// <summary>
        /// edit Selected Customer data from the table
        /// </summary>
        public RelayCommand EditSelectedCustomerCommand { get; set; }
        private void editSelectedCustomer()
        {

        }
        /// <summary>
        /// Delete All Customer data from the table
        /// </summary>
        public RelayCommand DeleteAllCustomerCommand { get; set; }
        private void deleteAll()
        {

        }

        public MainPageViewModel()
        {
            AddCusomerCommand = new RelayCommand(addCustomer);
            DeleteAllCustomerCommand = new RelayCommand(deleteAll);
            EditSelectedCustomerCommand = new RelayCommand(editSelectedCustomer);
            DeleteSelectedCustomerCommand = new RelayCommand(deleteSelectedCustomer);
        }


    }
/////////// App.Xaml.Cs
sealed partial class App : Application
    {
        /// <summary>
        /// Initializes the singleton application object.  This is the first line of authored code
        /// executed, and as such is the logical equivalent of main() or WinMain().
        /// </summary>
        public App()
        {
            this.InitializeComponent();
            this.Suspending += OnSuspending;
        }

        /// <summary>
        /// Invoked when the application is launched normally by the end user.  Other entry points
        /// will be used such as when the application is launched to open a specific file.
        /// </summary>
        /// <param name="e">Details about the launch request and process.</param>
        protected override void OnLaunched(LaunchActivatedEventArgs e)
        {

#if DEBUG
            if (System.Diagnostics.Debugger.IsAttached)
            {
                this.DebugSettings.EnableFrameRateCounter = true;
            }
#endif

            Frame rootFrame = Window.Current.Content as Frame;

            // Do not repeat app initialization when the Window already has content,
            // just ensure that the window is active
            if (rootFrame == null)
            {
                // Create a Frame to act as the navigation context and navigate to the first page
                rootFrame = new Frame();
                // Set the default language
                rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0];

                rootFrame.NavigationFailed += OnNavigationFailed;

                if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
                {
                    //TODO: Load state from previously suspended application
                }

                // Place the frame in the current Window
                Window.Current.Content = rootFrame;
            }

            var dbpath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path,"data.db3");
            using (var db = new SQLite.SQLiteConnection(dbpath))
            {
                // Create the tables if they don't exist
                db.CreateTable<Customer>();
                db.Commit();

                db.Dispose();
                db.Close();
            }

            if (rootFrame.Content == null)
            {
                // When the navigation stack isn't restored navigate to the first page,
                // configuring the new page by passing required information as a navigation
                // parameter
                rootFrame.Navigate(typeof(MVVM.Views.Page1), e.Arguments);
            }
            // Ensure the current window is active
            Window.Current.Activate();
        }

        /// <summary>
        /// Invoked when Navigation to a certain page fails
        /// </summary>
        /// <param name="sender">The Frame which failed navigation</param>
        /// <param name="e">Details about the navigation failure</param>
        void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
        }

        /// <summary>
        /// Invoked when application execution is being suspended.  Application state is saved
        /// without knowing whether the application will be terminated or resumed with the contents
        /// of memory still intact.
        /// </summary>
        /// <param name="sender">The source of the suspend request.</param>
        /// <param name="e">Details about the suspend request.</param>
        private void OnSuspending(object sender, SuspendingEventArgs e)
        {
            var deferral = e.SuspendingOperation.GetDeferral();
            //TODO: Save application state and stop any background activity
            deferral.Complete();
        }
    }

HTML
/// xaml Code
<Page.DataContext>
        <ViewModels:MainPageViewModel>

        </ViewModels:MainPageViewModel>
    </Page.DataContext>

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <ListView ItemsSource="{Binding _customerlist}"
                  HorizontalAlignment="Left" Margin="44,65,0,327" Width="456">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Width="400" Background="Chocolate">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Name}" FontSize="30" />
                            <TextBlock Text="," FontSize="30" />
                            <TextBlock Text="{Binding City }" FontSize="30" />
                        </StackPanel>
                        <TextBlock Text="{Binding Contact}" FontSize="30" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

        <Button Command="{Binding  AddCustomerCommand}" 
        		Content="Add person" 
        		FontSize="40" Margin="588,465,0,230"/>
        <Button Command="{Binding EditSelectedCustomerCommand}" 
        		Content="Edit" 
        		FontSize="40" Margin="865,465,0,230"/>
        <Button Command="{Binding DeleteSelectedCustomerCommand}" 
                Content="Delete" 
                FontSize="40" Margin="1037,465,0,230" />
        <Button Command="{Binding DeleteAllCustomerCommand }" 
                Content="Delete All" 
                FontSize="40" Margin="979,619,0,76" />

        <TextBlock Text="Name" FontSize="30" Margin="633,65,598,640" Height="63"/>
        <TextBox DataContext="{Binding PersonToAdd}" Text="{Binding Name, Mode=TwoWay}" 
                 FontSize="30" Margin="868,62,80,640"/>
        <TextBlock Text="City " FontSize="30" Margin="633,181,551,524"/>
        <TextBox DataContext="{Binding PersonToAdd}" Text="{Binding City, Mode=TwoWay}" 
                 FontSize="30" Margin="868,181,80,525"/>
        <TextBlock Text="Age" FontSize="30" Margin="633,296,536,400"/>
        <TextBox DataContext="{Binding PersonToAdd}" Text="{Binding Contact, Mode=TwoWay}" 
                 FontSize="30" Margin="868,296,80,403"/>
    </Grid>
Posted
Updated 20-May-14 6:48am
v2
Comments
[no name] 20-May-14 11:33am    
You are joking right? What does any of this unformatted difficult to read code have to do with any database? Just dumping your code here does not mean that someone is going to write a bunch of code and finish up your project for you.
[no name] 20-May-14 11:40am    
Provide me code that how to add data to the table via relaycommand
[no name] 20-May-14 11:44am    
Uhm.... let me think about your rude demand..... no
[no name] 20-May-14 11:51am    
Please sir , help me I know directly to add data but I don't know how to mvvm used . Please help me sir
CHill60 20-May-14 11:50am    
The problem here is that you have dumped a load of code but most of it is irrelevant and the functions that do the stuff that you mentioned in the text are all empty. Have a go at doing the inserts, edits and deletes then let us know what the problem is

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900