Connection to the database in WPF with MVVM design pattern

ammar.dev

Member
Joined
Sep 21, 2015
Messages
8
Programming Experience
Beginner
[FONT=&quot]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: [/FONT]

[FONT=&quot]Model / DataConnection.cs
[/FONT][/COLOR]

[LIST=|INDENT=1]
[*][COLOR=black][COLOR=#006699][B]public[/B][/COLOR]  [COLOR=#006699][B]class[/B][/COLOR] DataConnection  [/COLOR]
[*][COLOR=black]   {  [/COLOR]
[*][COLOR=black]        [COLOR=#006699][B]public[/B][/COLOR] [COLOR=#006699][B]void[/B][/COLOR] OpenConnection()  [/COLOR]
[*][COLOR=black]        {  [/COLOR]
[*][COLOR=black]        OleDbConnection con = [COLOR=#006699][B]new[/B][/COLOR] OleDbConnection([COLOR=blue]"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=db.accdb;"[/COLOR] +  [/COLOR]
[*][COLOR=black]              [COLOR=blue]"Persist Security Info=False;"[/COLOR]);  [/COLOR]
[*][COLOR=black]  [/COLOR]
[*][COLOR=black]  [/COLOR]
[*][COLOR=black]               OleDbCommand cmd = con.CreateCommand();  [/COLOR]
[*][COLOR=black]               cmd.CommandText = [COLOR=blue]"Insert into table (num,nom)Values(123,'nom1')"[/COLOR];  [/COLOR]
[*][COLOR=black]               cmd.Connection = con;  [/COLOR]
[*][COLOR=black]               con.Open();  [/COLOR]
[*][COLOR=black]               cmd.ExecuteNonQuery();  [/COLOR]
[*][COLOR=black]               MessageBox.Show([COLOR=blue]"Record Submitted"[/COLOR], [COLOR=blue]"Congrats"[/COLOR]);  [/COLOR]
[*][COLOR=black]                   }  [/COLOR]
[*][COLOR=black]   }  [/COLOR]
[/LIST]
[COLOR=#2F2E2E][FONT=&quot]


ViewModel / MainViewModel.cs
[/FONT][/COLOR]

[LIST=|INDENT=1]
[*][COLOR=black][COLOR=#006699][B]public[/B][/COLOR] [COLOR=#006699][B]class[/B][/COLOR] MainViewModel : ViewModelBase  [/COLOR]
[*][COLOR=black]    {  [/COLOR]
[*][COLOR=black] [COLOR=#006699][B]private[/B][/COLOR] DataConnection _dataconnection;  [/COLOR]
[*][COLOR=black] [COLOR=#006699][B]public[/B][/COLOR] MainViewModel(DataConnection dataconnexion)  [/COLOR]
[*][COLOR=black]        {  [/COLOR]
[*][COLOR=black]            _dataconnection = dataconnexion;  [/COLOR]
[*][COLOR=black]          _dataconnection.OpenConnection();  [/COLOR]
[*][COLOR=black]   [/COLOR]
[*][COLOR=black] }  [/COLOR]
[*][COLOR=black]}  [/COLOR]
[/LIST]
[COLOR=#2F2E2E][FONT=&quot]


ViewModel / ViewModelLocator.cs
[/FONT][/COLOR]

[LIST=|INDENT=1]
[*][COLOR=black][COLOR=#006699][B]public[/B][/COLOR] [COLOR=#006699][B]class[/B][/COLOR] ViewModelLocator  [/COLOR]
[*][COLOR=black]   {  [/COLOR]
[*][COLOR=black]       [COLOR=#006699][B]static[/B][/COLOR] ViewModelLocator()  [/COLOR]
[*][COLOR=black]       {  [/COLOR]
[*][COLOR=black]           ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);  [/COLOR]
[*][COLOR=black]SimpleIoc.Default.Register<DataConnection>();  [/COLOR]
[*][COLOR=black]SimpleIoc.Default.Register<MainViewModel>();  [/COLOR]
[*][COLOR=black]       }  [/COLOR]
[*][COLOR=black] [COLOR=#006699][B]public[/B][/COLOR] MainViewModel Main  [/COLOR]
[*][COLOR=black]       {  [/COLOR]
[*][COLOR=black]           [COLOR=#006699][B]get[/B][/COLOR]  [/COLOR]
[*][COLOR=black]           {  [/COLOR]
[*][COLOR=black]               [COLOR=#006699][B]return[/B][/COLOR] ServiceLocator.Current.GetInstance<MainViewModel>();  [/COLOR]
[*][COLOR=black]           }  [/COLOR]
[*][COLOR=black]       }  [/COLOR]
[*][COLOR=black]  [/COLOR]
[*][COLOR=black]       [COLOR=#008200]/// <summary>[/COLOR]  [/COLOR]
[*][COLOR=black]       [COLOR=#008200]/// Cleans up all the resources.[/COLOR]  [/COLOR]
[*][COLOR=black]       [COLOR=#008200]/// </summary>[/COLOR]  [/COLOR]
[*][COLOR=black]       [COLOR=#006699][B]public[/B][/COLOR] [COLOR=#006699][B]static[/B][/COLOR] [COLOR=#006699][B]void[/B][/COLOR] Cleanup()  [/COLOR]
[*][COLOR=black]       {  [/COLOR]
[*][COLOR=black]       }  [/COLOR]
[*][COLOR=black]   }[/COLOR]
[/LIST]
[COLOR=#2F2E2E][FONT=&quot]

when i run the program it does not do anything and when i debug i find that the instruction stops at the instruction level

[COLOR=#000000][FONT=Consolas]con.Open(); [/FONT][/COLOR]

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? [/FONT]
 

Latest posts

Back
Top Bottom