Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have gridview display columns from 2 tables
I need to edit & update record displayed in gridview
I wrote this code but it didn't work
Protected Sub GridView2_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView2.RowEditing
        GridView2.EditIndex = e.NewEditIndex
        GridView2.DataBind()
    End Sub

The updating

Protected Sub GridView2_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView2.RowUpdating
        ' Dim connString As String = ConfigurationManager.ConnectionStrings("Conn").ConnectionString
        Dim row As GridViewRow = DirectCast(GridView2.Rows(e.RowIndex), GridViewRow)
        Dim lblupdate As Label = DirectCast(row.FindControl("Label2"), Label)


        Dim id As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("lblid"), Label).Text

        Dim stname As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("txtstname"), TextBox).Text
        Dim dob As String = DirectCast(GridView1.Rows(e.RowIndex) _
                                .FindControl("txtdob"), TextBox).Text

        Dim nationality As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("txtnationality"), TextBox).Text
        Dim gender As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("txtgender"), TextBox).Text
        Dim level As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("txtlevel"), TextBox).Text
        Dim sect As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("txtsect"), TextBox).Text
        Dim newst As String = DirectCast(GridView1.Rows(e.RowIndex).FindControl("txtnewst"), TextBox).Text


        Dim con As New SqlConnection(strConnString)
        Dim cmd As New SqlCommand()
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "update stinfo set stname=@stname," _
       & "dob=@dob, nationality=@nationality, gender=@gender,level=@level,sect=@sect,newst=@newst where id=@id"
        cmd.Parameters.Add("@id", SqlDbType.NVarChar).Value = id
        cmd.Parameters.Add("@stname", SqlDbType.NVarChar).Value = stname
        cmd.Parameters.Add("@dob", SqlDbType.NChar).Value = dob
        cmd.Parameters.Add("@nationality", SqlDbType.NChar).Value = nationality
        cmd.Parameters.Add("@gender", SqlDbType.NVarChar).Value = gender
        cmd.Parameters.Add("@level", SqlDbType.NVarChar).Value = level
        cmd.Parameters.Add("@sect", SqlDbType.NChar).Value = sect
        cmd.Parameters.Add("@newst", SqlDbType.NVarChar).Value = newst
        cmd.ExecuteNonQuery()
        GridView1.EditIndex = -1
        'GridView1.DataSource = GetData(cmd)
        GridView1.DataBind()
        con.Close()
        
    End Sub

where is the error comes from?
Posted
Comments
[no name] 23-Jun-14 16:37pm    
What error are you referring to? All you told us was "didn't work" which is not at all a helpful description of a problem.
Dena2013 23-Jun-14 23:06pm    
when i click update there is nothing happend
KaushalJB 24-Jun-14 6:46am    
Is your first time Gridview binding method in

If Not IsPostBack
GridviewBind()
End If

?

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