Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

How to read data from database and put it in model

$
0
0

Hi esperts, i need help here, this is PersonViewModel.cs

namespace OliverCode.MVVM.ViewModel
{
    internal class PersonViewModel
    {
        public PersonModel Person { get; set; }

        private DelegateCommand savePersonCommand;

        public ICommand SavePersonCommand
        {
            get
            {                 
                if(savePersonCommand == null)
                    savePersonCommand = new DelegateCommand(new Action(SaveExecuted), new Func<bool>(SaveCanExecute));

                return savePersonCommand;
            }

        }

        public PersonViewModel()
        {
            //This data will load as the default person from the model attached to the view
            //Person = new PersonModel { FirstName = "John", LastName = "Doe", Age = 999 };
            //I want to read data from database using ado.net, read used datareader and put it in Person = new PersonModel
        }

        public bool SaveCanExecute()
        {
            return Person.Age > 0 && !string.IsNullOrEmpty(Person.FirstName) && !string.IsNullOrEmpty(Person.LastName);
        }

        public void SaveExecuted()
        {
            System.Windows.MessageBox.Show(string.Format("Saved: {0} {1} - ({2})", Person.FirstName, Person.LastName, Person.Age));
        }
    }
}

and this is Person.cs class

namespace OliverCode.MVVM.Model
{
    internal class PersonModel : System.ComponentModel.INotifyPropertyChanged
    {
        private string firstName;
        public string FirstName
        {
            get { return firstName; }
            set
            {
                firstName = value;
                OnPropertyChanged("FirstName");
            }
        }

        private string lastName;
        public string LastName
        {
            get { return lastName; }
            set
            {
                lastName = value;
                OnPropertyChanged("LastName");
            }
        }

        private int age;
        public int Age
        {
            get { return age; }
            set
            {
                age = value;
                OnPropertyChanged("Age");
            }
        }

        #region INotifyPropertyChanged Members

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}

and this in PersonView.xaml

<UserControl x:Class="OliverCode.MVVM.View.PersonView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="Auto" Width="Auto"
    xmlns:local="clr-namespace:OliverCode.MVVM.ViewModel"><StackPanel Orientation="Vertical" Margin="4"><!--Here is where we the view gets a copy to the ViewModel Declaratively--><StackPanel.DataContext><local:PersonViewModel /></StackPanel.DataContext>        <StackPanel Orientation="Vertical" DataContext="{Binding Path=Person, Mode=TwoWay}" Margin="4"><StackPanel Orientation="Horizontal"><Label Content="First Name:" Margin="0,0,4,0"/><TextBox Width="250" Text="{Binding Path=FirstName}"/></StackPanel><StackPanel Orientation="Horizontal" Margin="0,5,0,0"><Label Content="Last Name:" Margin="0,0,4,0"/><TextBox Width="250" Text="{Binding Path=LastName}"/></StackPanel><StackPanel Orientation="Horizontal" Margin="0,5,0,0"><Label Content="Age:" Margin="35,0,4,0"/><TextBox Width="50" MaxLength="3" Text="{Binding Path=Age}"/></StackPanel></StackPanel>        <StackPanel><Button Content="Save" HorizontalAlignment="Right" Width="80" Command="{Binding Path=SavePersonCommand}"/></StackPanel></StackPanel></UserControl>


Instead hardcoded like Person = new PersonModel { FirstName = "John", LastName = "Doe", Age = 999 };

I want to read data from database using ado.net, read used datareader and put it in Person = new PersonModel

Thanks in advance



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>