C
C#2y ago
yumo

❔ login system saves data from user

Hi this is the code that i have and i dont know why but its showing me the username and not the realname on the label, why? Button on Form1 to check if user credentials are correct and if user exists open MainMenu and display the realname of the user logged in.
private void btnEntrar_Click(object sender, EventArgs e)
{
string connectionString = "Data Source=(localdb)\\MSSqlLocalDB;Initial Catalog=oil;Integrated Security=True;Pooling=False";
string username = txtUsername.Text;
string password = txtPassword.Text;
UserData loggedInUser = null;

using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();

SqlCommand command = new SqlCommand("SELECT * FROM Users WHERE username = @username AND password = @password", connection);
command.Parameters.AddWithValue("@username", username);
command.Parameters.AddWithValue("@password", password);

SqlDataReader reader = command.ExecuteReader();

if (reader.HasRows)
{
reader.Read();
int id = reader.GetInt32(0);
string realname = reader.GetString(1);
string nivel = reader.GetString(2);

// create a new User object with the retrieved data
loggedInUser = new UserData(id, realname, nivel);

// open the main menu form
MainMenu mainMenu = new MainMenu(loggedInUser);
mainMenu.Show();
this.Hide();
}
else
{
// invalid credentials, display error message
MessageBox.Show("Credenciais inválidas!");
}

reader.Close();
}
}
private void btnEntrar_Click(object sender, EventArgs e)
{
string connectionString = "Data Source=(localdb)\\MSSqlLocalDB;Initial Catalog=oil;Integrated Security=True;Pooling=False";
string username = txtUsername.Text;
string password = txtPassword.Text;
UserData loggedInUser = null;

using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();

SqlCommand command = new SqlCommand("SELECT * FROM Users WHERE username = @username AND password = @password", connection);
command.Parameters.AddWithValue("@username", username);
command.Parameters.AddWithValue("@password", password);

SqlDataReader reader = command.ExecuteReader();

if (reader.HasRows)
{
reader.Read();
int id = reader.GetInt32(0);
string realname = reader.GetString(1);
string nivel = reader.GetString(2);

// create a new User object with the retrieved data
loggedInUser = new UserData(id, realname, nivel);

// open the main menu form
MainMenu mainMenu = new MainMenu(loggedInUser);
mainMenu.Show();
this.Hide();
}
else
{
// invalid credentials, display error message
MessageBox.Show("Credenciais inválidas!");
}

reader.Close();
}
}
11 Replies
yumo
yumoOP2y ago
This is what i have on MainMenu form
private UserData loggedInUser;
public MainMenu(UserData user)
{
InitializeComponent();
loggedInUser = user;

// display the logged-in user's first name on the lblNomeUtilizador label
lblNomeUtilizador.Text = loggedInUser.RealName;
}
private UserData loggedInUser;
public MainMenu(UserData user)
{
InitializeComponent();
loggedInUser = user;

// display the logged-in user's first name on the lblNomeUtilizador label
lblNomeUtilizador.Text = loggedInUser.RealName;
}
And this is my UserData class to save the information of the user
public class UserData
{
public int UserId { get; set; }
public string RealName { get; set; }
public string Nivel { get; set; }

public UserData(int id, string realname, string nivel)
{
UserId = id;
RealName = realname;
Nivel = nivel;
}

}
public class UserData
{
public int UserId { get; set; }
public string RealName { get; set; }
public string Nivel { get; set; }

public UserData(int id, string realname, string nivel)
{
UserId = id;
RealName = realname;
Nivel = nivel;
}

}
Angius
Angius2y ago
Use the debugger, see if the variables you get from the database are what you think they are That's why I recommend using Dapper at least, instead of having to fuck around with loosely-typed readers
yumo
yumoOP2y ago
idk hwo to work with dapper mb
Angius
Angius2y ago
Easier than with ADO that's for sure
mikernet
mikernet2y ago
It's better to select the columns instead of relying on the order that * returns them in
yumo
yumoOP2y ago
This? SELECT realname FROM Users WHERE username = @username AND password = @password ?
mikernet
mikernet2y ago
Well you're reading 3 columns So select 3 columns id, realname, nivel
yumo
yumoOP2y ago
Something like this
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();

SqlCommand command = new SqlCommand("SELECT realname, id, nivel FROM Users WHERE username = @username AND password = @password", connection);
command.Parameters.AddWithValue("@username", username);
command.Parameters.AddWithValue("@password", password);

SqlDataReader reader = command.ExecuteReader();

if (reader.HasRows)
{
reader.Read();
int id = reader.GetInt32(0);
string realname = reader.GetString(1);
string nivel = reader.GetString(2);

// create a new User object with the retrieved data
loggedInUser = new UserData(id, realname, nivel);

// open the main menu form
MainMenu mainMenu = new MainMenu(loggedInUser);
mainMenu.Show();
this.Hide();
}
else
{
// invalid credentials, display error message
MessageBox.Show("Credenciais inválidas!");
}

reader.Close();
}
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();

SqlCommand command = new SqlCommand("SELECT realname, id, nivel FROM Users WHERE username = @username AND password = @password", connection);
command.Parameters.AddWithValue("@username", username);
command.Parameters.AddWithValue("@password", password);

SqlDataReader reader = command.ExecuteReader();

if (reader.HasRows)
{
reader.Read();
int id = reader.GetInt32(0);
string realname = reader.GetString(1);
string nivel = reader.GetString(2);

// create a new User object with the retrieved data
loggedInUser = new UserData(id, realname, nivel);

// open the main menu form
MainMenu mainMenu = new MainMenu(loggedInUser);
mainMenu.Show();
this.Hide();
}
else
{
// invalid credentials, display error message
MessageBox.Show("Credenciais inválidas!");
}

reader.Close();
}
But displaying this error: System.InvalidCastException: 'The specific conversion isnt valid.' on this line: int id = reader.GetInt32(0);
mikernet
mikernet2y ago
Thats because you didnt select id as the first column
Angius
Angius2y ago
Meanwhile, in Dapper land
using (SqlConnection connection = new SqlConnection(connectionString))
{
var sql = "SELECT id, realname, nivel FROM Users WHERE username = @username AND password = @password";

var loggedInUser = connection.QueryFirstOrDefault<UserData>(sql, new { username, password });

if (loggedInUser != null)
{
// open the main menu form
MainMenu mainMenu = new MainMenu(loggedInUser);
mainMenu.Show();
this.Hide();
}
else
{
// invalid credentials, display error message
MessageBox.Show("Credenciais inválidas!");
}
}
using (SqlConnection connection = new SqlConnection(connectionString))
{
var sql = "SELECT id, realname, nivel FROM Users WHERE username = @username AND password = @password";

var loggedInUser = connection.QueryFirstOrDefault<UserData>(sql, new { username, password });

if (loggedInUser != null)
{
// open the main menu form
MainMenu mainMenu = new MainMenu(loggedInUser);
mainMenu.Show();
this.Hide();
}
else
{
// invalid credentials, display error message
MessageBox.Show("Credenciais inválidas!");
}
}
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server