0

I use Visual Studio 2012 to create some UserControls to be wrapped on SharePoint 2010. The following is code of one of them:

public partial class SideBarUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        liLogout.Visible = false;
        attendance.Visible = false;
        empEvaluation.Visible = false;
        Objections.Visible = false;
        myEval.Visible = false;
        rates.Visible = true;
        if (PageBase.CurrentUser == null)
        {
            string LoginName = SPContext.Current.Web.CurrentUser.LoginName;
            LoginName = LoginName.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries)[1];

            if (SystemUserBLL.IsUserExist(LoginName, "123456"))
            {
                SystemUserBLL.Login(LoginName, "123456");
            }
            else
                Response.Redirect("Login.aspx", true);
        }

        if (new UserDAL().GetUserFieldValue(PageBase.CurrentUser.ID, 51).FieldValue == "الدرجة الثانية")
        {
            rates.Visible = false;
            sysSettings.Visible = false;
            reports.Visible = false;
            string EmployeeType = new UserDAL().GetUserFieldValue(PageBase.CurrentUser.ID, 39).Value;
            myEval.Visible = false;

            if (EmployeeType == "2") // Employee
            {
                if (!Request.Path.ToLower().Contains("rateform.aspx") && !Request.Path.ToLower().Contains("myevaluation.aspx")
                    && !Request.Path.ToLower().Contains("homepage.aspx"))
                    Response.Redirect("MyEvaluation.aspx");
                myEval.Visible = true;

                Objections.Visible = false;
                empEvaluation.Visible = false;
            }
            else if (EmployeeType == "3") // Wakeel
            {
                if (!Request.Path.ToLower().Contains("rateform.aspx") && !Request.Path.ToLower().Contains("objections.aspx") && !Request.Path.ToLower().Contains("userevaluation.aspx")
                    && !Request.Path.ToLower().Contains("homepage.aspx") && !Request.Path.ToLower().Contains("percentages.aspx")
                    && !Request.Path.ToLower().Contains("userattendance.aspx"))
                    Response.Redirect("UserEvaluation.aspx");

                rates.Visible = false;
                Objections.Visible = true;
                empEvaluation.Visible = true;
                attendance.Visible = true;
                myEval.Visible = false;

                sysSettings.Visible = true;
                form.Visible = false;
                waitperiod.Visible = false;
            }
            else // Manager
            {
                if (!Request.Path.ToLower().Contains("rateform.aspx") && !Request.Path.ToLower().Contains("myevaluation.aspx")
                    && !Request.Path.ToLower().Contains("objections.aspx") && !Request.Path.ToLower().Contains("userevaluation.aspx")
                    && !Request.Path.ToLower().Contains("homepage.aspx") && !Request.Path.ToLower().Contains("userattendance.aspx"))
                    Response.Redirect("UserEvaluation.aspx");
                //myEval.Visible = false;

                rates.Visible = false;
                myEval.Visible = true;
                Objections.Visible = true;
                empEvaluation.Visible = true;
                attendance.Visible = true;
            }

            agenciesmanagers.Visible = false;
        }
    }
}

When I try to run it on SharePoint sometimes it runs perfectly and sometimes I have the following error.

(createchildcontrole) = object reference not set to an instance of an object

If the error occurred just refreshing the page solving the problem.

halfer
  • 19,824
  • 17
  • 99
  • 186
Abraham Josef
  • 653
  • 1
  • 8
  • 30
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Charles Mager Apr 29 '15 at 15:32
  • thank you Charles for your quick reply, I think it is not related to normal c# null reference exception, I think it belongs to sharepoint, code runs perfectly outside the context of sharepoint and error occurs just after inject usercontrol in sharepoint – Abraham Josef Apr 29 '15 at 15:35
  • 2
    You will still have to debug it in exactly the same way. – Charles Mager Apr 29 '15 at 15:36
  • I debugged the usercontrol in VisualStudio with no errors, but it occurs on sharePoint environment – Abraham Josef Apr 29 '15 at 15:42

0 Answers0