locked
winrt sql data not displaying via mvvm

    Question

  •  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 ObservableCollection<Customer> _customerlist { get; set; } public Customer CustomerToAdd { get; set; } /// <summary> /// Add Customer data to the table /// </summary> public RelayCommand AddCustomerCommand { get; set; } private async void addCustomer() { 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.Insert(new Customer() { Name = CustomerToAdd.Name, Contact = CustomerToAdd.Contact, City = CustomerToAdd.City, }); db.Commit(); db.Dispose(); db.Close(); var line = new MessageDialog("Records Inserted"); await line.ShowAsync(); } } /// <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() { AddCustomerCommand = new RelayCommand(addCustomer); CustomerToAdd = new Customer(); _customerlist = new ObservableCollection<Customer>(); DeleteAllCustomerCommand = new RelayCommand(deleteAll); EditSelectedCustomerCommand = new RelayCommand(editSelectedCustomer); DeleteSelectedCustomerCommand = new RelayCommand(deleteSelectedCustomer); }

    xaml

    <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 CustomerToAdd}" 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 CustomerToAdd}" Text="{Binding City, Mode=TwoWay}" 
                     FontSize="30" Margin="868,181,80,525"/>
            <TextBlock Text="Contact" FontSize="30" Margin="633,296,536,400"/>
            <TextBox DataContext="{Binding CustomerToAdd}" Text="{Binding Contact, Mode=TwoWay}" 
                     FontSize="30" Margin="868,296,80,403"/>
        </Grid>

    i am insert data and its show "Records Inserted" but list box not showing that data

    Wednesday, May 21, 2014 5:27 AM

Answers

  • Hi Sebastian,

    your Connection to the database is the local variable db

    using (var db = new SQLite.SQLiteConnection(dbpath))
    {
    }

    here you  read and write from and to the database.

    The field _customerList is bound to the listview.

     <ListView ItemsSource="{Binding _customerlist}" ...
    
    The data shown in the listview are the items inside this collection. So when you want to add or remove or insert items into the view, you have to add/remove/insert them there. This does not automatically submit hte changes to the database. So you have to add the item to _customerList and db to sync it manually. Same for remove and edit.

    lh

    Thursday, May 22, 2014 3:52 PM

All replies

  • Hi Sebastian,

    from your code it Looks like you are not adding The created customer to the _customerlist. Thats why it does not get shown.


    lh


    Wednesday, May 21, 2014 7:29 AM
  • How to do that Sir
    Wednesday, May 21, 2014 7:46 AM
  • var newCustomer = new Customer()
                   
    {
                       
    Name = CustomerToAdd.Name,
                       
    Contact = CustomerToAdd.Contact,
                       
    City = CustomerToAdd.City,
                       
                   
    }

    db.Insert(newCustomer );

    _customerList.Add(newCustomer)


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Wednesday, May 21, 2014 7:57 AM
  • thank you !

    How to Delete , Edit , Delete All . One item Selected On ListView it can edit or delete that selected item . and i think Delete all
     
    public RelayCommand DeleteAllCustomerCommand { get; set; }
    private async void deleteAll()
    {
    var dbpath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "data.db3");
    using (var db = new SQLite.SQLiteConnection(dbpath))
    {
    db.DeleteAll();
    db.Dispose();
    db.Close();

    }

    var line = new MessageDialog("All Records Deleted");
    await line.ShowAsync();
    }

    This code is right , if it is right delete all data then its showing that data ,data added close the app run the app previous data not showing

    Please Help Me Sir i am Beginner

    Wednesday, May 21, 2014 12:23 PM
  • Full Source Code one drive MVVM
    Wednesday, May 21, 2014 12:25 PM
  • you need to write logic to keep the _customerList in sync with the data in your database, if you add an item to you db, add it to the list. if you remove and item, remove it from the list etc

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Wednesday, May 21, 2014 12:27 PM
  • ok , that means _customerList  this my data base connection link , it used for edit , delete etc operations 
    Wednesday, May 21, 2014 12:40 PM
  • Hi Sebastian,

    your Connection to the database is the local variable db

    using (var db = new SQLite.SQLiteConnection(dbpath))
    {
    }

    here you  read and write from and to the database.

    The field _customerList is bound to the listview.

     <ListView ItemsSource="{Binding _customerlist}" ...
    
    The data shown in the listview are the items inside this collection. So when you want to add or remove or insert items into the view, you have to add/remove/insert them there. This does not automatically submit hte changes to the database. So you have to add the item to _customerList and db to sync it manually. Same for remove and edit.

    lh

    Thursday, May 22, 2014 3:52 PM