Search code examples
c#asp.netdatabaseascx

Return latest article in news control asp.net c#


I have got a news control which displays a news article when the id is selected and returned into the url. The problem is that if i go to the page without the id parameter in the url then the page is blank. What I want to do is display the latest news article if no id is selected. I have hacked it atm to display id 157. Any help would be great. Thanks

    protected Article _article;
    protected Article Article
    {
        get
        {
            if (null == _article && null != Request.QueryString["id"])
            {
                _article = Article.GetById(Int64.Parse(Request.QueryString["id"]));
            }
            else
            {
                _article = Article.GetById(Int64.Parse("157"));
            }
            return _article;
        }
    }

Solution

  • From what i understood, you need to keep track of what is the latest ID. And make use of it here instead of "157".

    when ever latest news is published store the new id make it accessible to above code snippet you have shared. Possible ways i think ,

    1. In the session variable store the latest ID
    2. if your code is not load balanced , use staic variable of latest id and update it and let all other access and use it.
    3. Get latest ID info from DB at the place of hardcoding it to 157

    if my understanding of your problem is not right. please elaborate your question