---
title: "Binding Gridview Problem"  
description: "Binding Gridview Problem"  
author: "Anonymous User"  
published: 2014-09-29  
updated: 2014-09-29  
canonical: https://www.mindstick.com/forum/2275/binding-gridview-problem  
category: "asp.net"  
tags: ["asp.net", "mssql server"]  
reading_time: 2 minutes  

---

# Binding Gridview Problem

I have a [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) when when [while loop](https://www.mindstick.com/forum/34579/while-loop-in-python) repeats it iteration the [already](https://yourviews.mindstick.com/view/88453/the-hidden-ways-ai-is-already-controlling-your-daily-life) binded is replacing by new [records](https://www.mindstick.com/forum/34640/how-to-create-a-stored-procedure-for-display-all-records) but i want to bind all [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) by the iteration of while loop.

I am a new I have no idea how it can be.

```
while (i >= 0){    if (i != 0)    {        group_idd = group_ids[--i];    }                       SqlConnection connection3 = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection_String"].ConnectionString);    using (connection3)    {        using (SqlCommand cmdd = new SqlCommand())        {            cmdd.CommandText = "SELECT [news_category],[id] FROM [news_profile] WHERE [user_id]='" + user_id + "' AND [group_id]='" + group_idd + "' AND [profile_id] IS NOT NULL";            cmdd.Connection = connection3;            connection3.Open();            GridView1.DataSource = cmdd.ExecuteReader();            GridView1.DataBind();            connection3.Close();                                    }    }    if (i == 0)    {    --i;    }}
```

## Replies

### Reply by Sumit Kesarwani

Hi Ankit, \

```
public class NewsProfile{    public string NewsID { get; set; }    public string NewsCategory { get; set; }}Now in your code-behind, do this List<NewsProfile> newsProfiles = new List<NewsProfile>();// while loop hereSqlConnection connection3 = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection_String"].ConnectionString);using (connection3){    using (SqlCommand cmdd = new SqlCommand())    {        cmdd.CommandText = "SELECT [news_category],[id] FROM [news_profile] WHERE [user_id]='" + user_id + "' AND [group_id]='" + group_idd + "' AND [profile_id] IS NOT NULL";        cmdd.Connection = connection3;        connection3.Open();        while (reader.Read())        {            NewsProfile np = new NewsProfile();            np.NewsCategory =  reader.IsDBNull(0) ? "" : reader.GetString(0);            np.NewsID =  reader.IsDBNull(1) ? "" : reader.GetString(1);            newsProfiles.Add(np);        }    }}//end while loop hereGridView1.DataSource = newsProfiles;GridView1.DataBind();
```


---

Original Source: https://www.mindstick.com/forum/2275/binding-gridview-problem

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
