Imports System.Data.OleDb

 

Public Class WebForm1

    Inherits System.Web.UI.Page

    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

    Protected WithEvents HyperLink1 As System.Web.UI.WebControls.HyperLink

 

 

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If Session("ValidUser") <> True Then

            Response.Redirect("Login.aspx")

        End If

        'Put user code to initialize the page here

        If Not Page.IsPostBack Then

            BindTheData("Select * From Passwords order by Account")

        End If

 

    End Sub

 

    Sub BindTheData(ByVal strSQL As String)

 

        '******************* OPEN A DATABASE ****************************************************

        'Could Be In Global Area ****************

        Dim objConn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & Server.MapPath("Passwords.mdb"))

        Dim objCommand As New OleDbCommand()

        Dim objDa As New OleDbDataAdapter()

        Dim objDs As New DataSet()

        Dim objCommandBuilder As New OleDbCommandBuilder()

        ' **********************************************

 

        objCommand.CommandText = "Select * From Passwords order by Account"

        objCommand.Connection = objConn

        objDa.SelectCommand = objCommand

        objDa.Fill(objDs, "Passwords")

        '***********************************************************************************************

        objConn.Close()

        DataGrid1.DataSource = objDs.Tables("Passwords")

        DataGrid1.DataBind()

    End Sub

 

 

 

 

    Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.ItemCommand

        'this threw me...

        If e.CommandName.ToUpper = "EDIT" Then

            DataGrid1.SelectedIndex = e.Item.ItemIndex

            DataGrid1.EditItemIndex = e.Item.ItemIndex

            BindTheData("Select * From Passwords order by Account")

        End If

 

        If e.CommandName.ToUpper = "CANCEL" Then

            DataGrid1.SelectedIndex = -1

            DataGrid1.EditItemIndex = -1

            BindTheData("Select * From Passwords order by Account")

        End If

 

        If e.CommandName.ToUpper = "DELETE" Then

            DeleteAccount(e.Item.Cells(4).Text)

        End If

 

    End Sub

 

    Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.UpdateCommand

        Dim o As New FunctionsAndSubs.FunctionsAndSubs()

 

        'this threw me...

        Dim strAccount As String = CType(e.Item.Cells(1).Controls(0), TextBox).Text

        Dim strUrl As String = CType(e.Item.Cells(2).Controls(0), TextBox).Text

        Dim strPW As String = o.EncryptString(CType(e.Item.Cells(3).Controls(0), TextBox).Text, "DaisyBean")

        Dim strId As String = e.Item.Cells(4).Text

 

 

        '*********************NONQUERY UPDATE *****************************

        Dim objConnection As New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("Passwords.mdb"))

        Dim objcommand As New OleDbCommand()

        Dim QStr As String

 

        QStr = "UPDATE Passwords SET "

        QStr = QStr & "Passwords.Account = '" & strAccount & "'" & ","

        QStr = QStr & "Passwords.URL = '" & strUrl & "'" & ","

        QStr = QStr & "Passwords.Password = '" & strPW & "'"

 

        QStr = QStr & " WHERE id = " & strId

 

        objcommand = New OleDbCommand(QStr, objConnection)

        objConnection.Open()

        objcommand.ExecuteNonQuery()

        objConnection.Close()

        '***************************************************************************************

 

        DataGrid1.EditItemIndex = -1

        BindTheData("Select * From Passwords order by Account")

 

 

    End Sub

 

    Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound

 

        Dim o As New FunctionsAndSubs.FunctionsAndSubs()

        Dim vstr As String = ""

 

        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.SelectedItem Then

            vstr = e.Item.Cells(3).Text.Trim

            vstr = o.DecryptString(vstr, "DaisyBean")

            e.Item.Cells(3).Text = vstr

        End If

 

        If e.Item.ItemType = ListItemType.EditItem Then

            vstr = CType(e.Item.Cells(3).Controls(0), TextBox).Text.Trim

            vstr = o.DecryptString(vstr, "DaisyBean")

            CType(e.Item.Cells(3).Controls(0), TextBox).Text = vstr

        End If

 

        'Delete Button...

        Dim B As Button

        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.EditItem Then

            B = e.Item.Cells(6).Controls(0)

            B.Attributes.Add("onclick", "return confirm('Are you sure you want to delete this record?');")

        End If

 

    End Sub

 

    Sub DeleteAccount(ByVal vId)

        '********** DELETE NONQUERY *************************************

        Dim objConn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & Server.MapPath("Passwords.mdb") & ";")

        Dim objcommand As New OleDbCommand()

        objcommand = New OleDbCommand("DELETE Passwords.* FROM Passwords Where Passwords.id=" & vId & ";", objConn)

        objConn.Open()

        objcommand.ExecuteNonQuery()

        objConn.Close()

        '*************************************************************************

        BindTheData("Select * From Passwords order by Account")

    End Sub

 

 

End Class