Click here to Skip to main content
15,882,698 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
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; }


C#
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);
        }


XML
<Page.DataContext>
        <ViewModels:MainPageViewModel>

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

    <Grid removed="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <ListView ItemsSource="{Binding _customerlist}"
                  HorizontalAlignment="Left" Margin="44,65,0,327" Width="456">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel Width="400" removed="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
Posted

C#
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,
                    
                });
                var newCustomer = new Customer()
                {
                    Name = CustomerToAdd.Name,
                    Contact = CustomerToAdd.Contact,
                    City = CustomerToAdd.City,
                };
                db.Insert(newCustomer);
                _Customerlist.Add(newCustomer);
               
                db.Commit();
                db.Dispose();
                db.Close();
                var line = new MessageDialog("Records Inserted");
                await line.ShowAsync();
            }
        }
 
Share this answer
 
Comments
Naz_Firdouse 4-Jun-14 0:59am    
Wont the record gets inserted in DB twice???
because you are already inserting the record into DB using
db.Insert(new Customer()
{
Name = CustomerToAdd.Name,
Contact = CustomerToAdd.Contact,
City = CustomerToAdd.City,

});


Again you are creating the object and inserting into DB and the same is added to the list
var newCustomer = new Customer()
{
Name = CustomerToAdd.Name,
Contact = CustomerToAdd.Contact,
City = CustomerToAdd.City,
};
db.Insert(newCustomer);

you either need to comment one of the insert into DB...
Try this
C#
Instead of using this
  db.Insert(new Customer()
                {
                    Name = CustomerToAdd.Name,
                    Contact = CustomerToAdd.Contact,
                    City = CustomerToAdd.City,
                    
                });
 
Use the following
 Customer newCustomer = new Customer() {
                    Name = CustomerToAdd.Name,
                    Contact = CustomerToAdd.Contact,
                    City = CustomerToAdd.City,
                    
                };
db.Insert(newCustomer);
customerlist.Add(newCustomer);


The reason is you are inserting the recod in DB but the same is not added to the list which is binded to lsitbox.

so you either need to add the customer object to the list whenever a new object is created or you need to pull the data from DB.
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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