Using ASP.net web display content from database going a loop

I need to add page content dynamically reading from the database. The following is just a draft code

<div> 
<asp button>
< asp label> 
</div>

Now let’s say I read an ID from the DB as 1 then ill assign one button with id=1 then likewise reading through the database I need to display divisions for each iteration of the data set read.

How am I possible to do it?

Just like creating a feed with buttons like Facebook
Details images are displayed for each separate posts

I want something like this

while(reader.read())
 {<div><label><label><div> 
}

OutCome should be

Name ABC 
Country B 
OK Cancel 


Name QWE 
Country L 
OK Cancel 

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Initially, you can try something like this and develop on it:

//using System.Web.UI.HtmlControls;    


var searchValue = "somethingYouSearchFor" ;

var connection = new SqlConnection("connection string here");
var query = new SqlCommand("SELECT Name, Country FROM MyTable WHERE value3 = @SearchValue", connection);
query.Parameters.AddWithValue("@SearchValue", searchValue);

connection.Open();

SqlDataReader reader = query.ExecuteReader();

if (reader.HasRows)
{
    while (reader.Read())
    {
        var addDiv = new HtmlGenericControl("div");      
        addDiv.ID = "anyId"; //Optional
        addDiv.Style.Add(HtmlTextWriterStyle.BackgroundColor, "Yellow"); //Optional
        addDiv.Style.Add(HtmlTextWriterStyle.Color, "Black"); //Optional
        addDiv.Style.Add(HtmlTextWriterStyle.Height, "200px"); //Optional
        addDiv.Style.Add(HtmlTextWriterStyle.Width, "300px"); //Optional
        
        var nameLabel = new HtmlGenericControl("label");  
        nameLabel.InnerText  = reader["Name"];
        addDiv.Controls.Add(nameLabel);
        
        var countryLabel = new HtmlGenericControl("label");  
        countryLabel.InnerText  = reader["Country"];
        addDiv.Controls.Add(countryLabel);
        
        this.Controls.Add(addDiv);

    }
}

connection.Close();
reader.Close();
query.Dispose();

It could be the simplest way to access the database, you need to fill connection string (using this link you can see several examples including the MS SQL connection string).

You need to provide a valid SQL query that matches your database (The good practice is to test your SQL query in MS SQL Studio aka SSMS) and then implement it in C# code.

Solution 2

I think the asp.net Repeater control works best in your situation.
MSDN link here.
Similar to the example shown at the MSDN docs, you would fill a collection with the data from your database and then bind that collection to your repeater’s data source.

Repeater1.DataSource = values;
Repeater1.DataBind();

In your markup, you can access object in the collection within

<asp:Repeater id="Repeater1" runat="server">
      <ItemTemplate>
         <%# DataBinder.Eval(Container.DataItem, "Name") %> (<%# DataBinder.Eval(Container.DataItem, "Content") %>)
      </ItemTemplate>             
   </asp:Repeater>

Spice it up with more markup and styling to fit your needs.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply