0
Guys I've a problem. I'm using access 2007. The file name has an extension .accdb .
I created a table called 'login' in the db file named 'MySite_Database.accdb'.

My HTML looks like this :

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="login.aspx.vb" Inherits="login_to_site._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <br />
    <p style="margin-left: 40px">&nbsp;<asp:Label ID="Label1" runat="server" 
            Text="Enter your credentials"></asp:Label>
    </p>
    <br />
    <asp:Label ID="UserName" runat="server" Text="UserID"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>

    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
        ControlToValidate="txtUserName" ErrorMessage="*"></asp:RequiredFieldValidator>

    <br />
    <asp:Label ID="Password" runat="server" Text="Password"></asp:Label>&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" 
        ControlToValidate="txtPassword" ErrorMessage="*"></asp:RequiredFieldValidator>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        
    <asp:Button ID="Button1" runat="server" Text="Log In" /><br /><br />
       <asp:label id="lblMessage" runat="server" width="288px" forecolor="#C00000" font-size="Medium"
        		    font-italic="True" font-bold="True"></asp:label>
    </form>
</body>
</html>

I then made a change in the web.config 
----------------
<appSettings>
    <add key="strConn" value="Provider = Microsoft.Jet.OLEDB.4.0;Data Source=E:\Dot NET Practice\login_to_site\login_to_site\MySite_Database.accdb.login;User ID=;Password=;"/>
  </appSettings>
-----------------
My login.aspx.vb looks like this : 
-------------------
Imports System.Web.Security
Imports System.Data
Imports System.Data.OleDb
Imports System.Configuration


Partial Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Function DBConnection(ByVal strUserName As String, ByVal strPassword As String) As Boolean
        '<sumamry>
        '   |||||   Declare Required Variables
        ' ||||| Access appSettings of Web.Config for Connection String (Constant)
        '</summary>
        Dim MyConn As OleDbConnection = New OleDbConnection(System.Configuration.ConfigurationManager.AppSettings("strConn"))
        '   |||||   This is the Connections Object for an SQL DB
        '<sumamry>
        '   |||||   Create a OleDb Command Object
        '   |||||   Pass in Stored procedure
        '   |||||   Set CommandType to Stored Procedure
        '</summary>

        ' ||||| To Access a Stored Procedure in Access - Requires a Command Object
        Dim MyCmd As New OleDbCommand("sp_ValidateUser", MyConn)
        '   |||||   To Access a Stored Procedure in SQL Server - Requires a Command Object

        MyCmd.CommandType = CommandType.StoredProcedure
        '   |||||   Create Parameter Objects for values passed in
        Dim objParam1, objParam2 As OleDbParameter
        '<sumamry>
        '   |||||   Add the parameters to the parameters collection of the
        ' ||||| command object, and set their datatypes (OleDbType in this case)
        '</summary> 
        objParam1 = MyCmd.Parameters.Add("@UserName", OleDbType.Char)
        objParam2 = MyCmd.Parameters.Add("@Password", OleDbType.Char)

        ''   |||||   Set the direction of the parameters...input, output, etc
        objParam1.Direction = ParameterDirection.Input
        objParam2.Direction = ParameterDirection.Input
        ''   |||||   Set the value(s) of the parameters to the passed in values
        objParam1.Value = strUserName
        objParam2.Value = strPassword

        '   |||||   Try, catch block!
        Try
            '   |||||   Check if Connection to DB is already open, if not, then open a connection
            If MyConn.State = ConnectionState.Closed Then
                '   |||||   DB not already Open...so open it
                MyConn.Open()
            End If

            '   |||||   Create OleDb Data Reader
            Dim objReader As OleDbDataReader
            objReader = MyCmd.ExecuteReader(CommandBehavior.CloseConnection)
            '   |||||   Close the Reader and the Connection Closes with it

            While objReader.Read()
                If CStr(objReader.GetValue(0)) <> "1" Then
                    lblMessage.Text = "Invalid Login!"
                Else
                    objReader.Close()   '   |||||   Close the Connections & Reader
                    Return True
                End If
            End While
        Catch ex As Exception
            lblMessage.Text = "Error Connecting to Database!"
        End Try


    End Function

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
        If Page.IsValid Then    '   ||||| Meaning the Control Validation was successful!
            '   |||||   Connect to Database for User Validation |||||
            If DBConnection(txtUserName.Text.Trim(), txtPassword.Text.Trim()) Then
                FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, False)
                '   |||||   default.aspx Page! Else

            Else
                '   |||||   Credentials are Invalid
                lblMessage.Text = "Invalid Login!"
                '   |||||   Increment the LoginCount (attempts)
                'Session("LoginCount") = CInt(Session("LoginCount")) + 1
                ' ||||| Determine the Number of Tries
                'If Session("LoginCount").Equals(intMaxLoginAttempts) Then
                '	Response.Redirect("Denied.aspx")
                'End If

                'If CInt(Session("Num_of_Tries")) > 2 Then ' ||||| If Exceeds then Deny!
                '	Response.Redirect("Denied.aspx")
                'End If

            End If
        End If
    End Sub
End Class
--------------------------------
I took all this from an existing thread here in this web site only.
The problem is that its giving invalid login for all entries in the user name and password field. My guess is that it is not able to access the table when I define my connection string in the web.config file.
<appSettings>
    <add key="strConn" value="Provider = Microsoft.Jet.OLEDB.4.0;Data Source=E:\Dot NET Practice\login_to_site\login_to_site\MySite_Database.accdb.login;User ID=;Password=;"/>
  </appSettings>

Edited by Shiban: corrected wordings

3
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by orclboy
0

1>Double click on the access database to open it then save it in the access 2002-2003
format i.e with the .mdb extension.
2>Next put this database in the App_Data folder of your website. If you dont have the
App_Data folder then create it.
3>Now your connection string in web.config would be as follows:-
<add name="accessconstr" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source= |DataDirectory|MySite_Database.mdb" providerName="System.Data.OleDb"/>

I am able to estb connection to the db using this connection string but the issue with my website is that I am not able to implement the forms authentication if you have any suggestion do let me know. I can directly type the addr of desired page in browser and the page is displayed even if user is not logged in.

0

Use authentication tag and define authorised user in web.config file
e.g.

<authentication mode="Forms">

<forms loginUrl = "XXX.aspx" name="XXX" timeout="1"/>

</authentication>

<authorization>

<deny users="?" />
<allow users="*" />

</authorization>

Edited by orclboy: Code not posted earlier.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.