You must Sign In to post a response.
  • Category: ASP.NET

    Intermittent Invalid Viewstate Error in ASP.NET Web pages

    Im developing a Chart that is displaying Farmer Status (current status, pending status etc...)
    the for the click status from a reapter using linkbutton it is working ...
    but when i close google chart and try to selet another farmer status it getting error ... *

    [ViewStateException: Invalid viewstate.
    Client IP: ::1
    Port: 27968
    Referer: http://localhost/HolidayTracker/Report/VacationChart.aspx
    Path: /HolidayTracker/Report/VacationChart.aspx
    User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)
    ViewState: ....

    protected void rpStatus_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
    try
    {
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
    if (e.CommandName == "Status")
    {
    this.dgFarmerDetail.DataSource = null;
    this.dgFarmerDetail.DataBind();

    this.dFarmerDetails.Visible = false;

    this.dDetailHead.Visible = false;
    this.dfarmerHead.Visible = false;

    StatusID = Convert.ToInt32(e.CommandArgument);

    this.SetFarmerChart();
    ScriptManager.RegisterStartupScript(this, this.Page.GetType(), "updatePanel1Script", "javascript:drawFarmerChart();", true);
    }
    }
    }
    catch (Exception ex)
    {
    throw ex;
    }
    }

    public string SetFarmerChart()
    {
    DataTable sourceFarmer = null;
    StringBuilder returnFarmerValue;
    int incVal;
    FarmerLogic farmerLogic;
    FarmerEntity farmerEntity;

    try
    {
    returnFarmerValue = new StringBuilder();

    farmerEntity = new FarmerEntity();

    if (StatusID == 0)
    {
    farmerEntity.UserID = GM.UserID;
    farmerEntity.StatusID = 0;
    farmerEntity.RegionID = 0;
    farmerEntity.DistrictID = 0;
    farmerEntity.TahsilID = 0;
    farmerEntity.VillageID = 0;
    }
    else if (StatusID > 0)
    {
    farmerEntity.UserID = GM.UserID;
    farmerEntity.StatusID = StatusID;
    farmerEntity.RegionID = 0;
    farmerEntity.DistrictID = 0;
    farmerEntity.TahsilID = 0;
    farmerEntity.VillageID = 0;
    }

    farmerLogic = new FarmerLogic();

    sourceFarmer = farmerLogic.SelectFarmerMasterStatusChart(farmerEntity);

    if (sourceFarmer == null) { sourceFarmer = null; return string.Empty; }
    if (sourceFarmer.Rows.Count <= 0) { sourceFarmer = null; return string.Empty; }

    if (sourceFarmer.Rows.Count > 0 && Convert.ToInt32(sourceFarmer.Rows[0]["TotalStatus"].ToString()) != 0)
    {
    returnFarmerValue.Append("data.addColumn('string', 'Status');");
    returnFarmerValue.Append("data.addColumn('number', 'TotalStatus');");

    returnFarmerValue.Append("data.addRows(" + sourceFarmer.Rows.Count + ");");

    for (incVal = 0; incVal < sourceFarmer.Rows.Count; incVal++)
    {
    returnFarmerValue.Append("data.setValue(" + incVal + ", 0, '" + sourceFarmer.Rows[incVal]["Status"] + "');");
    returnFarmerValue.Append("data.setValue(" + incVal + ", 1, " + sourceFarmer.Rows[incVal]["TotalStatus"] + ");");
    }

    return returnFarmerValue.ToString();
    }
    else
    {
    return returnFarmerValue.ToString();
    }
    }
    catch (Exception ex)
    {
    throw ex;
    }
    finally
    {
    sourceFarmer = null;
    farmerLogic = null;
    farmerEntity = null;
    }
    }
  • #768103
    Hi,

    Is that issue happening occasionally or all times?
    Because I face this kind of issue once and the issue was caused because of application pool reset each time.
    due to this my Authentication id get change each time and that throws the issue with the view state.

    If it is happening occasionally then that should be the application pool reset problem.
    For that we can follow two ways,

    @ We can disable ViewstateMac by putting this like

    ?enableViewStateMac='false'?

    in your web.config. But this approach was not so secured I feel.

    @ We can configure ASP.NET to not use Auto-Generated Key but rather a predefined key.

    Let us know if you are facing different level of issue.

    Thanks,
    Mani

  • #768116
    When we have lot of controls on the same page many times we face this issue of 'ViewStateException: Invalid viewstate'
    There are different solution for it, you need to try with following workarounds
    - Set the validationKey attribute if you are running in a Web farm
    - Do not store dynamically generated types in view state in a Web farm
    - set enableViewStateMac="false"
    - there also a message that state, You receive a "View state is invalid" error message when you use the Server.Transfer method
    - Another possibility is Forms authentication and view state fail intermittently under heavy load
    - Determine exactly what exception occurs when you receive the error message
    check out following link for more details
    https://support.microsoft.com/en-in/kb/829743

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


Sign In to post your comments