0

The course_id inside the while loop have multiple values. i wanted to display all values in GridView, but only last value is being displayed. how can i do that? do i have to store course_id in an array?

int course_id=0;
string query_select_course = "SELECT course_id FROM course_program where program_id = '" + program_id + "' ";

SqlDataReader dr_course = DataAccess.selectDataReader(query_select_course);
while (dr_course.Read())
{
  course_id = (int)dr_course.GetValue(0); // this course_id should have multiple values.
}
dr_course.Close();

string query_select_course_name = "SELECT course_title FROM courses where course_id = '" + course_id + "' ";
DataTable dt = DataAccess.selectData(query_select_course_name);
course_dataGridView.DataSource = dt;
1
  • 1
    You don't need two queries to get your values. JOIN sql clause allows you to do your work with just one query Commented Jun 11, 2016 at 11:55

2 Answers 2

4

Make a List<int> and add values in it.

List<int> course=new List<int>();
while (dr_course.Read())
{
 course.Add((int)dr_course.GetValue(0));
}
Sign up to request clarification or add additional context in comments.

1 Comment

You will need to use something like this as well string query_select_course_name = "SELECT course_title FROM courses where course_id IN ('" + string.Join("','", course) + "')";
1

Since you iterate your reader, your course_id will be the cell value of the last row.

I suggest you to create a List<int> and add those course_id inside your while statement, use string.Join to generate with comma delimiter string with those values like 1, 2, 3, use IN clause instead = inside in your query like;

course_id IN (1, 2, 3)

1 Comment

but than how i will display all values in gridview?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.