abdelwaheb ammar

abdelwaheb ammar

  • 1.3k
  • 457
  • 124.8k

Connection to the database in WPF with MVVM design pattern

Feb 9 2018 6:17 PM
Good evening,
I am a beginner at MVVM design patter and WPF I am trying to make a small example of connection to the MS Access database using the WPF and MVVM Light Toolkit patter design. here is the code I insert for the connection:

Model / DataConnection.cs
  1. public  class DataConnection  
  2.    {  
  3.         public void OpenConnection()  
  4.         {  
  5.         OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=db.accdb;" +  
  6.               "Persist Security Info=False;");  
  7.   
  8.   
  9.                OleDbCommand cmd = con.CreateCommand();  
  10.                cmd.CommandText = "Insert into table (num,nom)Values(123,'nom1')";  
  11.                cmd.Connection = con;  
  12.                con.Open();  
  13.                cmd.ExecuteNonQuery();  
  14.                MessageBox.Show("Record Submitted""Congrats");  
  15.                    }  
  16.    }  
 
ViewModel / MainViewModel.cs
  1.  public class MainViewModel : ViewModelBase  
  2.     {  
  3.  private DataConnection _dataconnection;  
  4.  public MainViewModel(DataConnection dataconnexion)  
  5.         {  
  6.             _dataconnection = dataconnexion;  
  7.           _dataconnection.OpenConnection();  
  8.    
  9.  }  
  10. }  
 
 ViewModel / ViewModelLocator.cs
  1. public class ViewModelLocator  
  2.    {  
  3.        static ViewModelLocator()  
  4.        {  
  5.            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);  
  6. SimpleIoc.Default.Register<DataConnection>();  
  7. SimpleIoc.Default.Register<MainViewModel>();  
  8.        }  
  9.  public MainViewModel Main  
  10.        {  
  11.            get  
  12.            {  
  13.                return ServiceLocator.Current.GetInstance<MainViewModel>();  
  14.            }  
  15.        }  
  16.   
  17.        /// <summary>  
  18.        /// Cleans up all the resources.  
  19.        /// </summary>  
  20.        public static void Cleanup()  
  21.        {  
  22.        }  
  23.    }  
 
when i run the program it does not do anything and when i debug i find that the instruction stops at the instruction level
  1. con.Open();  
model / DataConnection.cs file and then it goes to MainWindow.xaml.cs.
I do not find why he does not continue the execution of all the instructions? 
 

Answers (1)