1

I have a Grid View in which there is a Date column. In database I have one unique id column and a Date column. I want to get all the ids from the SQL query so that I can get all the ids for a particular date. For example if I write "27-06-2013" then all ids 121,123,124 receive. When I am doing using Response.Write I am getting only a single id. and all the ids then I want to show in grid view.

SqlCommand cmd = new SqlCommand("Select * from Scheduleappointment where Doctor_Id=@docid and Convert(varchar(5),Start_Time,108)+'-'+Convert(varchar(5),End_Time,108)=@time and Date=@date", con);
cmd.Parameters.AddWithValue("@docid", Label1.Text);
cmd.Parameters.AddWithValue("@time", timing.Text);
cmd.Parameters.AddWithValue("@date", Convert.ToDateTime(txtdate.Text).ToString("yyyy-MM-dd"));
conopen();
cmd.ExecuteNonQuery();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
    Response.Write(dr["Id"].ToString());
}

I want to show these id values in single row of grid view.Like 121,123 in single row next to date 27-06-2013.

9
  • 1
    show us the code what u r doing? Commented Jun 27, 2013 at 11:42
  • select id from tablename where date='27-06-2013 ' Commented Jun 27, 2013 at 11:48
  • @subhash-bt its not giving all the ids for 27-06-2013.I have tried this. Commented Jun 27, 2013 at 11:49
  • i m not getting that tolist();..when i m writing list<string> ids=new list<string>(); Commented Jun 27, 2013 at 11:52
  • ohh sorry my bad, instead of if use While(dr.Read()) { Response.Write(dr["Id"].ToString()); } Commented Jun 27, 2013 at 11:57

1 Answer 1

5

The issue is with the last part

if (dr.Read())
{
    Response.Write(dr["Id"].ToString());
}

The if will evaluate and read only once. If there is at least one row, it will be written. You probably need while, to read until there are no more rows.

while (dr.Read())
{
    Response.Write(dr["Id"].ToString());
}

You will probably also want to add some formatting (spaces or line breaks between values) as well as clean up code (close the data reader, etc.) after writing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.