Click here to Skip to main content
13,253,962 members (63,603 online)
Rate this:
Please Sign up or sign in to vote.
See more:

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

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

these are the events

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
    trns = con.BeginTransaction()
    Dim x As String
    x = UpdateDetails(DisNo, RefNo, Ncode, Subj, OutsideDetails, OutsideCopyDetails, trns)
        If Val(x) > 0 Then
        End If
    Catch ex As Exception
    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
Posted 9-Jan-13 1:18am
Updated 10-Jan-13 0:06am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Go through this whole discussion and see what you learn:[^]

Do read comment by PhilM99.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You should add code to the update event to check if the row in event arg e is itemtemplate or edittemplate.

The ddlNature control is inside edit template so you need to check that first then it will resolve.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.171114.1 | Last Updated 10 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100