Home > DeveloperSection > Forums > Binding Gridview Problem
Ankit Singh

Total Post:341

Points:2389
Posted on    September-29-2014 12:31 AM

 ASP.Net ASP.Net  MSSQL Server 
Ratings:


 1 Reply(s)
 592  View(s)
Rate this:

I have a problem when when while loop repeats it iteration the already binded is replacing by new records but i want to bind all data 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;

    }

}



Sumit Kesarwani

Total Post:378

Points:2694
Posted on    September-29-2014 12:57 AM

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 here

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();

        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 here

GridView1.DataSource = newsProfiles;

GridView1.DataBind();


Don't want to miss updates? Please click the below button!

Follow MindStick