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
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; }
}
}
public class RelayCommand : ICommand
{
private readonly Action _execute;
private readonly Func<bool> _canExecute;
public event EventHandler CanExecuteChanged;
public RelayCommand(Action execute)
: this(execute, null)
{
}
public RelayCommand(Action execute, Func<bool> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute();
}
public void Execute(object parameter)
{
_execute();
}
public void RaiseCanExecuteChanged()
{
var handler = CanExecuteChanged;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
}
class ViewModelBase
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void RaisePropertyChanged(string propertyName)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
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; }
public RelayCommand AddCusomerCommand { get; set; }
private void addCustomer()
{
}
public RelayCommand DeleteSelectedCustomerCommand { get; set; }
private void deleteSelectedCustomer()
{
}
public RelayCommand EditSelectedCustomerCommand { get; set; }
private void editSelectedCustomer()
{
}
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);
}
}
sealed partial class App : Application
{
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
}
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;
if (rootFrame == null)
{
rootFrame = new Frame();
rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0];
rootFrame.NavigationFailed += OnNavigationFailed;
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
}
Window.Current.Content = rootFrame;
}
var dbpath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path,"data.db3");
using (var db = new SQLite.SQLiteConnection(dbpath))
{
db.CreateTable<Customer>();
db.Commit();
db.Dispose();
db.Close();
}
if (rootFrame.Content == null)
{
rootFrame.Navigate(typeof(MVVM.Views.Page1), e.Arguments);
}
Window.Current.Activate();
}
void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
{
throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
}
private void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
deferral.Complete();
}
}
/// 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>