0

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)

4
  • What if your table has only one record Commented Feb 28, 2016 at 12:49
  • 1
    It looks like you are overwriting returnednews in each iteration through the loop. Perhaps you should be putting the rows into a contains (such as an array) or concatenating them together. Commented Feb 28, 2016 at 12:49
  • Oh right , I'm dumb . It should be "returnednews += string.Format(" instead of returnednews = string.Format(... thank you :) and sorry , I don't know how I missed that Commented Feb 28, 2016 at 12:51
  • 1
    @GordonLinoff - You know C# as well ;). Commented Feb 28, 2016 at 12:51

1 Answer 1

4

Since you assign your variables to returnednews every iteration again and again, your returnednews will only have the last row values.

I think you need to some string concatenation for every values that your reader returns like;

returnednews += string.Format("<div class='article'>...

Also use using statement to dispose your connection, command and reader automatically instead of calling Dispose method manually.

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

1 Comment

Yes I know , idk how I could miss that . fixed

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.