开发者

Displaying course details later than today's date

I'm using the VSTA C# on an infopath 2010 form, whereby by using cascading drop downs (Course Title & Course Details) information is displayed.

So when a user selects the Course Title drop down, the Course details is populated with the StartTime, EndTime, Location and Development Category information from a Sharepoint 2010 list.

Now the problem I have is that I want the user to only view the course details for today and onwards, and not view course details for the past. This is the code whereby I display the coursedetails. I've tried declaring a dateTime variable and using it to compare with a string that converts to DateTime with Today, to make it later than the DateTime variable, but it gives me an error after I select a course title, it says "Object Reference not set to an instance of an object". With the troubleshooting tips: "Use the new keyword to create an object i开发者_如何学运维nstance. Check to determine if the object is null before calling the method. Get gengeral help for this exception"

using (web = site.OpenWeb())
            {
                try
                {
                    //SPSecurity.RunWithElevatedPrivileges(new SPSecurity.CodeToRunElevated(delegate()
                    //{
                    SPList lstDocs = web.Lists["Training Calander"] as SPList;

                    string sTitle = "";
                    string sSDate = "";
                    string sEDate = "";
                    string sLocation = "";
                    string SDCategory = "";
                    string CourseDetails = "";
                    //DateTime TodayDate = DateTime.Today;

                    //DateTime dt1 = Convert.ToDateTime(sEDate);




                    if (lstDocs != null) 
                    {
                        SortedList<string, string> lstDetails = new SortedList<string, string>();

                        foreach (SPListItem item in lstDocs.Items)
                        {
                            try
                            {
                                sTitle = item["Title"].ToString();
                                sSDate = item["StartTime"].ToString();
                                sEDate = item["EndTime"].ToString();
                                sLocation = item["Location"].ToString();
                                SDCategory = item["Development Category"].ToString();



                            }
                            catch { }


                            if (sTitle == nValue) //&& (dt >= TodayDate))

                            {
                                try
                                {
                                    CourseDetails = sSDate + " - " + sEDate + " | " + sLocation + " | " + SDCategory;
                                    lstDetails.Add(CourseDetails,CourseDetails);


                                }
                                catch { }
                            }
                        }


I believe the problem is best solved before you execute your foreach loop. You need to create a query that will select only the Items that meet your criteria using a Where clause. They you can iterate through your loop without having to test the date on each pass, which is going to be slower.

Assuming Startdate is stored as a date variable, this should be a trivial query to write.

Apologies if I have misunderstood your issue.


 foreach (SPListItem item in lstDocs.Items.Where(item => item.StartTime.Date >= DateTime.Now.Date))

This is assuming there is a property called StartTime in the SPListItem class and that you are using .NET 3+ and have access to Linq.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜