I have a sign in method, which stores the details of a user based on the password and email that they enter. Within this method i call a function which stores those details inside of a list.
My question is how would i be able to access those individual values? I want to be able to access just the customerID. I could iterate through the list, but is there a simpler way?
here is my customer class:
This is my sign in method:
Finally this is my GetCustomerLogin function which is called within the sign in method:
EDIT : fixed the issue
i created a function which iterates through the list and stores the customerId:
Now whenever i need it to complete queries e.g. display customer details in "Update account" section i can call this function.
My question is how would i be able to access those individual values? I want to be able to access just the customerID. I could iterate through the list, but is there a simpler way?
here is my customer class:
C#:
public class CustomerLogin
{
public List<CustomerLogin> loginDetails = new List<CustomerLogin>();
public int CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string ContactNumber { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public override string ToString() => $"{CustomerId}, {FirstName}, {LastName}, {Address}, {ContactNumber}, {Email}, {Password}";
}
This is my sign in method:
C#:
private void SignInButton_Click(object sender, EventArgs e)
{
public static List<CustomerLogin> userDetails = new List<CustomerLogin>();
string query = "SELECT c.customerId, c.firstname, c.lastname, c.address, c.contactNumber, cl.email, cl.password FROM customer c Join customerLogin cl ON cl.customerId = c.customerId where cl.email =@email AND cl.password=@password";
cmd = new SqlCommand(query, _IsqlDataFunctions.GetConnection());
cmd.Parameters.AddWithValue("@email", signInEmail.Text);
cmd.Parameters.AddWithValue("@password", SqlDataFunctions.hashPassword(signInPassword.Text));
_IsqlDataFunctions.GetCustomerLogin(cmd, userDetails);
_IsqlDataFunctions.Login(cmd, new SignIn(), new Dashboard());
MessageBox.Show(userDetails[0]);// ive included this line to see what values i would get, but it gives me all of them together. I only want to access the customerID
}
Finally this is my GetCustomerLogin function which is called within the sign in method:
C#:
public void GetCustomerLogin(SqlCommand cmd, List<CustomerLogin> userDetails)
{
try
{
connection.Open();
var reader = cmd.ExecuteReader();
while (reader.Read())
{
userDetails.Add(new CustomerLogin()
{
CustomerId = reader.GetInt32(0),
FirstName = reader.GetString(1),
LastName = reader.GetString(2),
Address = reader.GetString(3),
ContactNumber = reader.GetString(4),
Email = reader.GetString(5),
Password = reader.GetString(6),
});
}
}
catch (System.NullReferenceException )
{
MessageBox.Show("Your login credentials are incorrect");
}
connection.Close();
}
EDIT : fixed the issue
i created a function which iterates through the list and stores the customerId:
C#:
public static int GetCustomerId()
{
int id = 0;
foreach (var i in userDetails)
{
id = i.CustomerId;
}
return id;
}
Now whenever i need it to complete queries e.g. display customer details in "Update account" section i can call this function.
Last edited: