Link to home
Start Free TrialLog in
Avatar of Ze Bra
Ze Bra

asked on

Very Simple Login form with MsAccess database

Very Simple Login form with MsAccess database

Using: Asp.net 3.5 / VB
Level: Novice

After browsing a lot and not finding what I want I ask the question.
I need to make a very simple login ( no need to use the logincontrol or any remember, modify,add functionality etc.) which verifies the user and password and if correct then I set a few session variables and redirect them to Main.aspx

So I create a form with a text field (UserName) and a password field ( Password ) and a button (btnLogin).

Now Ive to open the database in the click event so I add this code to it but how do I open the database. Found that Ive to use something like  Dim xx as new oledbconnection  or something like that and then xx.open() but for sure Im using it the wrong way because I dont get it work.

Thanx for any help
Rob

Partial Class login2
    Inherits System.Web.UI.Page
 
    Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogin.Click
 
        Dim Conn As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                             "Data Source=" & Server.MapPath("/App_Data/mydatabase.mdb") & ";Persist Security Info=False;"
 
        Dim sql As String = "Select * from Mydatabase WHERE username=" & txtUserName.Text & "AND password=" & txtPassword.Text
 
'-- and now I'm stucked :-(
'-- Dim cn As New ?????????
cn.open()
 
    End Sub
End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Bob Hoffman
Bob Hoffman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ze Bra
Ze Bra

ASKER

Thnax for the respons

I had already try the oledbconnection but if I use that the programm gives een errormessage so I thought I've to use something different.
This is the message:
Type oldDb.OleDbConnection is not defind

I'm using VWD 2008 express and use VB code

Any idea what I'm doing wrong ?

Regards
Rob
Avatar of Ze Bra

ASKER

Ok, I figured out that I've to use
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>

But where do I've to place that. My code is in a seperate .vb file.

If I use a file with no seperate .vb it's working (i.e. I don't get the error the oledbconnection isn't defined)
Avatar of Ze Bra

ASKER

Problem solved.

I had to place

Imports System.Data
Imports System.Data.OleDb

before the partial class in the .vb file
How did you solve your ORIGINAL problem? You should list the resolution here for others that might have a similar problem.
Avatar of Ze Bra

ASKER

You're right.

See the code snipet of the pblogin.aspx.vb file.
For clearity I've seperate the Dim statements from the set statements

Regards

Imports System.Data
Imports System.Data.OleDb
 
Partial Class login2
    Inherits System.Web.UI.Page
 
    Protected Sub BtnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnLogin.Click
 
        Dim objConnection As OleDbConnection
        Dim strSQLQuery As String
        Dim objCommand As OleDbCommand
        Dim objDataReader As OleDbDataReader
 
        objConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("App_Data\databasename.mdb"))
 
        strSQLQuery = "Select * from members WHERE name = '" & txtUserName.Text & "' And Password = '" & txtPassword.Text & "'"
 
        objConnection.Open()
        objCommand = New OleDbCommand(strSQLQuery, objConnection)
        objDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)
 
        If objDataReader.HasRows Then
            'found so set some session vars if you want so you can see in mainpage.aspx if the user is logged in
            Response.Redirect("mainpage.aspx")
        Else
            Return
        End If
    
    End Sub
End Class

Open in new window

This is basically the code I provided you reformatted. The fact that you set the object properties after the DIMs seems to me to be a preference rather then a requirement. The code I provided works to solve your original question.
Youre going to have a hard time getting help in the future if you close the question without awarding points simple because you dont like how the code snipet was formatted. The bottom line is you got your answer.

Avatar of Ze Bra

ASKER

??? You're almost too friendly :-(

Your solution was the solution I tried before ( see my comments ). The point was that the 2 imports
( Imports System.Data and Imports System.Data.OleDb )
where missing and that's why the oledbconnection didn't work and I don't see that back in your answer.
 
I assumed you would understand that the System.Data.OleDb namespace was required. Besides look at the time of your post! I live in California I was a sleep at that time.
Avatar of Ze Bra

ASKER

To assume that was wrong, it wasn't standing in the code I posted.
Also I don't know the time at your west coast, live in europe.

Lets stop this discission and close the question because the solution is found and further discussion i'sn't relevant in my opinion. Nobody becomes better of it.
You used the code I posted to solve your problem. You use my answer you award me the points, pretty simple concept dont you think? Why do you feel the need to close the question without awarding points?
Avatar of Ze Bra

ASKER

The code you posted was the code I found a million times so nothing new. The point is that the solution was the import which wasn't in your solution. So why must I give points ? If I do it is a little misleading I think.
It isn't that I don't wanna give points but it must be for a real solution and not because you want it.

Let's wait the opinion of a moderator. If they said you're right and I'm wrong I'll award you the points.

End of discission from my side


 
The very first line of your post says "After browsing a lot and not finding what I want" .... Your code is basically identical to the code I supplied.
So unless you're supplied the exact solution you don't award points? WOW!
Avatar of Ze Bra

ASKER

Dear Vee,

Okay, your discription is clear. I'll give the points.

Rob
Avatar of Ze Bra

ASKER

Besides the code you've to use
( if no seperate .vb file )
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
or
( if code is in seperate .vb file)
import Namespace="System.Data"
import Namespace="System.Data.OleDb"