SourceForge has been redesigned. Learn more.
Close

[c5a3d1]: / MainWindow.xaml.cs  Maximize  Restore  History

Download this file

102 lines (83 with data), 3.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
using System;
using System.Diagnostics;
using System.Windows;
using MySql.Data.MySqlClient;
using System.ComponentModel;
namespace YuGiOh_Ultra {
public partial class MainWindow : Window, INotifyPropertyChanged {
public string lblstatuscolor { get; set; }
readonly MySqlConnection connection;
public MainWindow() {
Com.GetImagePath();
connection = new MySqlConnection( Com.connStr );
lblstatuscolor = "#FFFFFFFF";
DataContext = this;
InitializeComponent();
connection.StateChange += connection_StateChange;
try {
connection.Open();
} catch {
MessageBox.Show( "Unable to connect to database server!", "Error!", MessageBoxButton.OK, MessageBoxImage.Error );
lblstatuscolor = "#FFFF0606";
lblstatus.Content = "Failed to connect to the server";
OnPropertyChanged( "lblstatuscolor" );
}
}
void connection_StateChange( object sender, System.Data.StateChangeEventArgs e ) {
if ( connection.State != System.Data.ConnectionState.Open ) return;
btnlogin.IsEnabled = true;
btnregister.IsEnabled = true;
lblstatus.Content = "Server is on!";
lblstatuscolor = "#FF337E17";
OnPropertyChanged( "lblstatuscolor" );
connection.Close();
}
private void btnlogin_Click( object sender, RoutedEventArgs e ) {
if( tbusername.Text.Length < 3 || tbpassword.Password.Length < 3 ) {
MessageBox.Show( "Username and password must contain at least 3 characters", "Warning!", MessageBoxButton.OK, MessageBoxImage.Warning );
return;
}
btnlogin.Content = "Logging in...";
btnlogin.IsEnabled = false;
this.UpdateLayout();
//try {
string query = "SELECT * FROM users WHERE `name` = '" + tbusername.Text + "' AND `password` = '" +
tbpassword.Password + "' LIMIT 1";
var con = new MySqlConnection( Com.connStr );
con.Open();
Debug.Write(con.State);
var cmd = new MySqlCommand(query, con);
var reader = cmd.ExecuteReader();
if( reader.Read() ){
//MessageBox.Show(reader.GetString(0) + ", " + reader.GetString(1));
Com.me = new Player( reader );
reader.Close();
cmd.Dispose();
con.Close();
new wMenu().Show();
this.Close();
} else {
MessageBox.Show("login failed!", "Login error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
btnlogin.IsEnabled = true;
//btnregister.IsEnabled = true;
btnlogin.Content = "Login";
}
reader.Close();
/*}
catch (Exception ex) {
MessageBox.Show( "Critical login error!\n" + ex.Source +": "+ ex.Message, "ERROR", MessageBoxButton.OK, MessageBoxImage.Error );
Application.Current.Shutdown();
}*/
}
private void Window_Closing( object sender, CancelEventArgs e ) {
//connection.Close();
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged( string name ) {
PropertyChangedEventHandler handler = PropertyChanged;
if( handler != null ) {
handler( this, new PropertyChangedEventArgs( name ) );
}
}
}
}