Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Deleting row from datagrid
Message
 
To
08/12/2002 13:00:12
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00730717
Message ID:
00730748
Views:
16
Hi James,

Thanks for your assistance. I finally got it working the way I wanted with code like this (with your help):
Private Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal e As ...

        Dim objConn As OleDb.OleDbConnection = New OleDb.OleDbConnection()
        Dim objCmd As OleDb.OleDbCommand
        Dim strKey As String
        Dim strSQL As String
        Dim strConn As String

        'save the PK of the record to be deleted to memvar 'strKey'
        strKey = e.Item.Cells(0).Text.ToString.Trim

        'create the Delete SQL String
        If Session("strRecordType") = "Mystery_1" Then
            strSQL = "Delete from BlahBlahBlah Where ID = " & strKey
        ElseIf Session("strRecordType") = "Mystery_2" Then
            strSQL = "Delete from BlahBlahBlah Where ID = " & strKey
        End If

        'set the connection string
        If ConfigurationSettings.AppSettings("AppMode") = "ModeProd" Then
            strConn = ConfigurationSettings.AppSettings("dbConnectLive")
        ElseIf ConfigurationSettings.AppSettings("AppMode") = "ModeTest" Then
            strConn = ConfigurationSettings.AppSettings("dbConnectTest")
        End If

        objConn.ConnectionString = strConn
        objCmd = New OleDb.OleDbCommand(strSQL, objConn)

        Try
            ' try to open the connection
            objConn.Open()
            Try
                'Now delete the record
                objCmd.ExecuteNonQuery()
            Catch err As Exception
                objConn.Close()
                Response.Write("Error Deleting record ..." & err.Message)
            End Try
        Catch err As Exception
            Response.Write("Error Connecting to DB ..." & err.Message)
        Finally
            'close the connection
            objConn.Close()
        End Try

        're-fill the data grid
        subFillGrid()
        're-bind the data grid
        DataGrid1.DataBind()


    End Sub
>Sorry the comment on the last line is wrong, it should have been something like 'Redisplay grid data.
>
>Aloha,
>
>James
>>Hey Allan,
>>
>>I'm not absolutly sure on all the details but here's my theory:
>>
>>
>>Private Sub DataGrid1_DeleteCommand(ByVal source As Object, _
>>ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
>>Handles DataGrid1.DeleteCommand
>>   Dim PKey As Integer
>>   iPKey = e.Items.Cells[1].Text
>>
>>   'Code to get a connection to the Access database
>>   SQLString = "Delete BlahBlahBlah Where PKey = " + Str(iPKey)
>>   oConnection = New OleDbConnection(???)
>>   oCommand = New OleDbCommand(SQLString, oConnection)
>>
>>   'Now delete the record
>>   oConnection.Open()
>>   oCommand.ExecuteNonQuery()
>>   oConnection.Close()
>>
>>   ' Add code to delete data from data source.
>>   subFillGrid()
>>End Sub
>>
>>
>>
>>
>>>Hi,
>>>
>>>I have a datagrid in which I've added a delete button on one of my webforms. The code I'm using to populate the grid from an Access DB (sorry, it couldn't be avoided ...) looks like this:
>>>
>>>
>>>
>>>Sub subFillGrid()
>>>
>>>Dim objConn As OleDb.OleDbConnection = New OleDb.OleDbConnection()
>>>Dim Adapter As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter()
>>>Dim dsDataDict As Data.DataSet = New Data.DataSet()
>>>Dim strConn, strSQL As String
>>>
>>>If ConfigurationSettings.AppSettings("AppMode") = "ModeProd" Then
>>>    strConn = ConfigurationSettings.AppSettings("dbConnectLive")
>>>ElseIf ConfigurationSettings.AppSettings("AppMode") = "ModeTest" Then
>>>    strConn = ConfigurationSettings.AppSettings("dbConnectTest")
>>>End If
>>>
>>>strSQL = "Select FirstNameVC, LastNameVC, EMailVC, DateDT from BlahBlahBlah"
>>>
>>>objConn.ConnectionString = strConn
>>>Adapter.SelectCommand = New OleDb.OleDbCommand(strSQL, objConn)
>>>Adapter.SelectCommand.Connection.Open()
>>>Adapter.Fill(dsDataDict, "FileReg")
>>>DataGrid1.DataSource = dsDataDict.Tables("FileReg")
>>>DataGrid1.DataBind()
>>>End Sub
>>>
>>>
>>>
>>>The grid gets populated just fine.
>>>
>>>Now I'm trying to figure out how to code this event. The help that I found was limited (I may not have been looking in the right place ...)
>>>
>>>
>>>
>>>' Visual Basic
>>>Private Sub DataGrid1_DeleteCommand(ByVal source As Object, _
>>>ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
>>>Handles DataGrid1.DeleteCommand
>>>   Dim index As Integer
>>>   index = e.Item.ItemIndex
>>>   ' Add code to delete data from data source.
>>>   DataGrid1.DataBind()
>>>End Sub
>>>
>>>
>>>
>>>I'm thinking that I need to do something with a DataRow - but I'm stumped. Any suggestions on the code I should use to delete the row from the DataTable and the underlying database itself would be appreciated
Al Williams

Anola MB, CANADA
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform