无法从数据库中获取一些数据

时间:2012-12-20 10:47:38

标签: linq sql-server-2008 c#-4.0

我正在使用VS 2010和SQL Server 2008。

我不断收到“Null对象必须有值”的错误。

我的代码是

public DataTable GetProgressOverview()
    {
        DataTable dtProgressOverview = new DataTable();
        dtProgressOverview.Columns.Add("studentName", typeof(string));
        dtProgressOverview.Columns.Add("lessonName", typeof(string));
        dtProgressOverview.Columns.Add("BeginDate", typeof(string));
        dtProgressOverview.Columns.Add("EndDate", typeof(string));
        dtProgressOverview.Columns.Add("EmailAddress", typeof(string));
        dtProgressOverview.Columns.Add("teacherName", typeof(string));
        dtProgressOverview.Columns.Add("numberOffUnits", typeof(int));
        dtProgressOverview.Columns.Add("numberOffFilledUnits", typeof(int));
        dtProgressOverview.Columns.Add("percentage", typeof(double));
        dtProgressOverview.Columns.Add("subscriptionID", typeof(Guid));
        dtProgressOverview.Columns.Add("studentID", typeof(Guid));
        dtProgressOverview.Columns.Add("teacherID", typeof(Guid));
        dtProgressOverview.Columns.Add("YearName", typeof(string));
        dtProgressOverview.Columns.Add("YearID", typeof(Guid));
        dtProgressOverview.Columns.Add("lessonID", typeof(Guid));
        dtProgressOverview.Columns.Add("status", typeof(bool));
        dtProgressOverview.Columns.Add("CompanyID", typeof(Guid));
        dtProgressOverview.Columns.Add("SubCompanyID", typeof(Guid));
        dtProgressOverview.Columns.Add("CompanyName", typeof(string));
        dtProgressOverview.Columns.Add("SubCompanyName", typeof(string));
        dtProgressOverview.Columns.Add("MentorID", typeof(Guid));
        dtProgressOverview.Columns.Add("MentorName", typeof(string));


        var progressOverview = from pg in Db.viewGetProgressOverviews
                               //where pg.CompanyID.Equals("0a9438cc-c188-46d6-9e1e-08bc7afcb0c7")
                               orderby pg.yearName
                               select pg;

        foreach (var sub in progressOverview)
        {
            Guid CompanyID = Guid.Empty;
            Guid SubCompanyID = Guid.Empty;
            Guid MentorId = Guid.Empty;
            string MentorName = string.Empty;
            string studentName = sub.studentName;
            string lessonName = sub.lessonName;
            DateTime BeginDate = sub.BeginDate.Value;
            DateTime EndDate = sub.EndDate.Value;
            string EmailAddress = sub.EmailAddress;
            string teacherName = sub.teacherName;
            int numberOffUnits = sub.numberOffUnits.Value;
            int numberOffFilledUnits = sub.numberOffFilledUnits.Value;
            decimal percentage = Math.Round(sub.percentage.Value, 2);
            Guid subscriptionID = sub.subscriptionID.Value;
            Guid studentID = sub.studentID.Value;
            Guid teacherID = sub.TeacherID.Value;
            string yearName = sub.yearName;
            Guid yearID = sub.yearID.Value;
            Guid lessonID = sub.lessonID.Value;
            bool status = sub.Status.Value;
            string CompanyName = string.Empty;
            string SubCompanyName = string.Empty;
            if (sub.CompanyID != null)
            {
                CompanyID = sub.CompanyID.Value;
                CompanyName = sub.CompanyName;
            }

            if (sub.SubCompanyID != null)
            {
                SubCompanyID = sub.SubCompanyID.Value;
                SubCompanyName = sub.SubCompanyName;
            }
            if (sub.MentorID != null)
            {
                MentorId = sub.MentorID.Value;
                MentorName = sub.mentorName;
            }


            dtProgressOverview.Rows.Add(studentName,
                lessonName,
                BeginDate.ToShortDateString(),
                EndDate.ToShortDateString(),
                EmailAddress,
                teacherName,
                numberOffUnits,
                numberOffFilledUnits,
                percentage,
                subscriptionID,
                studentID,
                teacherID,
                yearName,
                yearID,
                lessonID,
                status,
                CompanyID,
                SubCompanyID,
                CompanyName,
                SubCompanyName,
                MentorId,
                MentorName
                );
        }

        return dtProgressOverview;

    }

当我调试它时,它在行

中显示错误
DateTime BeginDate = sub.BeginDate.Value;

但我认为错误是因为表没有返回任何记录我不知道为什么..

当我更改查询时

 var progressOverview = from pg in Db.viewGetProgressOverviews
                                   where pg.CompanyID.Equals("0a9438cc-c188-46d6-9e1e-08bc7afcb0c7")
                                   orderby pg.yearName
                                   select pg;

它有效。

但对于孔表,它会给出null对象的错误。

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

DateTime? BeginDate = sub.BeginDate.Value