Click here to Skip to main content
14,869,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working with News articles display, I would like to make those news display by Years ==> Months ==> Days . The UI will display like following screen:
News 2019
 >> January (2)
     >>01/02/2019 articles
     >>01/09/2019 articles
News 2018
  >> December (2)
     >>12/02/2019 articles
     >>12/09/2019 articles
News 2017
  >> January (3)
     >>01/02/2019 articles
     >>01/09/2019 articles
News 2016
  >> January (1)
     >>01/02/2019 articles
     >>01/09/2019 articles


But I only can make it display like following.
News 2019
 >> January (2)
 
News 2018
  >> December (2)
   
News 2017
  >> January (3)


What I have tried:

protected void loadyears()
   {
       con.Open();
       BindMenu();
       try {
           //Create years for news view
           SqlCommand scmdY = new SqlCommand("select distinct year([NewsDate]) as Years from [WebSiteNewsMaint] order by Years desc ", con);
           SqlDataAdapter cmdY = new SqlDataAdapter(scmdY);

           DataSet ds = new DataSet();
           cmdY.Fill(ds, "WSNewsYears");

           //Create Months  for news view
           SqlCommand scmdM = new SqlCommand("select top(100) percent Datename(month,newsdate) as NMDate, MONTH(NewsDate) as MDate,  COUNT(Newsid) AS NArticle, year([NewsDate]) as Years from [WebSiteNewsMaint]  GROUP BY  Datename(month,newsdate) ,month(newsdate),year(newsdate) ORDER BY MONTH(NewsDate) DESC", con);
           SqlDataAdapter cmdM = new SqlDataAdapter(scmdM);
           cmdM.Fill(ds, "WSNewsMonths");

           //Create the relation between the years and months views

           ds.Relations.Add("YearMonthRelation", ds.Tables["WSNewsYears"].Columns["Years"], ds.Tables["WSNewsMonths"].Columns["Years"]);

           Years.DataSource = ds.Tables["WSNewsYears"];
           Page.DataBind();

       } catch (SqlException ex)
       {
           errormessage.Text = ex.Message.ToString();
       }
       finally
       {

       con.Close();
       }


   }
Posted
Updated 20-Feb-19 4:11am

1 solution

You've already got two levels working. For a third level, you just need a third table, a second relationship, and another nested repeater.
C#
const string Query = @"SELECT DISTINCT 
    Year(NewsDate) As Years 
FROM 
    WebSiteNewsMaint 
ORDER BY 
    Years DESC
;

SELECT 
    Year(NewsDate) As Years, 
    Month(NewsDate) As MDate, 
    DateName(month, NewsDate) As NMDatem, 
    Count(1) As NArticle 
FROM 
    WebSiteNewsMaint
GROUP BY
    Year(NewsDate) As Years, 
    Month(NewsDate) As MDate, 
    DateName(month, NewsDate) As NMDatem
ORDER BY
    Years DESC,
    MDate DESC
;

SELECT
    Year(NewsDate) As Years, 
    Month(NewsDate) As MDate, 
    NewsDate,
    NewsId
FROM
    WebSiteNewsMaint
ORDER BY
    Years DESC,
    MDate DESC,
    NewsDate DESC
;";

using (SqlCommand command = new SqlCommand(Query, con))
{
    SqlDataAdapter da = new SqlDataAdapter(command);
    
    // Map the three result sets to friendly names:
    // See: https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/dataadapter-datatable-and-datacolumn-mappings#handling-multiple-result-sets
    da.TableMappings.Add("Table", "WSNewsYears");
    da.TableMappings.Add("Table1", "WSNewsMonths");
    da.TableMappings.Add("Table2", "WSNewsDays");
    
    DataSet ds = new DataSet();
    da.Fill(ds);
    
    // Relationship from years to months:
    ds.Relations.Add("YearMonthRelation", ds.Tables["WSNewsYears"].Columns["Years"], ds.Tables["WSNewsMonths"].Columns["Years"]);
    
    // Relationship from months to days:
    ds.Relations.Add("MonthDayRelation", 
        new[] { ds.Tables["WSNewsMonths"].Columns["Years"], ds.Tables["WSNewsMonths"].Columns["MDate"] }, 
        new[] { ds.Tables["WSNewsDays"].Columns["Years"], ds.Tables["WSNewsDays"].Columns["MDate"] });
    
    Years.DataSource = ds.Tables["WSNewsYears"];
    Page.DataBind();
}

Put a "days" nested repeater inside the "months" nested repeater, and bind its DataSource property in the same way that you're already doing for the "months" repeater.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900