0

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

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

我的代码是

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;

有用。

但是对于孔表,它给出了空对象的错误。

谁能帮我?

4

1 回答 1

0

你可以试试这个:

DateTime? BeginDate = sub.BeginDate.Value
于 2012-12-20T10:57:30.650 回答