Struggling to store values in my list using SQl query

Aaron soggi 246 Reputation points
2021-03-23T15:02:56.96+00:00

I have a sign in method, which gets 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.

To confirm that these details have been saved I've then used MessageBox.Show(userDetails[0].ToString()) to display the first value. however I'm just getting the name of the class :
80628-image.png

Below is the code for the sign in method:

public static List<CustomerLogin> userDetails = new List<CustomerLogin>();   
  
 private void SignInButton_Click(object sender, EventArgs e)  
        {  
            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].ToString());                                             
        }  

This is the code i have within the CustomerLogin class:

    {  
        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; }        
    }  

Finally this is the GetCustomerLogin function that im calling within the SignInButton_Click method

   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();  
              
        }  

Can someone please tell me what im doing wrong here? Thankyou!

EDIT: ive noticed that I might be calling the attributes of the customerLogin the wrong way i.e userdetails[0]. What would be the correct way to do this?

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,820 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,639 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,197 questions
0 comments No comments
{count} votes

Accepted answer
  1. Olaf Helper 40,656 Reputation points
    2021-03-23T15:35:10.317+00:00

    however I'm just getting the name of the class

    Your class CustomerLogin don't override the ToString() method, so how should .NET know what to return others then the class name?

    See How to override the ToString method (C# Programming Guide) for how to


0 additional answers

Sort by: Most helpful