Solved

Very Simple Login form with MsAccess database

Posted on 2009-05-06
16
622 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:zebrain777
  • 9
  • 6
16 Comments
 
LVL 8

Accepted Solution

by:
Bob Hoffman earned 250 total points
ID: 24320534
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
 

Author Comment

by:zebrain777
ID: 24323589
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
 

Author Comment

by:zebrain777
ID: 24323796
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
 

Author Comment

by:zebrain777
ID: 24324300
Problem solved.

I had to place

Imports System.Data
Imports System.Data.OleDb

before the partial class in the .vb file
0
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 24325927
How did you solve your ORIGINAL problem? You should list the resolution here for others that might have a similar problem.
0
 

Author Comment

by:zebrain777
ID: 24328464
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
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 24328706
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:zebrain777
ID: 24329942
??? 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
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 24330050
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
 

Author Comment

by:zebrain777
ID: 24330277
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
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 24330435
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
 

Author Comment

by:zebrain777
ID: 24330787
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
 
LVL 8

Expert Comment

by:Bob Hoffman
ID: 24331183
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
 

Author Comment

by:zebrain777
ID: 24333539
Dear Vee,

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

Rob
0
 

Author Closing Comment

by:zebrain777
ID: 31578566
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now