When loading user control getting Error

Hi,
We have created user controls(.ascx)
When I click on Submit button in my page, I have to get dropdownlist values and I have to bind data to the gridview.

Some times we are getting below below error

"An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below."
 Again we refresh the page it's loding the page and displaying data in gridview.

See the attached screen shot.

Thanks in advance....

 
December 23rd, 2014 10:08am

Hi Reddy416,

Looks like you might be having data bind issue.

Thanks,

Vivek

Free Windows Admin Tool Kit Click here and download it now
December 23rd, 2014 10:13am

hi

without code of your control it is hard to say what causes the issue. But what exception shows is that it is thrown from Microsoft.SharePoint.WebControls.DeltaPage.LoadPageStateFromPersistenceMedium. You didn't mention on what Sharepoint version you work, but according to documentation DeltaPage is new class in SP2013. In this case as quick solution try to disable Minimal download strategy feature of web scope on your site. There will be less problems.

December 23rd, 2014 10:49am

Hi,

We are using SP 2013 version. 

Free Windows Admin Tool Kit Click here and download it now
December 23rd, 2014 12:03pm

Hi,

Based on the error message, it seems some object was not be instantiated.

For a better troubleshooting, I suggest you do as the followings:

1. Debug your code step by step to see if all the variable and objects are valid.

2. based on your description, it works properly when you refresh the page, so some objects not load correctly in the first page load. I suggest you check the detailed error line when the page throw the exception.

Here is a code snippet for your reference:

DataTable table = new DataTable();
table.Columns.Add("DropDownListValue", typeof(string));
DataRow row;
row = table.Rows.Add();
row["EmpName"] = dropdownlist1.DataValue.ToString();
GridView1.DataSource = table.DefaultView;
GridView1.DataBind();

Best Regards

December 24th, 2014 9:45am

Hi,

I am posting my code.Can you please check once. Still i am getting same error.

 protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                BindMonth();
                GetCurrentMonth();
                DataTable dt = Griddata();
                if (dt != null)
                {
                    DataTable dtcamltest = dt;
                    DataView dtview = new DataView(dtcamltest);
                    DataTable dtdistinct = dtview.ToTable(true, "UserName", "V_FirtsName", "V_LastName");
                    if (dtdistinct != null)
                    {
                        ViewState["Data"] = dtdistinct;
                        GvBindData.DataSource = dtdistinct;
                        GvBindData.DataBind();
                    }


                }
            }
        }
        catch(Exception ex)
        {
            Response.Write(ex);
        }
       
    }
    private void GetCurrentMonth()
    {
        DateTime dt = DateTime.Now;
        string month = DateTime.Now.ToString("MMMM");
        int year = dt.Year;

        ddlMonth.SelectedItem.Text = month + " " + year;
    }
    decimal MonthlyHours=0;
    decimal MonthlyTotal=0;
    decimal total;
    decimal yearlyTotal = 0;
    protected void GvBindData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        string ddlvalue = ddlMonth.SelectedItem.Text.Trim();
        
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string userName = ((System.Web.UI.WebControls.Label)e.Row.FindControl("lblUserName")).Text;
            Label lbfirstname = ((System.Web.UI.WebControls.Label)e.Row.FindControl("fstname"));
            Label lblastName = ((System.Web.UI.WebControls.Label)e.Row.FindControl("lstname"));
            Label lblHours = ((System.Web.UI.WebControls.Label)e.Row.FindControl("HOURS"));
            string s = ((System.Web.UI.WebControls.Label)e.Row.FindControl("HOURS")).Text;
            string ID = ((System.Web.UI.WebControls.Label)e.Row.FindControl("lblid")).Text;
            string Month = ((System.Web.UI.WebControls.Label)e.Row.FindControl("lblMonth")).Text;
            DataTable dtUsers = GetMonthHours(userName, ddlvalue);
            if(dtUsers!=null)
            {
                if(dtUsers.Rows.Count>0)
                {
                    for(int i=0;i<dtUsers.Rows.Count;i++)
                    {
                        MonthlyHours +=Convert.ToDecimal( dtUsers.Rows[i]["Hours"]);
                    }
                    //lbfirstname.Text = dtUsers.Rows[0]["V_FirtsName"].ToString();
                    //    lblastName.Text = dtUsers.Rows[0]["V_LastName"].ToString();
                }
            }
            lblHours.Text = MonthlyHours.ToString();
            MonthlyTotal += MonthlyHours;
            MonthlyHours = 0;
            string dept = "Docent Speakers Bureau";
           string month = ddlMonth.SelectedItem.Text.Trim();
            System.Web.UI.WebControls.Button btnEdit = (System.Web.UI.WebControls.Button)e.Row.FindControl("btnDetails");
            btnEdit.PostBackUrl = "/Pages/Details.aspx?CustomID=" + userName + "&Department=" + dept + "&Month=" + month;
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            System.Web.UI.WebControls.Label lblTotal = (System.Web.UI.WebControls.Label)e.Row.FindControl("lblTotal");
            lblTotal.Text = MonthlyTotal.ToString();

            System.Web.UI.WebControls.Label lblYearly = (System.Web.UI.WebControls.Label)e.Row.FindControl("lblYearly");
            
           string s = ddlMonth.SelectedItem.Text;
           string[] year = s.Split(' ');

           if (!string.IsNullOrEmpty(year[1].ToString()))
           {
               DataTable dtyear = GetYearlyData(year[1].ToString());
                if (dtyear != null)
               {

                   if (dtyear.Rows.Count > 0)
                   {
                      foreach (DataRow dr in dtyear.Rows)
                       {
                         yearlyTotal += Convert.ToDecimal(dr["Hours"]);
                       }
                   }
              }
           }
           lblYearly.Text = yearlyTotal.ToString();
        }
    }
    private void BindMonth()
    {
        int Current_year;
        Current_year = DateTime.Now.Year;
        for (int i = 1999; i <= Current_year; i++)
        {
            for (int j = 1; j <= 12; j++)
            {
                if (j == 1)
                {
                    ListItem LI = new ListItem("January " + i.ToString());
                    ddlMonth.Items.Add(LI);

                }
                if (j == 2)
                {
                    ListItem LI = new ListItem("Feburary " + i.ToString());
                    ddlMonth.Items.Add(LI);

                }
                if (j == 3)
                {
                    ListItem LI = new ListItem("March " + i.ToString());
                    ddlMonth.Items.Add(LI);

                }

                if (j == 4)
                {
                    ListItem LI = new ListItem("April " + i.ToString());
                    ddlMonth.Items.Add(LI);

                }
                if (j == 5)
                {
                    ListItem LI = new ListItem("May " + i.ToString());
                    ddlMonth.Items.Add(LI);

                }
                if (j == 6)
                {
                    ListItem LI = new ListItem("June " + i.ToString());
                    ddlMonth.Items.Add(LI);

                }
                if (j == 7)
                {
                    ListItem LI = new ListItem("July " + i.ToString());
                    ddlMonth.Items.Add(LI);

                }
                if (j == 8)
                {
                    ListItem LI = new ListItem("August " + i.ToString());
                    ddlMonth.Items.Add(LI);

                }

                if (j == 9)
                {
                    ListItem LI = new ListItem("September " + i.ToString());
                    ddlMonth.Items.Add(LI);

                }
                if (j == 10)
                {
                    ListItem LI = new ListItem("October " + i.ToString());
                    ddlMonth.Items.Add(LI);

                }
                if (j == 11)
                {
                    ListItem LI = new ListItem("November " + i.ToString());
                    ddlMonth.Items.Add(LI);

                }
                if (j == 12)
                {
                    ListItem LI = new ListItem("December " + i.ToString());
                    ddlMonth.Items.Add(LI);
                }
            }
        }
    }
    private DataTable Griddata()
    {
        DataTable dt = new DataTable();
        using (SPSite site = new SPSite(SPContext.Current.Site.Url))
        {
            using (SPWeb web = site.OpenWeb())
            {
                SPList list = web.Lists["TimeSheet"];
                string ddvalue = ddlMonth.SelectedItem.Text;
                SPQuery query = new SPQuery();
                query.Query = "<Where><And><Eq><FieldRef Name='Month_x0026_Year'/><Value Type='Text'>" + ddvalue.Trim() + "</Value></Eq><Eq><FieldRef Name='Department'/><Value Type='Text'>Docent Speakers Bureau</Value></Eq></And></Where>";
                query.ViewFields = string.Concat("<FieldRef Name='V_FirtsName'/>",
                                      "<FieldRef Name='V_LastName'/>",
                                      "<FieldRef Name='UserName'/>", "<FieldRef Name='Hours'/>", "<FieldRef Name='Month_x0026_Year'/>");
                dt = list.GetItems(query).GetDataTable();
                if (dt != null)
                {
                    if (dt.Rows.Count > 0)
                    {
                    }
                }
            }
        }
        return dt;
    }
    private DataTable GetHours(string username)
    {
        DataTable dt;
        using (SPSite site = new SPSite(SPContext.Current.Site.Url))
        {
            using (SPWeb web = site.OpenWeb())
            {
                SPList listTime = web.Lists["TimeSheet"];
                string ddvalue = ddlMonth.SelectedItem.Text;
                SPQuery query = new SPQuery();
                query.Query = "<Where><And><Eq><FieldRef Name='UserName'/><Value Type='Text'>" + username.Trim() + "</Value></Eq><Eq><FieldRef Name='Department' /><Value Type='Text'>Docent Speakers Bureau</Value></Eq></And></Where>";
                query.ViewFields = string.Concat(
                                      "<FieldRef Name='Hours'/>");
                dt = listTime.GetItems(query).GetDataTable();
            }
        }
        return dt;
    }
    protected void btnSubmuit_Click(object sender, EventArgs e)
    {
        try
        {
            GvBindData.DataSource = null;
            GvBindData.DataBind();
            DataTable dt = Griddata();
            if (dt != null)
            {
                DataTable dtcamltest = dt;
                DataView dtview = new DataView(dtcamltest);
                DataTable dtdistinct = dtview.ToTable(true, "UserName", "V_FirtsName", "V_LastName");

                if (dtdistinct != null)
                {
                    GvBindData.DataSource = dtdistinct;
                    GvBindData.DataBind();
                }
            }
        }
        catch(Exception ex)
        {
            Response.Write(ex);
        }
       
    }
    private DataTable GetYearlyData(string year)
    {
        DataTable dt = null;
        using (SPSite osite = new SPSite(SPContext.Current.Web.Url))
        {
            using (SPWeb oweb = osite.OpenWeb())
            {
                SPList olist = oweb.Lists.TryGetList("TimeSheet");
                if (olist != null)
                {
                    SPQuery oquery = new SPQuery();
                    oquery.Query = "<Where><And><Eq><FieldRef Name='Year' /><Value Type='Text'>" + year + "</Value></Eq><Eq><FieldRef Name='Department' /><Value Type='Text'>Docent Speakers Bureau</Value></Eq></And></Where>";
                    dt = olist.GetItems(oquery).GetDataTable();
                }
            }
        }
        return dt;
    }
    private DataTable GetSelectedMonthData()
    {
        DataTable dt = new DataTable();
        string ddvalue = ddlMonth.SelectedItem.Text;
        using (SPSite site = new SPSite(SPContext.Current.Site.Url))
        {
            using (SPWeb web = site.OpenWeb())
            {
                SPList listTime = web.Lists["TimeSheet"];

                SPQuery query = new SPQuery();
                query.Query = "<Where><And><Eq><FieldRef Name='Month_x0026_Year'/><Value Type='Text'>" + ddvalue + "</Value></Eq><Eq><FieldRef Name='Department'/><Value Type='Text'>Docent Speakers Bureau</Value></Eq></And></Where>";
                string.Concat("<FieldRef Name='Hours'/>",
                               "<FieldRef Name='V_LastName'/>",
                               "<FieldRef Name='V_FirtsName'/>");
                dt = listTime.GetItems(query).GetDataTable();
            }
        }
        return dt;
    }
    private DataTable GetMonthHours(string username,string ddlvalue)
    {
        DataTable dtuser;
        using (SPSite site = new SPSite(SPContext.Current.Site.Url))
        {
            using (SPWeb web = site.OpenWeb())
            {
                SPList listTime = web.Lists["TimeSheet"];
                string ddvalue = ddlMonth.SelectedItem.Text;
                SPQuery query = new SPQuery();
                query.Query = "<Where><And><And><Eq><FieldRef Name='UserName'/><Value Type='Text'>" + username.Trim() + "</Value></Eq><Eq><FieldRef Name='Department' /><Value Type='Text'>Docent Speakers Bureau</Value></Eq></And><Eq><FieldRef Name='Month_x0026_Year'/><Value Type='Text'>" + ddlvalue + "</Value></Eq></And></Where>";

                dtuser = listTime.GetItems(query).GetDataTable();
            }
        }
        return dtuser;
    }
    protected void GvBindData_Sorting(object sender, GridViewSortEventArgs e)
    {
        string ddvalue1 = ddlMonth.SelectedItem.Text;
        string sortingDirection = string.Empty;
        if (sorDirection == SortDirection.Ascending)
        {
            sorDirection = SortDirection.Descending;
            sortingDirection = "Desc";
        }
        else
        {
            sorDirection = SortDirection.Ascending;
            sortingDirection = "Asc";
        }
        ;
        DataTable table = (DataTable)ViewState["Data"];
        table.DefaultView.Sort = e.SortExpression + " " + sortingDirection;
        GvBindData.DataSource = table;
        GvBindData.DataBind();

    }
    public SortDirection sorDirection
    {
        get
        {
            if (ViewState["dirState"] == null)
            {
                ViewState["dirState"] = SortDirection.Ascending;
            }
            return (SortDirection)ViewState["dirState"];
        }
        set
        {
            ViewState["dirState"] = value;
        }
    }

Can u suggest me how to fix that issue....

Free Windows Admin Tool Kit Click here and download it now
December 30th, 2014 12:15pm

Hi,

You need to debug your code step by step to check if the datasource is valid when first page load.

I assume some object or datasource is null value in your code when first page load.

Best Regards

January 6th, 2015 5:58am

Hi sadomovalex,

Thanks for Responding for my Question,

As per your Answer ,The Feature i.e.Minimal Download Strategy is disabled. But Still we are getting same error.

Thanks,

Free Windows Admin Tool Kit Click here and download it now
January 29th, 2015 3:14pm

Hi Jerry,

we tried for Debugging code..it is executing entire code with out exception.

when we browse the site will get that error for some Time.

We are using SP2013 Version.

Thanks,

January 29th, 2015 3:17pm

Hi All,

If any one got the above error.Please do the following changes in web.config file.

It will work properly w/o error.

<SharePoint>    <SafeMode MaxControls="200" CallStack="false" DirectFileDependencies="10" TotalFileDependencies="2500" StackTrace="True">            <PageParserPaths>         <PageParserPath VirtualPath="/*" CompilationMode="Always" AllowServerSideScript="true" IncludeSubFolders="true" />       </PageParserPaths>

    </SafeMode>

Thanks,

Free Windows Admin Tool Kit Click here and download it now
February 2nd, 2015 3:25am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics