View Single Post
 
Old July 23rd, 2004, 02:40 PM
RPG SEARCH RPG SEARCH is offline
Friend of Wrox
 
Join Date: Apr 2004
Location: Preston, Lancashire, United Kingdom.
Posts: 105
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to RPG SEARCH
Default Database Connection with Web.Config

Hi there,

I literally just started to develope my own application after working through the beginners guide to ASP.NET in VB.NET to test what I had learnt. I have come across a problem.

When i am pulling user name and password from the database with the database string in the page code it is fine. but when i replace it with the ConfigurationSettings.AppSettings("connectionStrin g") it throws me errors. i have matched the syntax... this is my web.config

<?xml version="1.0" encoding="UTF-8" ?>

<configuration>

            <appSettings>
            <add key="connectionString" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwroot\DJSTORE\database\store.m db"/>
         </appSettings>

    <system.web>

        <authentication="Forms">

            <forms name=".djstore" loginurl="admin/login.aspx" protection="Validation" timeout="999999" />

        </authentication>

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

    </system.web>

</configuration>

And this is my page so far:

<%@ Page Language="VB" %>
<script runat="server">

    ' This is the code for the Login Button

    Sub LoginBtn_Click(Sender as Object, E as EventArgs)

        Dim UsersDB as System.Data.iDataReader
        UsersDB = Users()
        While UsersDB.Read

            Dim UserLogin as String = UsersDB("UserName")
            Dim UserPassword as String = UsersDB("UserPass")
            Dim AdminLevel as String = UsersDB("UserLevel")

              If (UserName.Text = UserLogin And UserPass.Text = UserPassword) Then

                Dim UserNameCookie as New HttpCookie("UserNameCookie")
                UserNameCookie.Value = UserName.Text
                Response.Cookies.Add(UserNameCookie)

                Dim UserLevelCookie as New HttpCookie("UserLevelCookie")
                UserLevelCookie.Value = AdminLevel
                Response.Cookies.Add(UserLevelCookie)

                FormsAuthentication.RedirectFromLoginPage(UserName .Text, true)

              Else

                Msg.Text = "Invalid User Name or Password: Please try again!"

              End If

            End While

    End Sub


    ' This creates the database connection and reads the information for admin logins

    Function Users() As System.Data.IDataReader
        Dim connectionString As String = ConfigurationSettings.AppSettings("connectionStrin g")
        Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString )
        Dim queryString As String = "SELECT [UserDetails].[UserName],[UserDetails].[UserPass], [UserDetails].[UserLevel] FROM [UserDetails]"

        Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
        dbCommand.CommandText = queryString
        dbCommand.Connection = dbConnection

        dbConnection.Open
        Dim dataReader As System.Data.IDataReader = dbCommand.ExecuteReader(System.Data.CommandBehavio r.CloseConnection)

        Return dataReader
    End Function

</script>
<html>
<head>
    <link id="css" href="../includes/shop.css" type="text/css" rel="stylesheet" />
</head>
<body>


    <form runat="server">
        <p align="center">



            <img src="images/logo.gif" />
        </p>
        <p align="center">
            User Name:&nbsp;<asp:TextBox id="UserName" runat="server"></asp:TextBox>
            <br />
            Password:
            <asp:TextBox id="UserPass" runat="server" textmode="Password"></asp:TextBox>
        </p>
        <p align="center">
            <asp:Button id="LoginBtn" onclick="LoginBtn_Click" runat="server" Text="Login"></asp:Button>
            <br />
            <asp:Label id="Msg" runat="server" forecolor="red"></asp:Label>
        </p>
    </form>
</body>
</html>

cheers for any help, if it cant be done then i will just have to do it with the direct connection which is not really what i wanted.

__________________
David Jenkins