简体   繁体   中英

Inserting value in Observable Collection From SQl Db In ViewModel Class

I have Table with two Columns: CourseID and CourseNAme. I want values of these two columns in Observable COllection - FillCourseId. in my view model Class. Please help me iam not able to populate my Observable collection with the values from my dataBase

//     ViewMOdel Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data;


namespace MVVMDemo
{
    public class ViewModel : ViewModelBase, INotifyPropertyChanged
    {
        private Student _student;
        private ObservableCollection<Student> _students;
        private ICommand _SubmitCommand;
        private ObservableCollection<Student> _fillCourseId = new 
        ObservableCollection<Student>();
        static String connectionString = @"Data Source=Ramco- 
         PC\SQLEXPRESS;Initial Catalog=SIT_Ramco_DB;Integrated 
         Security=True;";
        SqlConnection con;
        SqlCommand cmd;
       // SqlDataAdapter adapter;
       // DataSet ds;
        //SqlDataReader reader;

        public ObservableCollection<Student> FillCourseId
        {
            get { return _fillCourseId; }
            set
            {
                _fillCourseId = value;
                OnPropertyChanged("SystemStatusData");
            }
        }

        public Student Student
        {
            get
            {
                return _student;
            }
            set
            {
                _student = value;
                NotifyPropertyChanged("Student");
            }
        }
        public ObservableCollection<Student> Students
        {
            get
            {
                return _students;
            }
            set
            {
                _students = value;
                NotifyPropertyChanged("Students");
            }
        }

        public ICommand SubmitCommand
        {
            get
            {
                if (_SubmitCommand == null)
                {
                    _SubmitCommand = new RelayCommand(param => this.Submit(),
                        null);
                }
                return _SubmitCommand;
            }
        }


        public void GetCourseIdFromDB()
        {
            try
            {
                con = new SqlConnection(connectionString);
                con.Open();
                cmd = new SqlCommand("select * from dev_Course", con);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
               // Student Student = new Student();

                for (int i = 0; i < dt.Rows.Count; ++i)
                    FillCourseId.Add(new Student
                    {
                        CourseID = dt.Rows[i][0].ToString(),
                        CourseName =dt.Rows[i][1].ToString()
                    });

            }
            catch (Exception ex)
            {

            }
        }
        public ViewModel()
        {
            Student = new Student();
            Students = new ObservableCollection<Student>();
            Students.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Students_CollectionChanged);

        }

        void Students_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            NotifyPropertyChanged("Students");
        }

        private void Submit()
        {
            Student.JoiningDate = DateTime.Today.Date;
            Students.Add(Student);
            Student = new Student();
        }
        // Property Changed Event 
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyname)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyname));
        }
    }
}

//Model Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MVVMDemo
{
      public class Student
      {
          public string Name { get; set; }
          public int Age { get; set; }
          public string Course { get; set; }
          public string CourseID { get; set; }

I have Table with two Columns: CourseID and CourseNAme. I want values of these two columns in Observable COllection - FillCourseId. in my view model Class. Please help me iam not able to populate my Observable collection with the values from my dataBase

add this in app.xaml.cs It will work

public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            var mainWindow = new Window();
            var viewModel = new ViewModel();
            mainWindow.DataContext = viewModel;
            mainWindow.Show();
            viewModel.GetCourseIdFromDB();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM