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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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.
Method 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.
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