0

i want to Select Values ex(Date,Time....)from SQLServer on the PageLoad & shows Them in The Many Labels. i try this code but in all labels shows the Time . i want to show All Values not one value in all labels . Please help me .

string strquery = "select Time,Date,SeatPrice,EventName from Event_SingleReservation"; SqlConnection connection2 = DBConnection.getConnection(); connection2.Open(); SqlCommand cmd2 = new SqlCommand(); cmd2.Connection = connection2; cmd2.CommandText = strquery;

    string eventname = cmd2.ExecuteScalar().ToString();
    lbl1_EventName.Text = eventname;



    string eventdate = cmd2.ExecuteScalar().ToString();
    lbl2_EventDate.Text = eventdate;

    string eventtime = cmd2.ExecuteScalar().ToString();
    lbl3_EventTime.Text = eventtime;

    string seatprice = cmd2.ExecuteScalar().ToString();
    lbl_seatpriceshow.Text = seatprice; 
Ahmed
  • 9
  • 3

1 Answers1

0

The ExecuteScalar() selects only one value from the first column - i.e. using it against select Time,Date,SeatPrice,EventName from Event_SingleReservation will return only Time which is the first column.

To select all values you should use ExecuteReader()

SqlDataReader reader = cmd2.ExecuteReader();
if (reader.Read())
{
    lbl1_EventName.Text = reader[0];
    lbl3_EventDate.Text = reader[1];
    ...
}

See What is the difference between ExecuteScalar, ExecuteReader and ExecuteNonQuery?

Community
  • 1
  • 1
user2316116
  • 6,726
  • 1
  • 21
  • 35