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.
>>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 >> >>>>
>>> >>>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 >>> >>>>>>
>>> >>>' 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 >>> >>>>>>