Storing values from a single column within a list.

Aaron soggi 246 Reputation points
2021-03-24T12:09:43.92+00:00

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

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,839 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,813 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,309 questions
0 comments No comments
{count} votes

Accepted answer
  1. Olaf Helper 41,006 Reputation points
    2021-03-24T12:17:02.387+00:00

    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());
    
    0 comments No comments

0 additional answers

Sort by: Most helpful