I have two tables, tblstudent having fields [studentId,rollNo,classId,studentName], tblclass having fields [classId, division,className]. The two tables are referenced by field classId. I want to fill tblstudent. Also I want the table tblClass should be filled at the same time.
This is for creating reports. Can any one help me..

You could do something like this:
I made a mock-up of the tables (using classes for easy explanation):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DW_416318_CS_CON
{
   public class CStudent
   {
      public int studentID { get; set; }
      public int rollNo { get; set; }
      public int classID { get; set; }
      public string studentName { get; set; }

      public CStudent()
      {
         studentID = 0;
         rollNo = 0;
         classID = 0;
         studentName = "";
      }
   }

   public class CClass
   {
      public int classID { get; set; }
      public int division { get; set; }
      public string className { get; set; }

      public CClass()
      {
         classID = 0;
         division = 0;
         className = "";
      }
   }

   class Program
   {
      static void Main(string[] args)
      {
         List<CClass> lstClasses = new List<CClass>()
         {
            {new CClass(){classID=1, className="English", division=1}},
            {new CClass(){classID=2, className="CompSci", division=2}},
            {new CClass(){classID=3, className="History", division=3}},
            {new CClass(){classID=4, className="Calculus", division=2}},
         };

         List<CStudent> lstStudents = new List<CStudent>()
         {
            {new CStudent() {studentID=1, studentName="Johnson", rollNo=2012, classID=1}},
            {new CStudent() {studentID=1, studentName="Johnson", rollNo=2012, classID=2}},
            {new CStudent() {studentID=2, studentName="Smith", rollNo=2012, classID=1}},
            {new CStudent() {studentID=2, studentName="Smith", rollNo=2012, classID=4}},
         };

         var lstClassParticipant =
         (
            from student in lstStudents
            join course in lstClasses on student.classID equals course.classID
            where student.rollNo.Equals(2012)

            select new
            {
               STUDENT_NAME = student.studentName,
               COURSE = course.className,
               DIVISION = course.division
            }
         ).ToList();


         lstClassParticipant.ForEach(cp =>
            Console.WriteLine("Student: {0} is taking {1} in division {2}",
               cp.STUDENT_NAME, cp.COURSE, cp.DIVISION));
      }
   }
}

Something like that would allow you to easily pick the columns you want in the report.

Hi Mr. thines01,
Your answer helped me...
but still some problem..

I have created a linqToSqlClass. and named it as clsDataClass.
I have added tables and views to this class.
For creating reports, I have created an object of class tblClass (automatically created while creating clsDataClass).
using a linq query how can I fill the two related classes tblStudent and tblClass.
eg.

var varClass = from c in clsDataClass.tblClass
               where c.classID==1
               select c;
tblClass cls=new clsDataClass.tblClass();
foreach(tblClass c in varClass)
{
cls=c;
}

cls actually contains all the students corresponding to the class
but I dont know how to use this student rows to be displayed in reportviewer table rows

please help me...

Do you want ALL of the students in one result and ALL of the classes in another result?

I really like lists.
Check out the bottom of this -- still mocking-up the db tables:
This, of course, assumes I have a container to put them in.
You can use var instead of List<T> and still call .ToList() to make traversing easier.

using System;
using System.Collections.Generic;
using System.Linq;

namespace DW_416318_CS_CON
{
   public class CStudent
   {
      public int studentID { get; set; }
      public int rollNo { get; set; }
      public int classID { get; set; }
      public string studentName { get; set; }

      public CStudent()
      {
         studentID = 0;
         rollNo = 0;
         classID = 0;
         studentName = "";
      }
   }

   public class CClass
   {
      public int classID { get; set; }
      public int division { get; set; }
      public string className { get; set; }

      public CClass()
      {
         classID = 0;
         division = 0;
         className = "";
      }
   }

   class Program
   {
      static void Main(string[] args)
      {
         List<CClass> tblClasses = new List<CClass>()
         {
            {new CClass(){classID=1, className="English", division=1}},
            {new CClass(){classID=2, className="CompSci", division=2}},
            {new CClass(){classID=3, className="History", division=3}},
            {new CClass(){classID=4, className="Calculus", division=2}},
         };

         List<CStudent> tblStudents = new List<CStudent>()
         {
            {new CStudent() {studentID=1, studentName="Johnson", rollNo=2012, classID=1}},
            {new CStudent() {studentID=1, studentName="Johnson", rollNo=2012, classID=2}},
            {new CStudent() {studentID=2, studentName="Smith", rollNo=2012, classID=1}},
            {new CStudent() {studentID=2, studentName="Smith", rollNo=2012, classID=4}},
         };

         var lstClassParticipant =
         (
            from student in tblStudents
            join course in tblClasses on student.classID equals course.classID
            where student.rollNo.Equals(2012)

            select new
            {
               STUDENT_NAME = student.studentName,
               COURSE = course.className,
               DIVISION = course.division
            }
         ).ToList();


         lstClassParticipant.ForEach(cp =>
            Console.WriteLine("Student: {0} is taking {1} in division {2}",
               cp.STUDENT_NAME, cp.COURSE, cp.DIVISION));

         List<CClass> lstAllClasses =
         (
            from c in tblClasses
            select c
         ).ToList();

         lstAllClasses.ForEach(c => Console.WriteLine(c.className));


         List<CStudent> lstAllStudents =
         (
            from s in tblStudents
            select s
         ).ToList();

         lstAllStudents.ForEach(s => Console.WriteLine(s.studentName));
      }
   }
}

Edited 4 Years Ago by thines01: n/a

Thank You Mr. thines01.

Actually i have created foreign key relations in the 2 tables.
So just by loading one row in the class clsDataClass.tblClass, as it is referenced by studentId of table clsDataClass.tblStudent, this table also should be automatically filled. I am geting the table clsDataClass.tblStudent filled.

But my problem is that how can i use clsDataClass.tblStudent rows in report viewer.

ie., my output should be like this.


Class: XII-A
-------------
Roll No | Student Name |
------------------------------------------
15 | Xavier |
17 | Ronald |
------------------------------------------

for this I have created a report(.rdlc file). added a table, added a text box and given className and division as fields.

and to the rows I have added roll no and student name.


please help me.

Regards,
Jineesh

This article has been dead for over six months. Start a new discussion instead.