Click here to Skip to main content
15,879,490 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have referred following link :

http://www.aspdotnet-suresh.com/2012/03/bind-data-to-gridview-with-jquery-or.html[^]

I have written following code in .asmx file and calling this WebMethod in .aspx file
C#
using JSON:

 [WebMethod]
        public StudentDetails[] GetTodayReport(int StudentID, DateTime startdate)
        {
            try
            {
                context = new TMSEntities();
                List<studentdetails> details = new List<studentdetails>();
                List<string> sdetails = new List<string>();
                DataTable NewTable = new DataTable();
                NewTable.Columns.AddRange(new DataColumn[6] { new DataColumn("Type", typeof(string)), new DataColumn("StartDate", typeof(string)), new DataColumn("Name", typeof(string)), new DataColumn("Status", typeof(string)), new DataColumn("EndDate", typeof(string)), new DataColumn("Course", typeof(string)) });
                var query = (from E in context.tbl_ExamCreation
                             join EA in context.tbl_ExamAttendence on E.Exam_ID equals EA.Exam_ID
                             join C in context.tbl_Course on E.Course_ID equals C.CourseID
                             where EA.StudentID == StudentID && SqlFunctions.DateDiff("DAY", E.DateOfExam, startdate) == 0 && E.Status == "A"
                             select new { Type = "Exam", StartDate = E.DateOfExam, Name = E.ExamName, Status = EA.status, EndDate = E.ExamEndDatetime, Course = C.Name })
                           .Union
                           (from LS in context.tbl_LectureScheduling
                            join LA in context.tbl_LectureAttendence on LS.LectureScheduleID equals LA.LectureScheduleID
                            join L in context.tbl_Lectures on LS.LectureID equals L.LectureID
                            join CR in context.tbl_Course on L.CourseID equals CR.CourseID
                            where LA.StudentID == StudentID && SqlFunctions.DateDiff("DAY", LS.StartDateTime, startdate) == 0 && LS.Status == "A"
                            select new { Type = "Lecture", StartDate = LS.StartDateTime, Name = L.LectureName, Status = LA.status, EndDate = LS.EndDateTime, Course = CR.Name }
                            );
                foreach(var item in query)
                {
                   // Here I want to add above query result into "details" list which is of type   List<studentdetails> 
                }
              
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
    public class StudentDetails
    {
        public string Type{get; set;}
        public string StartDate { get; set; }
        public string Name { get; set; }
        public string Status { get; set; }
        public string EndDate { get; set; }
        public string Course { get; set; }
    }

}

I want to add above query result into "details" list which is of type List<studentdetails> in above for loop. So how can I do the same?
Posted
Updated 11-Sep-14 20:27pm
v2

Replace your query porion with this

C#
List<studentdetails> query = (from E in context.tbl_ExamCreation
                             join EA in context.tbl_ExamAttendence on E.Exam_ID equals EA.Exam_ID
                             join C in context.tbl_Course on E.Course_ID equals C.CourseID
                             where EA.StudentID == StudentID && SqlFunctions.DateDiff("DAY", E.DateOfExam, startdate) == 0 && E.Status == "A"
                             select new { Type = "Exam", StartDate = E.DateOfExam, Name = E.ExamName, Status = EA.status, EndDate = E.ExamEndDatetime, Course = C.Name })
                           .Union
                           (from LS in context.tbl_LectureScheduling
                            join LA in context.tbl_LectureAttendence on LS.LectureScheduleID equals LA.LectureScheduleID
                            join L in context.tbl_Lectures on LS.LectureID equals L.LectureID
                            join CR in context.tbl_Course on L.CourseID equals CR.CourseID
                            where LA.StudentID == StudentID && SqlFunctions.DateDiff("DAY", LS.StartDateTime, startdate) == 0 && LS.Status == "A"
                            select new StudentDetails(){ Type = "Lecture", StartDate = LS.StartDateTime, Name = L.LectureName, Status = LA.status, EndDate = LS.EndDateTime, Course = CR.Name }
                            ).ToList();
 
Share this answer
 
v2
instead of foreach use

C#
return query.Select(x=>new StudentDetails() { Type = x.Type, StartDate = x.StartDate.HasValue ? x.StartDate.Value : DateTime.MinValue,...}).ToArray();
 
Share this answer
 
v2
Comments
m-shraddha 12-Sep-14 4:21am    
public class StudentDetails
{
public string Type{get; set;}
public DateTime StartDate { get; set; }
public string Name { get; set; }
public string Status { get; set; }
public DateTime EndDate { get; set; }
public string Course { get; set; }
}

Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)

When I have tried your code I got above error near x.StartDate and x.EndDate. Please give me the solution
Oleh Zheleznyak 12-Sep-14 5:37am    
Fixed my solution, see the code snippet above - you have to use the Value property of the Nullable<datetime> structure

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