简体   繁体   中英

obtain the number of rows been selected from ListView

I want to get the number of rows selected or the SQL return code. The idea is not to do the listview1.dataBind when nothing has been returned. I cannot execute the cmd.ExecuteNonQuery() here, I know. I would like to get something similar.

private void ListApointement(string sChoixDdl)
{
    using (ApointementDataContext db = new ApointementDataContext())
    {
        var aptItems = from Apointement in db.Apointement 
                       where Apointement.doctorName == sChoiceDdl && 
                             Apointement.isAvailable == true && 
                             Apointement.date >= DateTime.Now 
                       select Apointement;

                  ListView1.DataSourceID = null;
                  ListView1.DataSource = aptItems;

//I want to get the number or rows which have been selected or the sql return code.
                  int numberOfRecords = cmd.ExecuteNonQuery(); 

                  ListView1.DataBind();

    };
}

You should be able to do this:

if (aptItems.Count() > 0)
    ListView1.DataBind();

Here are I fixed this issue. I left the DataBind() outside the if statement because it was causing an error "Cannot access a disposed object." after in the program. I decided to generate an empty ListView + an error message.

                  ListView1.DataSourceID = null;
                  ListView1.DataSource = rdvItems;

                  int numberOfRecords = rdvItems.Count();
                  if (numberOfRecords == 0)
                  {
                      lblMessage.Text = "No Apointement are available";

                  }


                  ListView1.DataBind();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM