Resolved Accessing an individual value within a list

sock1992

Well-known member
Joined
May 20, 2020
Messages
107
Programming Experience
Beginner
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:

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:
Perhaps you could provide your solution so that it might help others with similar issues. Also, if your solution is suboptimal, we may be able to help you by improving it.

Also, unless you do it almost immediately, it is generally preferred that you only edit a post in order to fix errors in it. Otherwise, please add a new post.
 
Perhaps you could provide your solution so that it might help others with similar issues. Also, if your solution is suboptimal, we may be able to help you by improving it.

Also, unless you do it almost immediately, it is generally preferred that you only edit a post in order to fix errors in it. Otherwise, please add a new post.
apologies dude, i've put the solution in the post above.
 
So, despite my asking you not to edit existing posts, you edited your existing post and the solution in the same post as the question? Nice.

Apart from that, the solution code makes little sense anyway. You go through the whole list and get the CustomerId of each one and just throw them all away except the last one. If it's just the last one you want, why not just get the last one? If you want them all or one at a specific index, that code won't help.
 
So, despite my asking you not to edit existing posts, you edited your existing post and the solution in the same post as the question? Nice.

Apart from that, the solution code makes little sense anyway. You go through the whole list and get the CustomerId of each one and just throw them all away except the last one. If it's just the last one you want, why not just get the last one? If you want them all or one at a specific index, that code won't help.
damn I'm sorry, I've literally just woke up and read your post wrong :rolleyes: noted for next time.(y)

I will only be storing the data from one customer in the list (This will be the customer that sign's in). Its a college project I'm doing for a booking system, just a model which allows a customer to sign in and then place a booking. I needed a way of storing a single customers details and then using that in the account section and also to fulfil queries such as inserting data into the booking table which has a customerId as a foreign key.

So now when I'm doing that specific query:

"Insert into Bookings (seatNo, ticketType, price, dateOfPurchase, customerId, scheduleId) VALUES ( @seatNo, @ticketType, @price, @date, @customerId, @scheduleId )";

i can just call the GetCustomerId() function at @customerId
 
If you only need to store one object then why use a list at all? What would you do if you had to store a single string or a single int? I'm guessing that you would not use a list in that case.
 

Latest posts

Back
Top Bottom