question

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

Storing values from a single column within a list.

I would like to read all of the values from the column name "seatNo" and store those values within a list. I've done most of the code but i have absolutely no idea how to actually store the values. Can someone help?

81194-image.png




Here is what i have so far:

 private List<string> seatNumbers = new List<string>();
    
         public void PreparingSeatUI()
         {
             string GetDataQuery = "SELECT seatNo FROM bookings where schedule = @scheduleId";
             cmd = new SqlCommand(GetDataQuery, _IsqlDataFunctions.GetConnection());
             cmd.Parameters.AddWithValue("@scheduleId", txtSchedule.Text);
    
             _IsqlDataFunctions.GetConnection().Open();
             var reader = cmd.ExecuteReader();
             while (reader.Read())
             {
                 seatNumbers.Add() // not sure how to add each value here
                 {
    
                 });
             }
             _IsqlDataFunctions.GetConnection().Close();
                
         }


Thankyou

sql-server-generaldotnet-csharpwindows-forms
image.png (7.6 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

You have a data type mismatch; "seatNo" in database is integer and in your code it's string
Anyway, this should work:

 seatNumbers.Add(reader.GetInt32(0).ToString());
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.