C
C#5d ago
ByGoalZ

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:
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!");
}
}
}
13 Replies
Monsieur Wholesome
Yeah, so... Dont post that connection string Is there a reason youre not working with a local mysql instance? Like with XAMPP or something What is your table defined as? The columns specifically
ByGoalZ
ByGoalZ5d ago
no I just found a free mysql host wdym? I just gave them a name, user and password
Angius
Angius5d ago
You can run a database locally, FYI And I second the question: what is the table defined as?
ByGoalZ
ByGoalZ5d ago
What does definied as mean? Its name is login
Angius
Angius5d ago
The columns
ByGoalZ
ByGoalZ5d ago
the names of the columns? sorry
Angius
Angius5d ago
Is name a varchar? a text? an int?
ByGoalZ
ByGoalZ5d ago
oh
Angius
Angius5d ago
Is the password a varchar, a byte, a datetime?
ByGoalZ
ByGoalZ5d ago
yea thats probably the issue, I didnt change that
Angius
Angius5d ago
If you get zeroes in both, then yes, they're probably some numeric type And MySQL being MySQL just accepts the text and inserts zeroes, instead of throwing an error like any reasonable database would
ByGoalZ
ByGoalZ5d ago
what should the type be? Theres no normal string type. And what about the other properties like Null Index or Length/Values it works!! thanks
Angius
Angius5d ago
Varchar, probably
Want results from more Discord servers?
Add your server
More Posts