Row updating event in asp.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ammu
    New Member
    • Aug 2011
    • 78

    Row updating event in asp.net

    I want to edit row in gridview i write code in row updating event but it is not working can anybody help me to solve this problem

    here is my code
    Code:
     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            binddata()    
    
        End Sub
     Public Sub binddata()
    
            conn.con.Open()
            da = New SqlDataAdapter("select * from ins_del_upd", conn.con)
            da.Fill(ds, "ins_del_upd")
            GridView2.DataSource = ds.Tables("ins_del_upd")
            GridView2.DataBind()
            conn.con.Close()
            ds = New DataSet
    
        End Sub
    
     Protected Sub GridView2_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView2.RowCancelingEdit
            GridView2.EditIndex = -1
    
        End Sub
    
     Protected Sub GridView2_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView2.RowEditing
            GridView2.EditIndex = e.NewEditIndex
    
        End Sub
    
     Protected Sub GridView2_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView2.RowUpdating
            Dim row = GridView2.Rows(e.RowIndex)
          
            Dim rid As Integer = (DirectCast(GridView2.Rows(e.RowIndex).Cells(1).FindControl("RID"), TextBox)).Text
            Dim names As String = (DirectCast(GridView2.Rows(e.RowIndex).Cells(2).FindControl("Names"), TextBox)).Text
            Dim age As Integer = (DirectCast(GridView2.Rows(e.RowIndex).Cells(3).FindControl("Age"), TextBox)).Text
            Dim place As String = (DirectCast(GridView2.Rows(e.RowIndex).Cells(4).FindControl("Place"), TextBox)).Text
    
            mycom1 = New SqlCommand("update ins_del_upd set Names='" & names & "',Age='" & age & "',Place='" & place & "' where RID='" & rid & "'", conn.con)
            mycom1.ExecuteNonQuery()
            conn.con.Close()
    end sub
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Remove the call to the binddata method in your page load event. Call the binddata method in the page PreRender event instead.

    If you bind your data to the GridView in the page load event, then you lose any data that the user provided.

    If you bind the data in the PreRender event, then it will bind after all of the page events have finished so you won't lose anything.

    -Frinny

    Comment

    • Ammu
      New Member
      • Aug 2011
      • 78

      #3
      Thank you Frinny. my probelm is solved. thank you so much.

      --ammu

      Comment

      • Mayuri30
        New Member
        • Jan 2012
        • 12

        #4
        hey,you can set edit property of datagridview... ....

        Comment

        Working...