Monday, June 27, 2011

It is a simple code for asp.net that you can use for your login page


Imports System.Data.OleDb

Partial Class _Default
    Inherits System.Web.UI.Page
    Dim cn As New OleDbConnection
    Dim cmd As New OleDbCommand
    Dim dr As OleDbDataReader

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            With cn
                If .State = Data.ConnectionState.Open Then .Close()
                .ConnectionString = "Provider=microsoft.jet.oledb.4.0;data source=" & Server.MapPath("data\asp.mdb")

                .Open()
                'MsgBox("connected")
            End With
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub

Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click

        If txtUsername.Text = "" Or txtPassword.Text = "" Then Exit Sub

        With cmd
            .CommandText = "Select * from tbluser Where username = '" & Trim(txtUsername.Text) & "' and password = '" & Trim(txtPassword.Text) & "' "
            .Connection = cn
            dr = .ExecuteReader
        End With


        If dr.HasRows Then
            'MsgBox("Welcome", MsgBoxStyle.Information, "Welcome")
            Session("name") = txtUsername.Text
            Response.Redirect("default2.aspx")
        Else
            'MsgBox("Access Denied", MsgBoxStyle.Exclamation, "Error")
            Label1.Text = ("Pangit ka! " & txtUsername.Text)
        End If
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click

        txtPassword.Text = Nothing
        txtUsername.Text = Nothing

    End Sub
End Class

No comments:

Post a Comment