Click here to Skip to main content
14,883,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my work I have a table

SQL
reservation_available(seat_id, seat_type, flight_id, available)


where flight_id is a foreign key and seat_type has two value(Economic, Business) consecutively. To retrieve the seat_id and available I wrote:

C#
using (SqlCommand command = new SqlCommand("SELECT seat_id, seat_type, available FROM reservation_available WHERE seat_type = 'Economic' AND reservation_available.flight_id = '" + flight_id + "' ", connection))
               {

                   string s_ID = seat_id.ToString();
                   string e_avail = EconomicAvailable.ToString();
                   string st = seat_type;


                   SqlDataReader myReader = command.ExecuteReader();

                   while (myReader.Read())
                   {
                       s_ID = myReader[0].ToString();
                       st = myReader[1].ToString();
                       e_avail = myReader[2].ToString();

                   }
                   int sId = Convert.ToInt32(s_ID);
                   int eAvail = Convert.ToInt32(e_avail);

                   myReader.Close();
                   set2(sId, eAvail, st);
               }


and similar code for the seat_type = "Business".

But retrieved data only shows the "seat_id" of seat_type = 'Business'. But I want respective seat_id for selected seat_type.

Help needed
Posted
Comments
CHill60 25-Apr-13 4:13am
   
The code you have posted will not return any data for seat_type = 'Business' because you have explicitly coded WHERE seat_type='Economic'

There are two possible reasons for this:

1) Your query is returning no records at all. In which case, set2 will be called with the default values you set when you declared the parameter variables.
2) Your query is returning more than one record. In which case, set2 will be called with the last such record returned only.

The first thing you want to do is use a debugger and look at what is happening. but a breakpoint at the "string s_ID = ..." line and step through to see what is being returned.
   
Some points to consider:
the SQL query may return 0, 1, or more rows. In case of 0 rows, you initialised the variables to some default values - not sure if you understand what you do in those three lines before SqlDataReader myReader...?
Then you correctly use a while loop - but you overwrite the data with every loop. You should create an appropriate object from the data, and add that object to a List.
Instead of the odd conversions from strings, you can get int values from the datareader (if your table is set up correctly...).
What does that set2 function do? Does it overwrite the values you received from the first query with the values you received from the second query?
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900