Hi guys im trying to do a Login system in Wpf with Visual Studio
I got the code and DB working fine with Windows Forms in Visual Studio
I have changed the code 3 times and they all work on Windows forms but not with Wpf Forms.
Below is the code i used, do i need to change it for Wpf? or am i missing something, im still learning so any input would be welcome
using System;
using System.Data;
using System.Windows;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace WpfChatApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
MySqlConnection connection = new MySqlConnection("server=localhost;username=;password=;database=login");
public static string Username = "";
public static string Account_ID = "";
public MainWindow()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
if (Properties.Settings.Default.Username != string.Empty)
{
txtUsername.Text = Properties.Settings.Default.Username;
txtPassword.Text = Properties.Settings.Default.Password;
}
}
private void BtnLogin_Click(object sender, RoutedEventArgs e)
{
Username = txtUsername.Text;
Hide();
Main newform = new Main();
newform.Show();
Data db = new Data();
String username = txtUsername.Text;
String password = txtPassword.Text;
DataTable Table = new DataTable();
MySqlDataAdapter adapter = new MySqlDataAdapter();
MySqlCommand command = new MySqlCommand("SELECT * FROM `users` WHERE `user` = @user and `password` = @pass ", db.getConnection());
command.Parameters.Add("@user", MySqlDbType.VarChar).Value = username;
command.Parameters.Add("@pass", MySqlDbType.VarChar).Value = password;
adapter.SelectCommand = command;
adapter.Fill(Table);
if (Table.Rows.Count > 0)
{
System.Windows.MessageBox.Show("Logged In");
}
else
{
if (username.Trim().Equals(""))
{
System.Windows.Forms.MessageBox.Show("Enter your username to Login", "Empty Username", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
}
else if (password.Trim().Equals(""))
{
System.Windows.Forms.MessageBox.Show("Enter your password to Login", "Empty Password", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
}
else
{
System.Windows.Forms.MessageBox.Show("Wrong Username or Passowrd", "Wrong Data", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
}
}
if (Properties.Settings.Default.Username != string.Empty)
{
txtUsername.Text = Properties.Settings.Default.Username;
txtPassword.Text = Properties.Settings.Default.Password;
}
}
private void BtnRegister_Click(object sender, RoutedEventArgs e)
{
}
}
}
Data.cs
using System.Data;
using MySql.Data.MySqlClient;
namespace WpfChatApp
{
class Data
{
MySqlConnection connection = new MySqlConnection("server=localhost;username=root;password=;database=login");
MySqlDataAdapter adapter;
DataTable Table = new DataTable();
public void openConnection()
{
if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
}
public void closeConnection()
{
if (connection.State == System.Data.ConnectionState.Open)
{
connection.Close();
}
}
public MySqlConnection getConnection()
{
return connection;
}
}
}