Hello Friends

I still muddled with some things (that is with a lot of them). I have a Gridview, where the first column ia a TemplateField with a LinkButton to delete the record, the selected row.

I did the same routine using the primary key with DataKeyNames, however in this case the GridView, was populated manually and now I'm using curl.

I did the following:

Protected Sub GrdDocumentos_RowDataBound (ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GrdDocumentos.RowDataBound 
       
         If e.Row.RowType = DataControlRowType.DataRow Then 
             Dim l As LinkButton = DirectCast (e.Row.FindControl ( "LinkButton1"), LinkButton) 
             l.Attributes.Add ( "onclick", ( "javascript: return" & "confirm ( 'Really delete the Document") & DataBinder.Eval (e.Row.DataItem, "REL_DOCUMENTO") & "?')") 
 End If 
End Sub
Private Sub  GrdDocumentos_RowCommand (ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GrdDocumentos.RowCommand 
         'Can I exclude here in RowCommand 

         If e.CommandName = "Delete" Then 
              RelID GrdDocumentos.SelectedRow.Cells = (4). Text (does not work that way) 
                 
             'GrdDocumentos.SelectedIndex = SelectedGridRow.RowIndex 
             'Delete the record 
             'Implement this on your own:) 
             DeleteRecordByID (relID) 
         End If 
End Sub
Private  Sub DeleteRecordByID (ByVal RegID As Integer) 

         Dim Sql As String 
         Dim cmd As SqlCommand 

         Dim connection As SqlConnection 

         connection = New SqlConnection (ConfigurationManager.ConnectionStrings ( "Timesheet"). ConnectionString) 

         Sql = "DELETE FROM REL_PROJETOS" 
         Sql + = "WHERE REL_ID =" & RegID 

         cmd = New SqlCommand (Sql, connection) 

         Try 
             conexao.Open () 

             cmd.Connection = connection 

             cmd.CommandType = CommandType.Text 

             cmd.CommandText = Sql 

             cmd.ExecuteNonQuery () 
         Catch ex As Exception 
             lblMensagem.Text = "Error Occurred While Deleting:" & vbCrLf & ex.Message & vbCrLf '& ex.InnerException.ToString 
         Finally 
             conexao.Close () 
             GrdDocumentos.DataBind () 
         End Try 
     End Sub Protected Sub GrdDocumentos_SelectedIndexChanging (ByVal sender As Object, ByVal e As System.EventArgs) Handles GrdDocumentos.SelectedIndexChanged 
         RelID = GrdDocumentos.SelectedRow.Cells (12). Text 
End Sub

Then I need to get the ID to be able to delete the desired record.

Thanks and a hug

Hello Friends

I still muddled with some things (that is with a lot of them). I have a Gridview, where the first column ia a TemplateField with a LinkButton to delete the record, the selected row.

I did the same routine using the primary key with DataKeyNames, however in this case the GridView, was populated manually and now I'm using curl.

I did the following:

Protected Sub GrdDocumentos_RowDataBound (ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GrdDocumentos.RowDataBound 
       
         If e.Row.RowType = DataControlRowType.DataRow Then 
             Dim l As LinkButton = DirectCast (e.Row.FindControl ( "LinkButton1"), LinkButton) 
             l.Attributes.Add ( "onclick", ( "javascript: return" & "confirm ( 'Really delete the Document") & DataBinder.Eval (e.Row.DataItem, "REL_DOCUMENTO") & "?')") 
 End If 
End Sub
Private Sub  GrdDocumentos_RowCommand (ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GrdDocumentos.RowCommand 
         'Can I exclude here in RowCommand 

         If e.CommandName = "Delete" Then 
              RelID GrdDocumentos.SelectedRow.Cells = (4). Text (does not work that way) 
                 
             'GrdDocumentos.SelectedIndex = SelectedGridRow.RowIndex 
             'Delete the record 
             'Implement this on your own:) 

          [B]Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim Row As GridViewRow = GrdDocumentos.Rows(Index)
            Dim relIDAs String = Row.Cells(2).Text.Trim[/B]

             DeleteRecordByID (relID) 
         End If 
End Sub
Private  Sub DeleteRecordByID (ByVal RegID As Integer) 

         Dim Sql As String 
         Dim cmd As SqlCommand 

         Dim connection As SqlConnection 

         connection = New SqlConnection (ConfigurationManager.ConnectionStrings ( "Timesheet"). ConnectionString) 

         Sql = "DELETE FROM REL_PROJETOS" 
         Sql + = "WHERE REL_ID =" & RegID 

         cmd = New SqlCommand (Sql, connection) 

         Try 
             conexao.Open () 

             cmd.Connection = connection 

             cmd.CommandType = CommandType.Text 

             cmd.CommandText = Sql 

             cmd.ExecuteNonQuery () 
         Catch ex As Exception 
             lblMensagem.Text = "Error Occurred While Deleting:" & vbCrLf & ex.Message & vbCrLf '& ex.InnerException.ToString 
         Finally 
             conexao.Close () 
             GrdDocumentos.DataBind () 
         End Try 
     End Sub Protected Sub GrdDocumentos_SelectedIndexChanging (ByVal sender As Object, ByVal e As System.EventArgs) Handles GrdDocumentos.SelectedIndexChanged 
         RelID = GrdDocumentos.SelectedRow.Cells (12). Text 
End Sub

Then I need to get the ID to be able to delete the desired record.

Thanks and a hug

please see the bold-red text. Hope I can help you :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.