Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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. but when i run the program it does not do anything and when i debug i find that the instruction stops at the instruction level.
C#
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?

What I have tried:

Model / DataConnection.cs
C#
public  class DataConnection  
   {  
        public void OpenConnection()  
        {  
        OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=db.accdb;" +  
              "Persist Security Info=False;");  
  
  
               OleDbCommand cmd = con.CreateCommand();  
               cmd.CommandText = "Insert into table (num,nom)Values(123,'nom1')";  
               cmd.Connection = con;  
               con.Open();  
               cmd.ExecuteNonQuery();  
               MessageBox.Show("Record Submitted", "Congrats");  
                   }  
   }  


ViewModel / MainViewModel.cs
C#
public class MainViewModel : ViewModelBase  
    {  
 private DataConnection _dataconnection;  
 public MainViewModel(DataConnection dataconnexion)  
        {  
            _dataconnection = dataconnexion;  
          _dataconnection.OpenConnection();  
   
 }  
}  

ViewModel / ViewModelLocator.cs
C#
public class ViewModelLocator  
   {  
       static ViewModelLocator()  
       {  
           ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);  
SimpleIoc.Default.Register<DataConnection>();  
SimpleIoc.Default.Register<MainViewModel>();  
       }  
 public MainViewModel Main  
       {  
           get  
           {  
               return ServiceLocator.Current.GetInstance<MainViewModel>();  
           }  
       }  
  
       /// <summary>  
       /// Cleans up all the resources.  
       /// </summary>  
       public static void Cleanup()  
       {  
       }  
   }
Posted
Updated 10-Feb-18 4:13am
Comments
J. Calhoun 10-Feb-18 8:14am    
Are you saying it is stopping at the statement con.open()? If so I believe it would be an issue with your connection string. First I would wrap your OleDBConnection logic in a Try/Catch, then when you debug if something was to break the catch would make it easier to debug, not to mention gracefully handle the error if there was to be one. Also judging by the + in your string, you don't have a space between the ; and Persist, which might be your issue.
abboudi_ammar 10-Feb-18 9:03am    
i use that with try and it show me an error:
"System.InvalidOperationException: Provider 'Microsoft.ACE.OLEDB.12.0' Is Not Inscribed on the Local Computer"

1 solution

It says that because you don't have either an appropriate architecture of Office installed or Access Runtime[^] installed on the machine.

If your app is compiled AnyCPU and running on a 64-bit machine, you'd have to use the 64-bit Office or Runtime.

If your app is compiled AnyCPU and running on a 32-bit machine, you'd have to use the 32-bit Office or Runtime.

If your app is compiled x86, you have to have the 32-bit Office or Runtime installed.

If your app is compiled x64, you have to have the 64-bit Office or Runtime installed.
 
Share this answer
 
Comments
abboudi_ammar 10-Feb-18 10:52am    
my app is compiled AnyCPU and running on a 64-bit machine and i use the 32-bit office 2007 and install 2007 Office System Driver: Data Connectivity Components(https://www.microsoft.com/en-us/download/details.aspx?id=23734) but it doesn't worked.
Dave Kreskowiak 10-Feb-18 13:10pm    
Of course it doesn't work. You're app is compiled AnyCPU, running on a 64-bit version of Windows, means it runs as a 64-bit process.

You have a 32-bit Office and/or runtime installed.

You can NOT mix 32-bit and 64-bit code in the same process. Your 64-bit code is trying to use 32-bit components and that will not work.

Recompile your app x86 only and it'll work. Well, so long as there are any other bugs in the code that is.

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



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