Click here to Skip to main content
15,894,182 members

Failed to load view State

ujjwal uniyal asked:

Open original thread
Hello!

I am trying to update the row of a gridview . When i click on edit button nothing happens. when i again click on it , it shows me this error.

Failed to load viewstate.  The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.  For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.


on my pageload i am calling this function

VB
Public Sub BindGrid()
        Dim cd As String = Session("usercd")
        Dim x As String = "select e.index_no , e.ref_no ,n.nature_detail as natureD,e.[subject] , e.outside_details,e.outside_copy_details from let_entry e "
        x += "inner join nature as n on n.nature_cd=nature_code "

        x += "where index_no like '%-D-%'"
        x += " and org_usr_code='" + cd + "'"
        x += "and let_no not in (select let_no from dispatch_det) order by 1 desc"
        Dim dt As New DataTable()
        dt = Dal.GettDS(x).Tables(0)
        GridView3.DataSource = dt
        GridView3.DataBind()



these are the events

VB
Protected Sub GridView3_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView3.RowEditing
    GridView3.EditIndex = e.NewEditIndex

End Sub

Protected Sub GridView3_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView3.RowCancelingEdit
    e.Cancel = True
    GridView3.EditIndex = -1
End Sub

Protected Sub GridView3_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView3.RowUpdating
    Dim gvrow As GridViewRow = GridView3.Rows(e.RowIndex)
    Dim NatureCode As DropDownList = DirectCast(gvrow.FindControl("ddlNature"), DropDownList)
    Dim txtReferenceNo As TextBox = DirectCast(gvrow.FindControl("txtRefNo"), TextBox)
    Dim txtSubject As TextBox = DirectCast(gvrow.FindControl("txtSubject"), TextBox)
    Dim txtOutsideDetails As TextBox = DirectCast(gvrow.FindControl("txtOutsideDetails"), TextBox)
    Dim txtOutsideCopyDetail As TextBox = DirectCast(gvrow.FindControl("txtOutsideCopyDetails"), TextBox)
    Dim lblDisNo As Label = DirectCast(gvrow.FindControl("lblDisNo"), Label)

    Dim DisNo As String = lblDisNo.Text
    Dim Ncode As Int32 = NatureCode.SelectedValue
    Dim RefNo As String = txtReferenceNo.Text
    Dim Subj As String = txtSubject.Text
    Dim OutsideDetails As String = txtOutsideDetails.Text
    Dim OutsideCopyDetails As String = txtOutsideCopyDetail.Text

    Dim constring As String = ConfigurationManager.ConnectionStrings("patravaliConnectionString").ConnectionString
    Dim con As New SqlConnection(constring)
    Dim trns As SqlTransaction
    con.Open()
    trns = con.BeginTransaction()
    Dim x As String

    x = UpdateDetails(DisNo, RefNo, Ncode, Subj, OutsideDetails, OutsideCopyDetails, trns)
    Try
        If Val(x) > 0 Then
            trns.Commit()
        Else
            trns.Rollback()
        End If
    Catch ex As Exception
    Finally

        con.Close()
        BindGrid()
    End Try



End Sub

Public Function UpdateDetails(ByVal Disno As String, ByVal RefrenceNo As String, ByVal Naturecode As Int32, ByVal Subject As String, ByVal OutsideDetails As String, ByVal OutsideCopyDetails As String, ByVal trns As SqlTransaction) As String


    Dim update As String = "update let_entry set ref_no=@ref_no ,nature_code=@nature_cd, [subject]=@subject , outside_details=@outside_details, outside_copy_details=@outside_copy_details where index_no =@index_no"

    Dim cmd As New SqlCommand(update, trns.Connection)
    cmd.Transaction = trns
    cmd.Parameters.AddWithValue("@ref_no", RefrenceNo)
    cmd.Parameters.AddWithValue("@index_no", Disno)
    cmd.Parameters.AddWithValue("@nature_cd", Naturecode)
    cmd.Parameters.AddWithValue("@subject", Subject)
    cmd.Parameters.AddWithValue("@outside_details", OutsideDetails)
    cmd.Parameters.AddWithValue("@outside_copy_details", OutsideCopyDetails)

    UpdateDetails = cmd.ExecuteNonQuery().ToString()



End Function
Tags: ASP.NET, SQL Server

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900