Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET SQL-Server
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
 
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
 
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 9-Jan-13 0:18am
Edited 9-Jan-13 23:06pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Go through this whole discussion and see what you learn: http://forums.asp.net/t/940975.aspx/4/10[^]
 
Do read comment by PhilM99.
  Permalink  
Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 483
1 Sergey Alexandrovich Kryukov 207
2 RyanDev 165
3 PhilLenoir 135
4 Richard Deeming 120
0 Sergey Alexandrovich Kryukov 6,415
1 OriginalGriff 5,856
2 CPallini 2,473
3 Richard MacCutchan 1,667
4 Abhinav S 1,530


Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 10 Jan 2013
Copyright © CodeProject, 1999-2014
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