Reputation: 630
Okay so the totourials I've been following to make my application have been based on the winforms side of c# where as my app has been based on wpf. However even though my app works fine, I'm told that there's a better way of binding my textboxes using MVVM. I however have been having a difficultt time finding MySQL related totourials that are wpf based that cover MVVM data-binding.
This is my current way of doing things, and so far it works. Apparently it could be better...
string sqlcon = "datasource = localhost; port = 3306; username = root; password = root";
string query = "Select * from users.login where tag = '" + this.tag.Text + "' ;";
new MySqlConnection(sqlcon);
MySqlCommand cmd = new MySqlCommand(query, con);
MySqlDataReader rdr;
try
{
con.Open();
rdr = cmd.ExecuteReader();
MessageBox.Show("Saved");
while (rdr.Read())
{
//Declarations
string susername = GetString(rdr, "username");
string spassword = GetString(rdr, "password");
string ssecurity = GetString(rdr, "question");
string sanswer = GetString(rdr, "answer");
//Binding strings to textboxes
username.Text = susername;
password.Text = spassword;
question.Text = ssecurity;
answer.Text = sanswer;
}
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Maybe I'm not looking in the right places but I couldn't find anything specific on MySQL data-binding that had anything to do with xaml. What would be the best way to do this in wpf? Even just an example will do fine. Just so I can get an idea at least.
Upvotes: 1
Views: 1361
Reputation: 956
An easier way would be to use entity framework:
http://msdn.microsoft.com/en-us/data/ef.aspx
MVVM Entity Framework tut
http://msdn.microsoft.com/en-us/data/jj574514.aspx
Try to get some knowledge of it first if u actually want to use it.
I first did it the same way u did, but now I use this, I find it way easier.
Also when using MVVM u can just make a project for the entity framework object, and link to your modules.
If u want to do it like you do it now, you can do it like this:
some tuts:
http://www.codeproject.com/Articles/165368/WPF-MVVM-Quick-Start-Tutorial
http://www.codeproject.com/Articles/124160/Using-WPF-MVVM-for-Database-Access
VIEW
add an item source to your object,
ItemsSource="{Binding}"
and try to get stuff from the database loaded in there, that's already a big step.
Upvotes: 1