ByGoalZ
ByGoalZ
CC#
Created by ByGoalZ on 7/3/2024 in #help
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!");
}
}
}
21 replies
CC#
Created by ByGoalZ on 5/5/2024 in #help
Newbie question: Decleration of stack vs Arrays
Hello, so I know the basics of C# but one thing I never understood or learned was how data structures are being created/declared So there are like 3+ ways of declaring an array, I typically use: String[] example = new string[3]; (First question: What is new for? Why is it being used here?) And today I learned stacks but they are declared differently: Stack<int> stack = new Stack<int>(); So why is that? Why isnt it Stack[int] stack = new Stack; And what are the brackets for
18 replies
CC#
Created by ByGoalZ on 4/2/2024 in #help
IndexOutOfRange Leetcode help
Hey, having an issue with my leetcode problem. I know that its supposed to be a bitwise XOR operation but I tried a different algorithm as im not that advanced yet. "Given a non-empty array of integers nums, every element appears twice except for one. Find that single one."
public class Solution {
public int SingleNumber(int[] nums) {
int[] nums2 = new int[nums.Length];

if (nums.Length == 1) {
return nums[0];
}
for (int i = 0; i < nums.Length; i++) {
if (Array.IndexOf(nums2, nums[i]) < 0) {
nums2[i] = nums[i];

} else {
var x = Array.IndexOf(nums2, nums[i]);
nums[x] = 0;
nums[i] = 0;
}
}

var index = Array.FindIndex(nums, value => value != 0);
return nums[index];





}
}
public class Solution {
public int SingleNumber(int[] nums) {
int[] nums2 = new int[nums.Length];

if (nums.Length == 1) {
return nums[0];
}
for (int i = 0; i < nums.Length; i++) {
if (Array.IndexOf(nums2, nums[i]) < 0) {
nums2[i] = nums[i];

} else {
var x = Array.IndexOf(nums2, nums[i]);
nums[x] = 0;
nums[i] = 0;
}
}

var index = Array.FindIndex(nums, value => value != 0);
return nums[index];





}
}
Error:Unhandled exception. System.IndexOutOfRangeException: Index was outside the bounds of the array.
116 replies
CC#
Created by ByGoalZ on 8/25/2023 in #help
❔ Get website icons from URL
Hey, so I'm currently learning C# (i have coded in other languages already) and for my first project I wanted to make a password manager. I already kind of planned how it should work but I thought of a cool idea. In a password manager you give the website (i.e. twitter.com) and your password (then it will be saved to a text file). What I want to do is to automatically get the icon of that website (if possible) and add it on an imagelabel (im using windows forms in visual studio). Would that even work, and if yes, how? It may be too advanced as a beginner but I like learning like that.
18 replies