Register user doesnt work (MySQL)
Hey, im new to databases and just wanted to learn how to work with them. But every time I regsiter a new user a new line is added to my table but "user" and "password" are both zero instead of the actual values.
Code:
Code:
private static string connectionString = "";
static void Main(string[] args)
{
Console.WriteLine("Press 1 to Login or 2 to Register:");
var choice = Console.ReadLine();
switch (choice)
{
case "1":
Login();
break;
case "2":
Register();
break;
default:
Console.WriteLine("Invalid choice");
break;
}
}
static void Register()
{
Console.Write("Enter username: ");
string username = Console.ReadLine();
Console.Write("Enter password: ");
string password = Console.ReadLine();
string hashedPassword = BCrypt.Net.BCrypt.HashPassword(password);
using (var connection = new MySqlConnection(connectionString))
{
connection.Open();
var command = new MySqlCommand("INSERT INTO login (user, password) VALUES (@user, @password)", connection);
command.Parameters.AddWithValue("@user", username);
command.Parameters.AddWithValue("@password", hashedPassword);
try
{
command.ExecuteNonQuery();
Console.WriteLine("User registered successfully!");
}
catch (MySqlException ex) when (ex.Number == 1062)
{
Console.WriteLine("Username already exists!");
}
}
}private static string connectionString = "";
static void Main(string[] args)
{
Console.WriteLine("Press 1 to Login or 2 to Register:");
var choice = Console.ReadLine();
switch (choice)
{
case "1":
Login();
break;
case "2":
Register();
break;
default:
Console.WriteLine("Invalid choice");
break;
}
}
static void Register()
{
Console.Write("Enter username: ");
string username = Console.ReadLine();
Console.Write("Enter password: ");
string password = Console.ReadLine();
string hashedPassword = BCrypt.Net.BCrypt.HashPassword(password);
using (var connection = new MySqlConnection(connectionString))
{
connection.Open();
var command = new MySqlCommand("INSERT INTO login (user, password) VALUES (@user, @password)", connection);
command.Parameters.AddWithValue("@user", username);
command.Parameters.AddWithValue("@password", hashedPassword);
try
{
command.ExecuteNonQuery();
Console.WriteLine("User registered successfully!");
}
catch (MySqlException ex) when (ex.Number == 1062)
{
Console.WriteLine("Username already exists!");
}
}
}