This is a migrated thread and some comments may be shown as answers.

Problems with Insert/Update/Delete in MOSS Web Part

3 Answers 67 Views
Scheduler
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 1
Joe asked on 27 May 2009, 03:36 PM
Background:

I have implemented Rad Scheduler in a MOSS 2007 SP1 web part. I have bound the scheduler to an object data source following this example:

http://www.telerik.com/help/aspnet-ajax/schedule_gettingstarted.html

The web part is rendering properly and the scheduler is binding data from the custom data source.

Problem:

On insert, update, and delete, my web part errors and throws the standard SharePoint "an unexpected error has occured" page. I have all my custom code wrapped in try/catch blocks, so I'm pretty sure it's not a problem with my custom code.

Since the scheduler is rendering and pulling the data, I know the control is properly configured (web.config, script manager, etc). I also know that the object data source select method is working - it's pulling the data properly.

To troubleshoot I have commented out all code in the actual InsertTask method - to ensure my custom code is not throwing the error.

So I'm pretty much stumped as to what is causing this. Here is my relavent code:

Data Source Creation - inside OnInit() method

                    ds = new ObjectDataSource();  
                    ds.ID = "dsTaskList";  
                    ds.TypeName = "WIT.TaskList.TaskList";  
                    ds.SelectMethod = "AllData";  
                    ds.SelectParameters.Add("siteurl", _site);  
                    ds.SelectParameters.Add("webname", _web);  
                    ds.SelectParameters.Add("tasklistname", _tasklist);  
                    ds.SelectParameters.Add("viewname", _view);  
                    ds.SelectParameters.Add("startdatefield", _datastarttimefield);  
                    ds.SelectParameters.Add("enddatefield", _dataendtimefield);  
                    ds.SelectParameters.Add("subjectfield", _datasubjectfield);  
                    ds.InsertMethod = "InsertTask";  
                    ds.InsertParameters.Add(new Parameter("Subject", System.Data.DbType.String));  
                    ds.InsertParameters.Add(new Parameter("Start", System.Data.DbType.DateTime));  
                    ds.InsertParameters.Add(new Parameter("End", System.Data.DbType.DateTime));  
                    ds.InsertParameters.Add("siteurl", _site);  
                    ds.InsertParameters.Add("webname", _web);  
                    ds.InsertParameters.Add("tasklistname", _tasklist);  
                    ds.InsertParameters.Add("viewname", _view);  
                    ds.InsertParameters.Add("startdatefield", _datastarttimefield);  
                    ds.InsertParameters.Add("enddatefield", _dataendtimefield);  
                    ds.InsertParameters.Add("subjectfield", _datasubjectfield);  
                    ds.UpdateMethod = "UpdateTask";  
                    ds.UpdateParameters.Add(new Parameter("ID", System.Data.DbType.Int32));  
                    ds.UpdateParameters.Add(new Parameter("Subject", System.Data.DbType.String));  
                    ds.UpdateParameters.Add(new Parameter("Start", System.Data.DbType.DateTime));  
                    ds.UpdateParameters.Add(new Parameter("End", System.Data.DbType.DateTime));  
                    ds.UpdateParameters.Add("siteurl", _site);  
                    ds.UpdateParameters.Add("webname", _web);  
                    ds.UpdateParameters.Add("tasklistname", _tasklist);  
                    ds.UpdateParameters.Add("viewname", _view);  
                    ds.UpdateParameters.Add("startdatefield", _datastarttimefield);  
                    ds.UpdateParameters.Add("enddatefield", _dataendtimefield);  
                    ds.UpdateParameters.Add("subjectfield", _datasubjectfield);  
                    ds.DeleteMethod = "DeleteTask";  
                    ds.DeleteParameters.Add(new Parameter("ID", System.Data.DbType.Int32));  
                    ds.DeleteParameters.Add("siteurl", _site);  
                    ds.DeleteParameters.Add("webname", _web);  
                    ds.DeleteParameters.Add("tasklistname", _tasklist);  
                    ds.DeleteParameters.Add("viewname", _view);  
                    ds.DeleteParameters.Add("startdatefield", _datastarttimefield);  
                    ds.DeleteParameters.Add("enddatefield", _dataendtimefield);  
                    ds.DeleteParameters.Add("subjectfield", _datasubjectfield);  
                    this.Page.Controls.AddAt(1, ds); 

Web Part - CreateChildControls()
                    //new instance of scheduler  
                    schedule = new RadScheduler();  
                    //set scheduler appearance  
                    schedule.Width = Unit.Percentage(100);  
                    schedule.Height = Unit.Percentage(100);  
                    schedule.Skin = _skin;  
                    schedule.SelectedView = _startview;  
                    //set scheduler data properties  
                    schedule.DataSourceID = ds.ID;  
                    schedule.DataKeyField = "_id";  
                    schedule.DataEndField = "_enddatetime";  
                    schedule.DataStartField = "_startdatetime";  
                    schedule.DataSubjectField = "_subject";  
                    //other scheduler properties  
                    schedule.AllowDelete = _allowdelete;  
                    schedule.AllowEdit = _allowedit;  
                    schedule.AllowInsert = _allowinsert;  
                    schedule.EnableAjaxSkinRendering = true;  
                    schedule.DayEndTime = _dayendtime;  
                    schedule.DayStartTime = _daystarttime;  
                    schedule.FirstDayOfWeek = _firstdayofweek;  
                    schedule.WorkDayEndTime = _workdayendtime;  
                    schedule.WorkDayStartTime = _workdaystarttime;  
                    schedule.StartInsertingInAdvancedForm = true;  
                    schedule.StartEditingInAdvancedForm = false;  
                    //add scheduler to control collection  
                    Controls.Add(schedule);  
                    schedule.DataBind(); 

TaskList.cs
        const string TasksKey = "TaskListScheduler_key";  
        public static List<TaskInfo> AllData(string siteurl, string webname, string tasklistname, string viewname, string startdatefield, string enddatefield, string subjectfield)  
        {  
            List<TaskInfo> sessApts = HttpContext.Current.Session[TasksKey] as List<TaskInfo>;  
            if (sessApts == null)  
            {  
                sessApts = new List<TaskInfo>();  
 
                SPSecurity.RunWithElevatedPrivileges(delegate()  
                {  
                    //get task list information  
                    using (SPSite site = new SPSite(siteurl))  
                    {  
                        SPWeb web = site.OpenWeb(webname);  
                        SPList tasklist = web.Lists[tasklistname];  
                        SPView view = tasklist.Views[viewname];  
                        SPListItemCollection lic = tasklist.GetItems(view);  
 
                        foreach (SPListItem item in lic)  
                        {  
                            if (item[startdatefield] != null)  
                            {  
                                if (item[enddatefield] != null)  
                                {  
                                    if (item[subjectfield] != null)  
                                    {  
                                        string subject = Convert.ToString(item[subjectfield]);  
                                        DateTime start = (DateTime)item[startdatefield];  
                                        DateTime end = (DateTime)item[enddatefield];  
 
                                        //if (start <= end)  
                                        //{  
                                        //    if (start == end)  
                                        //    {  
                                        //end = end.AddDays(1);  
                                        TaskInfo ti = new TaskInfo(Convert.ToInt32(item["ID"]), subject, start, end);  
                                        sessApts.Add(ti);  
                                        //    }  
                                        //}  
                                    }  
                                }  
                            }  
                        }  
                    }  
                });  
 
                HttpContext.Current.Session[TasksKey] = sessApts;  
            }  
            return sessApts;  
        }  
        public static void InsertTask(string Subject, DateTime Start, DateTime End, string siteurl, string webname, string tasklistname, string viewname, string startdatefield, string enddatefield, string subjectfield)  
        {  
 
            List<TaskInfo> sessApts = (List<TaskInfo>)HttpContext.Current.Session[TasksKey];  
 
            int ID = 0;  
            //try  
            //{  
            //    SPSecurity.RunWithElevatedPrivileges(delegate()  
            //    {  
            //        using (SPSite site = new SPSite(siteurl))  
            //        {  
            //            //open task list  
            //            SPWeb web = site.OpenWeb(webname);  
            //            SPList tasklist = web.Lists[tasklistname];  
            //            //add new item to list  
            //            SPListItem item = tasklist.Items.Add();  
            //            //get ID  
            //            ID = item.ID;  
            //            //set meta data values  
            //            item[subjectfield] = Subject;  
            //            item[startdatefield] = Start;  
            //            item[enddatefield] = End;  
            //            //update inserted item with meta data  
            //            item.Update();  
            //        }  
            //    });  
            //}  
            //catch (Exception ex) { HttpContext.Current.Response.Write(ex.ToString()); }  
 
            TaskInfo ti = new TaskInfo(ID, Subject, Start, End);  
            sessApts.Add(ti);  
            HttpContext.Current.Session[TasksKey] = sessApts;  
        }  
        public static void DeleteTask(int ID, string siteurl, string webname, string tasklistname, string viewname, string startdatefield, string enddatefield, string subjectfield)  
        {  
            try 
            {  
                List<TaskInfo> sessApts = AllData(siteurl, webname, tasklistname, viewname, startdatefield, enddatefield, subjectfield);  
 
                SPSecurity.RunWithElevatedPrivileges(delegate()  
                {  
                    using (SPSite site = new SPSite(siteurl))  
                    {  
                        //open task list  
                        SPWeb web = site.OpenWeb(webname);  
                        SPList tasklist = web.Lists[tasklistname];  
                        //delete item  
                        tasklist.Items.DeleteItemById(ID);  
                    }  
                });  
 
                sessApts.Remove(FindById(ID, sessApts));  
            }  
            catch (Exception ex) { HttpContext.Current.Response.Write(ex.ToString()); }  
        }  
        public static void UpdateTask(int ID, string Subject, DateTime Start, DateTime End, string siteurl, string webname, string tasklistname, string viewname, string startdatefield, string enddatefield, string subjectfield)  
        {  
            try 
            {  
                List<TaskInfo> sessApts = AllData(siteurl, webname, tasklistname, viewname, startdatefield, enddatefield, subjectfield);  
 
                SPSecurity.RunWithElevatedPrivileges(delegate()  
                {  
                    using (SPSite site = new SPSite(siteurl))  
                    {  
                        //open task list  
                        SPWeb web = site.OpenWeb(webname);  
                        SPList tasklist = web.Lists[tasklistname];  
                        //get list item to update  
                        SPListItem item = tasklist.GetItemById(ID);  
                        //set meta data values  
                        item[subjectfield] = Subject;  
                        item[startdatefield] = Start;  
                        item[enddatefield] = End;  
                        //update item with new meta data  
                        item.Update();  
                    }  
                });  
 
                TaskInfo ti = FindById(ID, sessApts);  
                ti._subject = Subject;  
                ti._startdatetime = Start;  
                ti._enddatetime = End;  
            }  
            catch (Exception ex) { HttpContext.Current.Response.Write(ex.ToString()); }  
        }  
        public static TaskInfo FindById(int ID, List<TaskInfo> sessTasks)  
        {  
            foreach (TaskInfo ti in sessTasks)  
            {  
                if (ti._id == ID)  
                {  
                    return ti;  
                }  
            }  
            return null;  
        } 

TaskInfo.cs
public class TaskInfo  
    {  
        public int _id { getset; }  
        public string _subject { getset; }  
        public DateTime _startdatetime { getset; }  
        public DateTime _enddatetime { getset; }  
 
        public TaskInfo(int id, string subject, DateTime start, DateTime end)   
        {  
            this._id = id;  
            this._subject = subject;  
            this._startdatetime = start;  
            this._enddatetime = end;  
         }  
    } 

Please let me know if I can provide any other information that would be helpful in resolving this issue. Thanks.

3 Answers, 1 is accepted

Sort by
0
Joe
Top achievements
Rank 1
answered on 27 May 2009, 06:13 PM
Sorry, I realized my post was maybe a bit confusing...

I get the error page after the insert/update/delete forms. So the forms (confirmation box on delete) open just fine, the error occurs when the user submits those forms. The error is also generated on any drag and drop. I think something is not getting fired properly either in the scheduler or the object data source insert, update, or delete methods. Thanks again.
0
Dimitar Milushev
Telerik team
answered on 01 Jun 2009, 11:11 AM
Hello Joe,

Can you please open a support ticket send us the web part you are trying to create? I will debug it and will try to find what's causing the problem.

You should also check this blog post about disabling the "unexpected error" page so you can see a more meaningful error message.

Kind regards,
Dimitar Milushev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Joe
Top achievements
Rank 1
answered on 02 Jun 2009, 08:42 PM
Thanks for the info. When I first started developing web parts, I was told it wasn't possible to get rid of that stupid "unexpected error" page. I was always able to output the errors if I used try/catch blocks for everything. In this case, that didn't work. I was able to shut off that page and found it was an error with my object data source. The parameter names weren't exactly the same as the field names in my class. Once I changed that, everything started working.

Thanks again!
Tags
Scheduler
Asked by
Joe
Top achievements
Rank 1
Answers by
Joe
Top achievements
Rank 1
Dimitar Milushev
Telerik team
Share this question
or