2

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 

4
  • 1
    What Database? What did you try yet? Commented Dec 21, 2020 at 12:07
  • normal sql server database i want do create a feed from each data row something similar to facebook posts Commented Dec 21, 2020 at 12:09
  • 1
    What did you try? Code? Commented Dec 21, 2020 at 12:10
  • i just cant fire out what process do literally 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 ``` Commented Dec 21, 2020 at 12:18

2 Answers 2

2

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.

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

2 Comments

Thank you it created me the divisions as i wanted but is there a way to style it like adding </br> inbetween the divisions and inbetween the labels and adding bootstrap
@techlk , br is a self-closing tag so according to my knowledge it is not supported by HtmlGenericControl but you can inherit from it to develop something custom. for more info, see the answer in this post please stackoverflow.com/questions/13493586/…
0

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.

Comments

Your Answer

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