I'm trying to load news from my SQL Server database but I'm having an issue .
Index.aspx.cs :
public static string Load_News()
{
string returnednews = "";
SqlConnection connection = new SqlConnection(Functions.ConnectionTag());
try
{
connection.Open();
using (SqlDataReader reader = new SqlCommand("SELECT TOP 3 * FROM website.dbo.news ORDER BY ui DESC", connection).ExecuteReader())
{
while (reader.Read())
{
returnednews = string.Format("<div class='article'><div class=\"a_header\"> <div class='title' ><a href = '/articles/article.aspx?id={0}' ><i class='fa fa-feed' ></i>{1}</a></div></div><div class='a_body' ><div class='a_thumb' ><img src = '/img/news_thumb_1.jpg' /><hr>{4}<br/><font style='font -weight: 600; color: #8e44ad;'>{2}</font></div><div class='a_content' ><p>{3}</p></div></div><div class='a_footer'></div></div>", new object[] { reader["ui"], reader["title"], reader["poster"], reader["announcement"], reader["date"] });
}
}
return returnednews;
}
catch (Exception exception)
{
return exception.ToString();
}
finally
{
if (connection != null)
{
connection.Dispose();
}
}
}
Index.aspx :
<%@ Page
Title="Website"
Language="C#"
MasterPageFile="~/wes.master"
AutoEventWireup="true"
CodeFile="index.aspx.cs"
Inherits="index" %>
<%@ MasterType VirtualPath='~/wes.master' %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div>
<%=Load_News()%>
</div>
</asp:Content>
Everything works perfectly and I get the results just fine however , It only gets 1 result from the database instead of 3 ? As you can see on the query , I'm selecting the three TOP news but I'm only getting one (query works on SQL Server without issues)
returnednewsin each iteration through the loop. Perhaps you should be putting the rows into a contains (such as an array) or concatenating them together.C#as well ;).