Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 9-Jan-13 23:06pm
v2

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.
 
Share this answer
 
Go through this whole discussion and see what you learn: http://forums.asp.net/t/940975.aspx/4/10[^]

Do read comment by PhilM99.
 
Share this answer
 

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



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