How to delete records

loveonfire4u

New member
Joined
Jan 18, 2006
Messages
3
Programming Experience
Beginner
can any 1 help how can i add dis feature to delete button dat when i type say employee ID in text box of form created in Vb 2005 , and press delete button it shuld del that employee id and info from my database, I know to how to make form connection etc just need help regarding dose code or step by step thing to have delete form for my employee form. thnx
 
Give this a try:

VB.NET:
Const g_strDATABASE_CONNECTION_STRING As String = "Data Source=[HOST NAME];database=[DB];uid={User Name];password=[Password];"

    Private g_conConnection As SqlConnection = New SqlConnection
    Private g_cmdSQLStatement As SqlCommand

''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Open a connection to the database.  Open/close a connection each time we want 
    ''' to do something to take advantage of IIS/SQL automatic connection pooling.
    ''' </summary>
    ''' <returns>Boolean</returns>
    ''' <remarks>
    ''' <history>
    '''     [Brandon Schenz]    9/26/2005    Created
    '''     [Brandon Schenz]    9/29/2005   Updated to return False if a problem occurs
    ''' </history>
    ''' </remarks>
    ''' -----------------------------------------------------------------------------
    Public Function OpenDatabaseConnection() As Boolean

        Try

            ' Close anything that's being used or open
            If CloseDatabaseConnection() = True Then

                ' Open a connection to the database
                g_conConnection.ConnectionString = g_strDATABASE_CONNECTION_STRING
                g_conConnection.Open()

                ' SQL Command
                'g_cmdSQLStatement = New MySqlCommand
                g_cmdSQLStatement = g_conConnection.CreateCommand
                g_cmdSQLStatement.Connection = g_conConnection

                ' Success
                return True

            Else

                Return False

            End If

        Catch excError As Exception

            ' Display error message
            WriteLog(excError.ToString)

            CloseDatabaseConnection()

            Throw

        Finally

        End Try

    End Function


    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Close the connection to the database.  Open/close a connection each time we want 
    ''' to do something to take advantage of IIS/SQL automatic connection pooling.
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>
    ''' <history>
    '''     [Brandon Schenz]    9/26/2005    Created
    '''     [Brandon Schenz]    9/29/2005   Updated to return False if a problem occurs
    ''' </history>
    ''' </remarks>
    ''' -----------------------------------------------------------------------------
    Public Function CloseDatabaseConnection() As Boolean

        Try

            ' Is the connection open? 
            If g_conConnection.State <> ConnectionState.Closed Then

                ' Yes, close it
                g_conConnection.Close()

            End If

            ' Clean up
            g_cmdSQLStatement = Nothing

            ' Success
            Return True

        Catch excError As Exception

            WriteLog(excError.ToString)

            Return False

            Throw

        End Try

    End Function

''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Delete all records from table that match the ID.
    ''' </summary>
    ''' <param name="intRecordID">ID of Record to be Deleted</param>
    ''' <param name="PrimaryKey">PrimaryKey of Table to Delete From</param>
    ''' <param name="strTable">Table that contains the record to be deleted</param>
    ''' <returns>Boolean</returns>
    ''' <remarks>
    ''' <history>
    '''     [Brandon Schenz]    9/27/2005    Created
    '''     [Brandon Schenz]    9/29/2005   Updated to return False if a problem occurs
    '''     [Brandon Schenz]    10/3/2005   Added a Call to CloseDatabaseConnection in any Error Catch
    ''' </history>
    ''' </remarks>
    ''' -----------------------------------------------------------------------------
    Public Function DeleteRecordsFromTable(ByVal intRecordID As Integer, ByVal strPrimaryKey As String, ByVal strTable As String) As Boolean

        Try

            If OpenDatabaseConnection() = True Then

                ' Build the SQL String
                g_cmdSQLStatement.CommandText = "DELETE FROM " & strTable & " WHERE " & strPrimaryKey & " = " & intRecordID

                ' Do it
                g_cmdSQLStatement.ExecuteNonQuery()

                ' Success
                Return True

            Else

                Return False

            End If

        Catch excError As Exception

            ' Display error message
            WriteLog(excError.ToString)

            Return False

            Throw

        Finally

            CloseDatabaseConnection()

        End Try

    End Function
 
Back
Top