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

zebrain777Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Bob HoffmanConnect With a Mentor DeveloperCommented:
If you want a DataSet to bind to a data-aware control use this code:  

            Dim oCn As New OleDb.OleDbConnection(<< your connection string>>)
            Dim oCmd As New OleDb.OleDbCommand(<< your SQL statement>>, oCn)
            'Open the database connection and execute the command
            Dim oAdapter As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter
            oAdapter.SelectCommand = oCmd
            Dim oDataSet As DataSet = New DataSet
            oAdapter.Fill(oDataSet)
 
If you want DataReader to loop through the row/cloumns in code use this code:

            Dim oCn As New OleDb.OleDbConnection(<< your connection string>>)
            Dim oCmd As New OleDb.OleDbCommand(<< your SQL statement>>, oCn)
            'Open the database connection and execute the command
            oCn.Open()
            Dim oDataReader As OleDb.OleDbDataReader = oCmd.ExecuteReader()
 
I didn't test this but I beleive it's complete.
0
 
zebrain777Author Commented:
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
0
 
zebrain777Author Commented:
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)
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
zebrain777Author Commented:
Problem solved.

I had to place

Imports System.Data
Imports System.Data.OleDb

before the partial class in the .vb file
0
 
Bob HoffmanDeveloperCommented:
How did you solve your ORIGINAL problem? You should list the resolution here for others that might have a similar problem.
0
 
zebrain777Author Commented:
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

0
 
Bob HoffmanDeveloperCommented:
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.

0
 
zebrain777Author Commented:
??? 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.
 
0
 
Bob HoffmanDeveloperCommented:
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.
0
 
zebrain777Author Commented:
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.
0
 
Bob HoffmanDeveloperCommented:
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?
0
 
zebrain777Author Commented:
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


 
0
 
Bob HoffmanDeveloperCommented:
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!
0
 
zebrain777Author Commented:
Dear Vee,

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

Rob
0
 
zebrain777Author Commented:
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"
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.