Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to show total number of users whose data is stored in a table in SQL database which is based on year in chart control in asp.net i.e. all the users who registered in a particular year, these data should be displayed for all the months in a year. Firstly I have to show data for all the years. Than the user can select a particular year from the dropdownlist and than populate the chart control for that particular year. When the user has selected a year from dropdownlist than for that particular year the data should be populated for individual months example: 40 users from Jan, 85 from March, etc.

SQL
int month = 1;

for (int i = month; i <= 12; i++)
{
    var usercount = (from u in context.Profiles
                     where u.CreatedOn.Month == month
                     select u).Count();
}


I am using entity framework for writing the database related codes.
Posted
Updated 14-Apr-15 21:24pm
v3
Comments
Sergey Alexandrovich Kryukov 15-Apr-15 3:25am    
What have you tried so far?
—SA

 
Share this answer
 
Comments
Maciej Los 15-Apr-15 5:55am    
Good advice ;)
Sergey Alexandrovich Kryukov 15-Apr-15 9:00am    
Thank you, Maciej.
—SA
If you want to get the count of users created each month of specified year, use:
C#
//get distinct years and bind data to combobox
var years = context.Profiles
                  .Select(u=>u.CreatedOn.Year)
                  .Distinct()
                  .ToList();
ComboBox1.DataSource = years;


C#
int uyear = 2014; //ComboBox1 value
var qry = context.Profiles
                  .Where(u=>u.CreatedOn.Year == uyear)
                  .GroupBy(u=> u.CreatedOn.Month)
                  .Select(grp=>new{month=grp.Key, count=grp.Count()})
                  .ToList();
//now, you can bind data from qry object to GridView, ListView, etc.


Good luck!
 
Share this answer
 
Comments
Amit Jangid 15-Apr-15 5:00am    
Thank Your very much for helping.......
Maciej Los 15-Apr-15 5:57am    
You're very welcome ;)
Sergey Alexandrovich Kryukov 15-Apr-15 9:01am    
Is that all he wanted? All right, a 5. :-)
—SA
Maciej Los 15-Apr-15 10:10am    
Thank you, Sergey ;)
Sometimes it's just enough...

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