2
\$\begingroup\$

I inherited this code that saves data to a sharepoint list. This method saves the data to the list. In other parts of the application a list is presented and can be paged through. You then select an item and it loads all the columns and you can modify them and save them. It's taking roughly 25 seconds to save data. Any pointers as to where I could speed things up?

private void SaveFormData(object sender, EventArgs eventArgs)
    {
        try
        {
            if (Page.IsValid)
            {
                Button btnSaveA = (Button)sender;
                TabContainer tbc = ((TabContainer)btnSaveA.Parent.FindControl("tbc"));

                Label lblTab1TR1TC2 = (Label)tbc.Tabs[0].FindControl("lblTR1TC2");

                //tab1 data
                HiddenField hfSaveOrUpdate = (HiddenField)tbc.Tabs[0].FindControl("hfSaveOrUpdate");
                TextBox tbTab1TR3TC2 = (TextBox)tbc.Tabs[0].FindControl("tbTR3TC2");
                TextBox tbTab1TR4TC2 = (TextBox)tbc.Tabs[0].FindControl("tbTR4TC2");
                TextBox tbTab1TR5TC2 = (TextBox)tbc.Tabs[0].FindControl("tbTR5TC2");
                Label lblTab1TR6TC2 = (Label)tbc.Tabs[0].FindControl("lblTR6TC2");
                TextBox tbTab1TR7TC2 = (TextBox)tbc.Tabs[0].FindControl("tbTR7TC2");
                TextBox tbTab1TR8TC2 = (TextBox)tbc.Tabs[0].FindControl("tbTR8TC2");
                TextBox tbTab1TR9TC2 = (TextBox)tbc.Tabs[0].FindControl("tbTR9TC2");
                DropDownList ddlTab1TR11TC2 = (DropDownList)tbc.Tabs[0].FindControl("ddlTR11TC2");
                DropDownList ddlTab1TR12TC2 = (DropDownList)tbc.Tabs[0].FindControl("ddlTR12TC2");
                TextBox tbTab1TR13TC2 = (TextBox)tbc.Tabs[0].FindControl("tbTR13TC2");
                DropDownList ddlTab1TR14TC2 = (DropDownList)tbc.Tabs[0].FindControl("ddlTR14TC2");
                DropDownList ddlTab1TR15TC2 = (DropDownList)tbc.Tabs[0].FindControl("ddlTR15TC2");
                TextBox tbTab1TR16TC2 = (TextBox)tbc.Tabs[0].FindControl("tbTR16TC2");

                //tab2 data
                DropDownList ddlTab2TR1TC2 = (DropDownList)tbc.Tabs[1].FindControl("ddlTR1TC2");
                DropDownList ddlTab2TR2TC2 = (DropDownList)tbc.Tabs[1].FindControl("ddlTR2TC2");
                DropDownList ddlTab2TR3TC2 = (DropDownList)tbc.Tabs[1].FindControl("ddlTR3TC2");
                TextBox tbTab2TR4TC2 = (TextBox)tbc.Tabs[1].FindControl("tbTR4TC2");
                TextBox tbTab2TR5TC2 = (TextBox)tbc.Tabs[1].FindControl("tbTR5TC2");
                TextBox tbTab2TR6TC2 = (TextBox)tbc.Tabs[1].FindControl("tbTR6TC2");
                DropDownList ddlTab2TR7TC2 = (DropDownList)tbc.Tabs[1].FindControl("ddlTR7TC2");
                TextBox tbTab2TR8TC2 = (TextBox)tbc.Tabs[1].FindControl("tbTR8TC2");

                //tab3 data
                DropDownList ddlTab3TR1TC2 = (DropDownList)tbc.Tabs[2].FindControl("ddlTR1TC2");
                DropDownList ddlTab3TR2TC2 = (DropDownList)tbc.Tabs[2].FindControl("ddlTR2TC2");
                DropDownList ddlTab3TR3TC2 = (DropDownList)tbc.Tabs[2].FindControl("ddlTR3TC2");
                TextBox tbTab3TR4TC2 = (TextBox)tbc.Tabs[2].FindControl("tbTR4TC2");
                TextBox tbTab3TR5TC2 = (TextBox)tbc.Tabs[2].FindControl("tbTR5TC2");
                DropDownList ddlTab3TR6TC2 = (DropDownList)tbc.Tabs[2].FindControl("ddlTR6TC2");
                DropDownList ddlTab3TR7TC2 = (DropDownList)tbc.Tabs[2].FindControl("ddlTR7TC2");
                //}

                //tab4 data
                GridView gvHistory = (GridView)tbc.Tabs[3].FindControl("gvHistory");

                //16nov2011 fix. use 'using'
                //SPWeb oWeb = SPControl.GetContextSite(Context).OpenWeb();

                SPSite siteh2 = SPContext.Current.Site;

                //using (SPWeb oWeb = SPControl.GetContextSite(Context).OpenWeb())

                using (SPWeb oWeb = siteh2.OpenWeb())
                {

                #region using

                //String sharePointSite = SPContext.Current.Web.Url;
                //using (SPSite oSite = new SPSite(sharePointSite))
                //{
                //using (SPWeb oWeb = oSite.OpenWeb())
                //{
                //was these two...
                SPList theList = oWeb.Lists["Ideas"];
                SPListItem newListItem = theList.Items.Add();

                //NEW
                SPQuery myQuery = new SPQuery();
                myQuery.Query = "<Where><Eq><FieldRef Name=\"ID\" /><Value Type=\"Counter\">" + lblTab1TR1TC2.Text.ToString() + "</Value></Eq></Where>";

                SPListItemCollection itemToSave = theList.GetItems(myQuery);
                //NEW END

                SPList HistoryList = oWeb.Lists["History"];
                SPListItem HistoryListItem1 = HistoryList.Items.Add();
                SPListItem HistoryListItem2 = HistoryList.Items.Add();
                SPListItem HistoryListItem3 = HistoryList.Items.Add();
                SPListItem HistoryListItem4 = HistoryList.Items.Add();

                if (hfSaveOrUpdate.Value == "Save")
                {
                    //tab1 values
                    newListItem["Date_x0020_on_x0020_Suggestion_x"] = fixDate(tbTab1TR3TC2.Text);
                    newListItem["Employee_x0020_Name"] = tbTab1TR4TC2.Text;
                    newListItem["Staff_x0020_Number"] = tbTab1TR5TC2.Text;
                    newListItem["Office_x0020_Location"] = lblTab1TR6TC2.Text;
                    newListItem["Idea_x0020_For"] = tbTab1TR7TC2.Text;
                    newListItem["Detailed_x0020_Description"] = tbTab1TR8TC2.Text;
                    newListItem["Proposed_x0020_Solution"] = tbTab1TR9TC2.Text;
                    newListItem["Method_x0020_of_x0020_Suggestion"] = ddlTab1TR11TC2.SelectedValue;
                    newListItem["Category"] = ddlTab1TR12TC2.SelectedValue;
                    newListItem["Manager_x0020_Name"] = tbTab1TR13TC2.Text;
                    if (ddlTab1TR14TC2.SelectedValue == "Yes")
                        newListItem["If_x0020_GDI"] = true;
                    else
                        newListItem["If_x0020_GDI"] = false;
                    if (ddlTab1TR15TC2.SelectedValue == "Yes")
                        newListItem["GDI_x0020_Line_x0020_Manager_x00"] = true;
                    else
                        newListItem["GDI_x0020_Line_x0020_Manager_x00"] = false;
                    newListItem["Comment_x0020_for_x0020_GDI"] = tbTab1TR16TC2.Text;

                    newListItem["Tab1_x0020_Logged_x0020_By"] = logonUser;

                    newListItem["Cluster"] = givenOfficeFindClusterID(lblTab1TR6TC2.Text);
                    newListItem["Region"] = givenClusterFindRegionID(givenOfficeFindCluster(lblTab1TR6TC2.Text));

                    newListItem.Update();

                    //HistoryListItem1["Title"] = "Idea logged (" + newListItem.ID.ToString() + ")";
                    HistoryListItem1["Title"] = "Idea logged";
                    HistoryListItem1["Date"] = DateTime.Now.ToLongDateString();
                    HistoryListItem1["IdeaID"] = newListItem.ID.ToString();
                    HistoryListItem1["UserID"] = logonUser;
                    HistoryListItem1.Update();

                    newListItem["Title"] = newListItem.ID.ToString();
                    newListItem.Update();

                    ((Label)btnSaveA.Parent.FindControl("lblFeedback")).Text = "<br>Data Saved!";
                    ((Label)btnSaveA.Parent.FindControl("lblFeedback")).ForeColor = System.Drawing.Color.Green;

                }
                else //save the tab2+3 values - DO AN UPDATE OF AN SPLISTITEM
                {

                    //loop through the litems in the ideas list until we find the right idea
                    //for (int i = 0; i < theList.Items.Count; i++)
                    foreach (SPListItem updateItem in itemToSave)
                    {
                        //SPListItem updateItem = theList.Items[i];

                        //
                        if (updateItem["ID"].ToString() == lblTab1TR1TC2.Text.ToString())
                        {
                            //tab1 values
                            updateItem["Date_x0020_on_x0020_Suggestion_x"] = fixDate(tbTab1TR3TC2.Text);
                            updateItem["Employee_x0020_Name"] = tbTab1TR4TC2.Text;
                            updateItem["Staff_x0020_Number"] = tbTab1TR5TC2.Text;
                            updateItem["Idea_x0020_For"] = tbTab1TR7TC2.Text;
                            updateItem["Detailed_x0020_Description"] = tbTab1TR8TC2.Text;
                            updateItem["Proposed_x0020_Solution"] = tbTab1TR9TC2.Text;
                            updateItem["Method_x0020_of_x0020_Suggestion"] = ddlTab1TR11TC2.SelectedValue;
                            updateItem["Category"] = ddlTab1TR12TC2.SelectedValue;
                            updateItem["Manager_x0020_Name"] = tbTab1TR13TC2.Text;
                            if (ddlTab1TR14TC2.SelectedValue == "Yes")
                                updateItem["If_x0020_GDI"] = true;
                            else
                                updateItem["If_x0020_GDI"] = false;
                            if (ddlTab1TR15TC2.SelectedValue == "Yes")
                                updateItem["GDI_x0020_Line_x0020_Manager_x00"] = true;
                            else
                                updateItem["GDI_x0020_Line_x0020_Manager_x00"] = false;
                            updateItem["Comment_x0020_for_x0020_GDI"] = tbTab1TR16TC2.Text;

                            //tab2 values
                            updateItem["For_x0020_Other_x0020_Directorat"] = ddlTab2TR1TC2.SelectedValue;
                            //updateItem["Additional_x0020_Information_x00"] = ddlTab2TR2TC2.SelectedValue;
                            if (ddlTab2TR2TC2.SelectedValue == "Yes")
                                updateItem["Additional_x0020_Information_x00"] = true;
                            else
                                updateItem["Additional_x0020_Information_x00"] = false;

                            updateItem["Status"] = ddlTab2TR3TC2.SelectedValue;
                            updateItem["If_x0020_Rejected_x0020_Reason_x"] = tbTab2TR4TC2.Text;
                            updateItem["Date_x0020_Reviewed_x0020_by_x00"] = fixDate(tbTab2TR5TC2.Text);
                            updateItem["Comments_x0020_for_x0020_Letter"] = tbTab2TR6TC2.Text;
                            //updateItem["Letter_x0020_Sent"] = ddlTab2TR7TC2.SelectedValue;
                            if (ddlTab2TR7TC2.SelectedValue == "Yes")
                                updateItem["Letter_x0020_Sent"] = true;
                            else
                                updateItem["Letter_x0020_Sent"] = false;

                            if (ddlTab2TR7TC2.SelectedValue == "Yes")
                                updateItem["Date_x0020_Letter_x0020_Sent"] = fixDate(tbTab2TR8TC2.Text);
                            else
                                updateItem["Date_x0020_Letter_x0020_Sent"] = "01/01/1900";

                            //tab3 values
                            updateItem["Project_x0020_Category"] = ddlTab3TR1TC2.SelectedValue;
                            updateItem["Six_x0020_Sigma_x0020_Status"] = ddlTab3TR2TC2.SelectedValue;
                            updateItem["Who_x0020_in_x0020_Six_x0020_Sig"] = ddlTab3TR3TC2.SelectedValue;
                            updateItem["Business_x0020_Impact"] = tbTab3TR4TC2.Text;
                            updateItem["Proposed_x0020_ROI"] = tbTab3TR5TC2.Text;
                            //updateItem["Project_x0020_Acheived_x0020_ROI"] = ddlTab3TR6TC2.SelectedValue;
                            if (ddlTab3TR6TC2.SelectedValue == "Yes")
                                updateItem["Project_x0020_Acheived_x0020_ROI"] = true;
                            else
                                updateItem["Project_x0020_Acheived_x0020_ROI"] = false;
                            updateItem["Certification_x0020_Achieved"] = ddlTab3TR7TC2.SelectedValue;

                            //updateItem["Tab2and3_x0020_Logged_x0020_By"] = logonUser;


                            bool tab2FormValueDifferentToListValue = false;
                            bool tab3FormValueDifferentToListValue = false;
                            ideaID = updateItem["ID"].ToString();




                            string query = "<Query></Query>";
                            DataTable spDataTable = ExecuteCamlQuery("Ideas", query);
                            string whereClause = "ID = '" + ideaID + "'";
                            DataRow[] results = spDataTable.Select(whereClause);

                            if (results.Length == 0)
                            {
                                //return "";
                            }
                            else
                            {
                                foreach (DataRow dr in results)
                                {


                                    if (cleanValue(dr["For_x0020_Other_x0020_Directorat"].ToString()) != ddlTab2TR1TC2.SelectedValue)
                                        tab2FormValueDifferentToListValue = true;
                                    if (convertYesNo(cleanValue(dr["Additional_x0020_Information_x00"].ToString())) != ddlTab2TR2TC2.SelectedValue)
                                        tab2FormValueDifferentToListValue = true;
                                    if (cleanValue(dr["Status"].ToString()) != ddlTab2TR3TC2.SelectedValue)
                                        tab2FormValueDifferentToListValue = true;
                                    if (cleanValue(dr["If_x0020_Rejected_x0020_Reason_x"].ToString()) != tbTab2TR4TC2.Text)
                                        tab2FormValueDifferentToListValue = true;
                                    try
                                    {
                                        if (cleanValue(dr["Date_x0020_Reviewed_x0020_by_x00"].ToString()).Substring(0, 10) != tbTab2TR5TC2.Text)
                                            tab2FormValueDifferentToListValue = true;
                                    }
                                    catch (Exception)
                                    {
                                    }
                                    if (cleanValue(dr["Comments_x0020_for_x0020_Letter"].ToString()) != tbTab2TR6TC2.Text)
                                        tab2FormValueDifferentToListValue = true;
                                    if (convertYesNo(cleanValue(dr["Letter_x0020_Sent"].ToString())) != ddlTab2TR7TC2.SelectedValue)
                                        tab2FormValueDifferentToListValue = true;
                                    if (cleanValue(dr["Date_x0020_Letter_x0020_Sent"].ToString()) != tbTab2TR8TC2.Text)
                                    {
                                        if (cleanValue(dr["Date_x0020_Letter_x0020_Sent"].ToString()) != "01/01/1900 00:00:00")
                                            tab2FormValueDifferentToListValue = true;
                                    } 
                                    if (cleanValue(dr["Project_x0020_Category"].ToString()) != ddlTab3TR1TC2.SelectedValue)
                                        tab3FormValueDifferentToListValue = true;
                                    if (cleanValue(dr["Six_x0020_Sigma_x0020_Status"].ToString()) != ddlTab3TR2TC2.SelectedValue)
                                        tab3FormValueDifferentToListValue = true;
                                    if (cleanValue(dr["Who_x0020_in_x0020_Six_x0020_Sig"].ToString()) != ddlTab3TR3TC2.SelectedValue)
                                        tab3FormValueDifferentToListValue = true;
                                    if (cleanValue(dr["Business_x0020_Impact"].ToString()) != tbTab3TR4TC2.Text)
                                        tab3FormValueDifferentToListValue = true;
                                    if (cleanValue(dr["Proposed_x0020_ROI"].ToString()) != tbTab3TR5TC2.Text)
                                        tab3FormValueDifferentToListValue = true;
                                    if (convertYesNo(cleanValue(dr["Project_x0020_Acheived_x0020_ROI"].ToString())) != ddlTab3TR6TC2.SelectedValue)
                                        tab3FormValueDifferentToListValue = true;
                                    //if (getValueFromList("Ideas", "ROI_x0020_Acheived_x0020_Details", "ID", ideaID) != ddlTab3TR7TC2.SelectedValue)
                                    if (cleanValue(dr["Certification_x0020_Achieved"].ToString()) != ddlTab3TR7TC2.SelectedValue)
                                        tab3FormValueDifferentToListValue = true;


                                }
                            }






                            if ((tab2FormValueDifferentToListValue == true) && (tab3FormValueDifferentToListValue == true))
                            {
                                updateItem["Tab2_x0020_Logged_x0020_By"] = logonUser;
                                updateItem["Tab3_x0020_Logged_x0020_By"] = logonUser;
                                HistoryListItem2["Title"] = "Tab 2 and 3 updated.";
                            }
                            else if (tab2FormValueDifferentToListValue == true)
                            {
                                updateItem["Tab2_x0020_Logged_x0020_By"] = logonUser;
                                HistoryListItem2["Title"] = "Tab 2 updated.";
                            }
                            else if (tab3FormValueDifferentToListValue == true)
                            {
                                updateItem["Tab3_x0020_Logged_x0020_By"] = logonUser;
                                HistoryListItem2["Title"] = "Tab 3 updated.";
                            }

                            if ((tab2FormValueDifferentToListValue == true) || (tab3FormValueDifferentToListValue == true))
                            {
                                HistoryListItem2["Date"] = DateTime.Now.ToLongDateString();
                                HistoryListItem2["IdeaID"] = lblTab1TR1TC2.Text.ToString();
                                HistoryListItem2["UserID"] = logonUser;
                                HistoryListItem2.Update();

                                gvHistory.DataSource = getHistory();
                                gvHistory.DataBind();
                            }




                            updateItem.Update();


                            ((Label)btnSaveA.Parent.FindControl("lblFeedback")).Text = "<br>Data Updated!";
                            ((Label)btnSaveA.Parent.FindControl("lblFeedback")).ForeColor = System.Drawing.Color.Green;

                            //((GridView)btnSaveA.Parent.Parent.FindControl("gvIdeas")).DataSource = getIdeas("Ideas", logonUser, getValueFromList("Users", "Access_x0020_Level", "Username", logonUser), getValueFromList("Users", "Office", "Username", logonUser), "Created", "Descending", "", "", "", "", "", "", "");
                            //((GridView)btnSaveA.Parent.Parent.FindControl("gvIdeas")).DataBind();



                            //ImageButton filterButton = (ImageButton)sender;
                            GridView gvIdeas = ((GridView)btnSaveA.Parent.FindControl("gvIdeas"));
                            TextBox filterIdeaID = ((TextBox)btnSaveA.Parent.FindControl("filterIdeaID"));
                            DropDownList filterOffice = ((DropDownList)btnSaveA.Parent.FindControl("filterOffice"));
                            TextBox filterFormDateFrom = ((TextBox)btnSaveA.Parent.FindControl("filterFormDateFrom"));
                            TextBox filterFormDateTo = ((TextBox)btnSaveA.Parent.FindControl("filterFormDateTo"));
                            DropDownList filterCategory = ((DropDownList)btnSaveA.Parent.FindControl("filterCategory"));
                            //DropDownList filterManagerName = ((DropDownList)filterButton.Parent.FindControl("filterManagerName"));
                            DropDownList filterGDI = ((DropDownList)btnSaveA.Parent.FindControl("filterGDI"));
                            //DropDownList filterGDIApproved = ((DropDownList)filterButton.Parent.FindControl("filterGDIApproved"));
                            DropDownList filterLetterSent = ((DropDownList)btnSaveA.Parent.FindControl("filterLetterSent"));
                            Label existingIdeas = ((Label)btnSaveA.Parent.FindControl("existingIdeas"));


                            ViewState["filterIdeaID" + this.ID.ToString()] = filterIdeaID.Text;
                            ViewState["filterOffice" + this.ID.ToString()] = filterOffice.SelectedValue;
                            ViewState["filterFormDateFrom" + this.ID.ToString()] = filterFormDateFrom.Text;
                            ViewState["filterFormDateTo" + this.ID.ToString()] = filterFormDateTo.Text;
                            ViewState["filterCategory" + this.ID.ToString()] = filterCategory.SelectedValue;
                            //ViewState["filterManagerName"] = filterManagerName.SelectedValue;
                            ViewState["filterGDI" + this.ID.ToString()] = filterGDI.SelectedValue;
                            ViewState["filterLetterSent" + this.ID.ToString()] = filterLetterSent.SelectedValue;
                            //ViewState["filterGDIApproved"] = filterGDIApproved.SelectedValue;

                            //gvIdeas.DataSource = getIdeas("Ideas", logonUser, getValueFromList("Users", "Access_x0020_Level", "Username", logonUser), getValueFromList("Users", "Office", "Username", logonUser), "Created", "Descending", ViewState["filterIdeaID"].ToString(), ViewState["filterOffice"].ToString(), ViewState["filterFormDateFrom"].ToString(), ViewState["filterFormDateTo"].ToString(), ViewState["filterCategory"].ToString(), ViewState["filterGDI"].ToString(), ViewState["filterLetterSent"].ToString());
                            //gvIdeas.DataBind();

                            string sortColumn;
                            string sortDirection;

                            if (ViewState["SortExpression" + this.ID.ToString()] == null)
                                sortColumn = "ID";
                            else
                                sortColumn = ViewState["SortExpression" + this.ID.ToString()].ToString();

                            if (ViewState["SortDirection" + this.ID.ToString()] == null)
                                sortDirection = "DESC";
                            else
                                sortDirection = ViewState["SortDirection" + this.ID.ToString()].ToString();

                            if (sortDirection == "Ascending")
                                sortDirection = "ASC";
                            if (sortDirection == "Descending")
                                sortDirection = "DESC";

                            if (sortColumn == "Office")
                                sortColumn = "Office_x0020_Location";
                            if (sortColumn == "FormDate")
                                sortColumn = "Date_x0020_on_x0020_Suggestion_x";
                            if (sortColumn == "EmployeeName")
                                sortColumn = "Employee_x0020_Name";
                            if (sortColumn == "IdeaFor")
                                sortColumn = "Idea_x0020_For";
                            if (sortColumn == "IfGDI")
                                sortColumn = "If_x0020_GDI";
                            if (sortColumn == "LetterSent")
                                sortColumn = "Letter_x0020_Sent";

                            DataTable matchingData = new DataTable();
                            if(applyDefaultValueToFilter)
                                matchingData = getIdeas("Ideas", logonUser, getValueFromList("Users", "Access_x0020_Level", "Username", logonUser), getValueFromList("Users", "Office", "Username", logonUser), "Created", "Descending", ViewState["filterIdeaID" + this.ID.ToString()].ToString(), ViewState["filterOffice" + this.ID.ToString()].ToString(), ViewState["filterFormDateFrom" + this.ID.ToString()].ToString(), ViewState["filterFormDateTo" + this.ID.ToString()].ToString(), ViewState["filterCategory" + this.ID.ToString()].ToString(), ViewState["filterGDI" + this.ID.ToString()].ToString(), ViewState["filterLetterSent" + this.ID.ToString()].ToString(), dataToFilterByValue);
                            else
                                matchingData = getIdeas("Ideas", logonUser, getValueFromList("Users", "Access_x0020_Level", "Username", logonUser), getValueFromList("Users", "Office", "Username", logonUser), "Created", "Descending", ViewState["filterIdeaID" + this.ID.ToString()].ToString(), ViewState["filterOffice" + this.ID.ToString()].ToString(), ViewState["filterFormDateFrom" + this.ID.ToString()].ToString(), ViewState["filterFormDateTo" + this.ID.ToString()].ToString(), ViewState["filterCategory" + this.ID.ToString()].ToString(), ViewState["filterGDI" + this.ID.ToString()].ToString(), ViewState["filterLetterSent" + this.ID.ToString()].ToString(), "");
                            matchingData.DefaultView.Sort = sortColumn + " " + sortDirection;

                            //gvIdeas.PageIndex = e.NewPageIndex;
                            gvIdeas.DataSource = matchingData;
                            gvIdeas.DataBind();
                            HttpContext.Current.Response.Write("gvIdeas.DataBind 1<br>");

                            //((GridView)btnSaveA.Parent.Parent.FindControl("gvIdeas")).DataSource = getIdeas("Ideas", logonUser, getValueFromList("Users", "Access_x0020_Level", "Username", logonUser), getValueFromList("Users", "Office", "Username", logonUser), "Created", "Descending", "", "", "", "", "", "", "");
                            //((GridView)btnSaveA.Parent.Parent.FindControl("gvIdeas")).DataSource = getIdeas("Ideas", logonUser, getValueFromList("Users", "Access_x0020_Level", "Username", logonUser), getValueFromList("Users", "Office", "Username", logonUser), "Created", "Descending", ViewState["filterIdeaID"].ToString(), ViewState["filterOffice"].ToString(), ViewState["filterFormDateFrom"].ToString(), ViewState["filterFormDateTo"].ToString(), ViewState["filterCategory"].ToString(), ViewState["filterGDI"].ToString(), ViewState["filterLetterSent"].ToString());
                            //((GridView)btnSaveA.Parent.Parent.FindControl("gvIdeas")).PageIndex = e.NewPageIndex;
                            ((GridView)btnSaveA.Parent.Parent.FindControl("gvIdeas")).DataSource = matchingData;
                            ((GridView)btnSaveA.Parent.Parent.FindControl("gvIdeas")).DataBind();


                        }
                    }
                    //}
                    //}
                }

                //hide the tab container after new idea logged or current one is updated
                //((TabContainer)btnSave1.Parent.FindControl("tbc")).Visible = false;
                //((Label)btnSave1.Parent.FindControl("lblFeedback")).Visible = true;
                if (((Label)btnSaveA.Parent.FindControl("lblFeedback")).Text == "<br>Data Saved!")
                    ((TabContainer)btnSaveA.Parent.FindControl("tbc")).Visible = false;
                else
                    ((TabContainer)btnSaveA.Parent.FindControl("tbc")).Visible = true;

                ((Label)btnSaveA.Parent.FindControl("lblFeedback")).Visible = true;
                //((Label)btnSave1.Parent.FindControl("addOrEdit")).Visible = false;
                //btnSave1.Visible = false;

                //((GridView)btnSave.Parent.FindControl("gvIdeas")).DataSource = getIdeas("Ideas", logonUser, getValueFromList("Users", "Access_x0020_Level", "Title", getFullnameFromLogonUser(logonUser)), getValueFromList("Users", "Office", "Title", getFullnameFromLogonUser(logonUser)));
                //((GridView)btnSave.Parent.FindControl("gvIdeas")).DataBind();

                if (addOrEdit == AddOrEditEnum.Edit)
                {
                    GridView gvIdeas2 = ((GridView)btnSaveA.Parent.FindControl("gvIdeas"));
                    gvIdeas2.Visible = true;
                    Table filterTable = ((Table)btnSaveA.Parent.FindControl("filterTable"));
                    filterTable.Visible = true;
                    tbc.Visible = false;
                }

                #endregion using

                } //end using

            }
            else
            {
                //((Label)btnSave1.Parent.FindControl("lblFeedback")).Text = "<br>Invalid Form Data. Data NOT Saved!";
                //((Label)btnSave1.Parent.FindControl("lblFeedback")).ForeColor = System.Drawing.Color.Red;
                ((Label)btnSaveA.Parent.FindControl("lblFeedback")).Text = "<br>Invalid Form Data. Data NOT Saved!";
                ((Label)btnSaveA.Parent.FindControl("lblFeedback")).ForeColor = System.Drawing.Color.Red;
            }

        }
        catch (Exception ex)
        {
            //((Label)btnSave1.Parent.FindControl("lblFeedback")).Text = "<br>ERROR : Data NOT Saved!";
            //((Label)btnSave1.Parent.FindControl("lblFeedback")).ForeColor = System.Drawing.Color.Red;
            ((Label)btnSaveA.Parent.FindControl("lblFeedback")).Text = "<br>ERROR : Data NOT Saved!";
            ((Label)btnSaveA.Parent.FindControl("lblFeedback")).ForeColor = System.Drawing.Color.Red;
            throw;
        }



        System.Web.HttpContext.Current.Response.Redirect(System.Web.HttpContext.Current.Request.Url.ToString());

        //}
    }
\$\endgroup\$
  • \$\begingroup\$ Debug it, profile it and tell us what exact part is taking most of the time. But to start with, a function should do a single thing(in this case save data) and be as short as posible, a 448 line function is a VERY long function, split it up into GetControls, place the big if-else body into different functions, same on the foreach ...etc \$\endgroup\$ – null Sep 3 '16 at 10:54
  • \$\begingroup\$ I'll try to attack this properly later, but my initial impression is that you have a call to DataBind within a foreach, which is usually a bad sign. If getHistory and/or getIdeas are database calls then you've got multiple database round-trips. \$\endgroup\$ – Eterm Sep 5 '16 at 19:25
2
\$\begingroup\$

Regarding performance, here's a major design flaw:

string query = "<Query></Query>";
DataTable spDataTable = ExecuteCamlQuery("Ideas", query);
string whereClause = "ID = '" + ideaID + "'";
DataRow[] results = spDataTable.Select(whereClause);

This code is telling SharePoint to retrieve every item in the list, forcing all of the returned items into an in-memory DataTable. Then it's manually filtering that table down to just one specific item.

This is inefficient for two reasons:

  1. In the CAML query that we pass to SharePoint, we can specify filter conditions to retrieve only the items we want, so that SharePoint can efficiently give us that one item we're interested in.
  2. We already performed that query; this code is even inside an If block that verified that we found the item that matches the desired ideaID

Instead of requerying for that item and checking its values to set the tabXFormValueDifferentToListValue Boolean flags, we can just set those flags earlier in the code, when we're first accessing the values of the list item (using the updateItem variable).

if (cleanValue(updateItem["For_x0020_Other_x0020_Directorat"].ToString()) != ddlTab2TR1TC2.SelectedValue)
    tab2FormValueDifferentToListValue = true;
updateItem["For_x0020_Other_x0020_Directorat"] = ddlTab2TR1TC2.SelectedValue;
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.