Sunday, June 12, 2011

Save,Update and Delete records using functions

Declare to a class 

 

Public Sub SaveEntry(ByVal id As String, ByVal name As String, ByVal course As String)
        Try

            connection_open()
            strSQL = "insert into tbl_stud(id,name,course) values('" & id & "','" & name & "','" & course & "')"

            cmSQL = New OleDbCommand(strSQL, cnSQL)
            cmSQL.ExecuteNonQuery()
            cmSQL.Dispose()

            connection_close()

        Catch Exp As OleDbException
            MsgBox(Exp.Message, MsgBoxStyle.Critical, "SQL Error")

        Catch Exp As Exception
            MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
        End Try

    End Sub

    Public Sub updateEntry(ByVal id As String, ByVal name As String, ByVal course As String)
        Try

            connection_open()
            strSQL = "UPDATE tbl_stud SET name='" & name & "',course='" & course & "'where id='" & id & "'"

            cmSQL = New OleDbCommand(strSQL, cnSQL)
            cmSQL.ExecuteNonQuery()
            cmSQL.Dispose()

            connection_close()

        Catch Exp As OleDbException
            MsgBox(Exp.Message, MsgBoxStyle.Critical, "SQL Error")

        Catch Exp As Exception
            MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
        End Try

    End Sub

    Public Sub deleteEntry(ByVal id As String, ByVal name As String, ByVal course As String)
        Try

            connection_open()
            strSQL = "delete from tbl_stud where id='" & id & "'"

            cmSQL = New OleDbCommand(strSQL, cnSQL)
            cmSQL.ExecuteNonQuery()
            cmSQL.Dispose()

            connection_close()

        Catch Exp As OleDbException
            MsgBox(Exp.Message, MsgBoxStyle.Critical, "SQL Error")

        Catch Exp As Exception
            MsgBox(Exp.Message, MsgBoxStyle.Critical, "General Error")
        End Try

    End Sub

Declare to the form


 Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click
        deleteEntry(txtid.Text, txtname.Text, txtcourse.Text)
        MsgBox("Student has been Successfully Deleted.", MsgBoxStyle.Information)
      End Sub

 Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
        updateEntry(txtid.Text, txtname.Text, txtcourse.Text)
        MsgBox("Student has been Successfully Udated.", MsgBoxStyle.Information)
      
    End Sub

Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
   
            SaveEntry(txtid.Text, txtname.Text, txtcourse.Text)
            MsgBox("Student has been Successfully Added.", MsgBoxStyle.Information)
    

    End Sub

No comments:

Post a Comment