How can I get specific column from each row in DataTable. In this example I want to get top 3 news and I got problem with Image. This code gives allways a image from first row, because I defined dr.Rows[0]["Image"]. How can i change this to get right image for every row?
SqlCommand cmd = new SqlCommand("SELECT TOP 3 Title, Content, Date, Autor, Category, Image FROM News", con);
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow row in dt.Rows)
{
Panel panel = new Panel();
panel.Attributes.Add("class", "post");
HtmlGenericControl header = new HtmlGenericControl("h2");
header.InnerHtml = row["Title"].ToString();
header.Attributes.Add("class", "naslovPosta");
panel.Controls.Add(header);
HtmlGenericControl date = new HtmlGenericControl("p");
date.InnerHtml = ((DateTime)row["Date"]).ToString("dd.MM.yyyy.");
date.Attributes.Add("class", "datumPosta");
panel.Controls.Add(date);
Image img1 = new Image();
img1.ImageUrl = ResolveUrl("~/Images/" + dt.Rows[0]["Image"]);
panel.Controls.Add(img1);
HtmlGenericControl content = new HtmlGenericControl("pre");
content.InnerHtml = row["Content"].ToString();
panel.Controls.Add(content);
pnlNews.Controls.Add(panel);
}
You already enumerate all rows, you find the current row in the row
-variable, so replace:
img1.ImageUrl = ResolveUrl("~/Images/" + dt.Rows[0]["Image"]);
with
img1.ImageUrl = ResolveUrl("~/Images/" + row["Image"]);
like you did it already f.e. at row["Title"].ToString();
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments