I'm trying to create a login and register forum and store all of the usernames and passwords in a MySQL database. I found a free MySQL hosting site and instead of just having your databases listed I have the user name listed and then that users tables. I'm not really sure if doing it through a user is causing the problem or not. Here is the error I get.
Project Royal.vshost.exe Error: 0 : Unable to connect to any of the specified MySQL hosts. Exception thrown: 'MySql.Data.MySqlClient.MySqlException' in MySql.Data.dll
And also here is my code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Project_Royal
{
public partial class Login : Form
{
private MySqlConnection conn;
private string server;
private string database;
private string uid;
private string password;
public Login()
{
server = "82.197.130.215";
database = "2190559_users";
uid = "2190559_users";
password = "";
string connString;
connString = $"SERVER={server};DATABASE={database};UID={uid};PASSWORD={password};";
conn = new MySqlConnection(connString);
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
string user = textBox1.Text;
string pass = textBox2.Text;
if (Register(user, pass))
{
MessageBox.Show($"User {user} has been created!");
}
else
{
MessageBox.Show($"User {user} has not been created!");
}
}
private void button1_Click(object sender, EventArgs e)
{
string user = textBox1.Text;
string pass = textBox2.Text;
if (IsLogin(user, pass))
{
MessageBox.Show($"Welcome {user}!");
}
else
{
MessageBox.Show($"{user} does not exist or password is incorrect!");
}
}
public bool Register(string user, string pass)
{
string query = $"INSERT INTO Whitelisted (id, username, password) VALUE ('', '{user}', '{pass}');";
try
{
if (OpenConnection())
{
MySqlCommand cmd = new MySqlCommand(query, conn);
try
{
cmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
return false;
}
}
else
{
conn.Close();
return false;
}
}
catch (Exception ex)
{
conn.Close();
return false;
}
}
public bool IsLogin(string user, string pass)
{
string query = $"SELECT * FROM Whitelisted WHERE username = '{user}' AND password = '{pass}');";
try
{
if (OpenConnection())
{
MySqlCommand cmd = new MySqlCommand(query, conn);
MySqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
reader.Close();
conn.Close();
return true;
}
else
{
reader.Close();
conn.Close();
return false;
}
}
else
{
conn.Close();
return false;
}
}
catch (Exception ex)
{
conn.Close();
return false;
}
}
private bool OpenConnection()
{
try
{
conn.Open();
return true;
}
catch (MySqlException ex)
{
switch (ex.Number)
{
case 0:
MessageBox.Show("Connection to server failed!");
break;
case 1045:
MessageBox.Show("Server username or password is incorrect!");
break;
}
return false;
}
}
}
}