question

Aaronsoggi-6095 avatar image
0 Votes"
Aaronsoggi-6095 asked Aaronsoggi-6095 commented

Struggling to store values in my list using SQl query

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?

sql-server-generaldotnet-csharpwindows-forms
image.png (3.8 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

OlafHelper-2800 avatar image
0 Votes"
OlafHelper-2800 answered Aaronsoggi-6095 commented

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


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

oh yeahhh! I forgot to add that. Thank you :)

0 Votes 0 ·