TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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
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
public
class
MainViewModel : ViewModelBase
{
private
DataConnection _dataconnection;
public
MainViewModel(DataConnection dataconnexion)
{
_dataconnection = dataconnexion;
_dataconnection.OpenConnection();
}
}
ViewModel / ViewModelLocator.cs
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()
{
}
}
when i run the program it does not do anything and when i debug i find that the instruction stops at the instruction level
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?
Reply
Answers (
1
)
Want Source code of Visual Studio Database back and Restore
Want to auto populate data from one combobox to another.