Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace DataTableExample
{
    class Program
    {
        static void Main(string[] args)
        {

            DataTable dt = new DataTable("Student");

            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Branch", typeof(string));
            dt.Columns.Add("Age", typeof(int));
            dt.Columns.Add("SocialMarks", typeof(int));
            dt.Columns.Add("MathsMarks", typeof(int));
            dt.Columns.Add("ScienceMarks", typeof(int));
            dt.Columns.Add("TotalMarks", typeof(int));
            dt.Columns.Add("AverageMarks", typeof(decimal));


            List<Students> students = new List<Students>();//Students the type or namespace coulnt be found 

            Console.WriteLine("Please enter a number to create students and start processing");
            string[] classes = { "Semester-I", "Semester-II", "Semester-III", "Semester-IV" };
            string[] Branches = { "Computers", "Electronics", "Civil", "Mechanics" };



            Random cls = new Random();
            Random brnc = new Random();
            Random ag = new Random();
            Random marks1 = new Random();
            Random marks2 = new Random();
            Random marks3 = new Random();
            int n = int.Parse(Console.ReadLine());
            for (int i = 0; i < n; i++)
            {



                DataRow dr = dt.NewRow();
                dr["id"] = i;
                dr["Name"] = "Name" + i.ToString();
                dr["Branch"] = Branches[brnc.Next(0, 3)];
                int age = ag.Next(17, 22);
                int socialMarks = marks1.Next(50, 100);
                int mathsMarks = marks2.Next(50, 100);
                int scienceMarks = marks3.Next(50, 100);
                int totalMarks = socialMarks + mathsMarks + scienceMarks;
                dr["Age"] = age;
                dr["SocialMarks"] = socialMarks;
                dr["MathsMarks"] = mathsMarks;
                dr["ScienceMarks"] = scienceMarks;
                dr["TotalMarks"] = totalMarks;
                dr["AverageMarks"] = (float.Parse(dr["TotalMarks"].ToString()) / 3).ToString();
                dt.Rows.Add(dr);
            }
            Console.WriteLine("from data table");
            Console.WriteLine("ID | Name | Age| Branch | Socail | Science | Maths | Total Marks|Average");
            foreach (DataRow dr in dt.Rows)

            {
                Console.WriteLine(dr["Id"] + "| " + dr["Name"] + "|" + dr["Age"] + "|" + dr["Branch"] + "|" + dr["SocialMarks"] + "|" + dr["ScienceMarks"] + "|" + dr["MathsMarks"] + "|" + dr["TotalMarks"] + "|" + dr["AverageMarks"]);
            }
       

            Console.WriteLine("list the count of students agewise");
            var groupbyage = Student.GroupBy(s => s.Age).Select(lst => new { Age = lst.Key, Count = lst.Count() });
            foreach (var ags in groupbyage.OrderBy(lst => lst.Age))
            {
                Console.WriteLine("Age : " + ags.Age + " | Count: " + ags.Count);
            }
Posted
Updated 28-Dec-15 1:55am
v2
Comments
Thomas Daniels 28-Dec-15 7:58am    
Does the Students class exist?
Member 12185899 28-Dec-15 8:20am    
no ...now its working thank you
Hemant Singh Rautela 28-Dec-15 8:12am    
:-) you copied the code/concept form somewhere and used it. First read properly from where you copied the concept/code.

1 solution

It seems that you forgot to add using statement for namespace in which "Students" class is present.

Also check the spelling of "Students" as the class Names are usually singular.
 
Share this answer
 

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