Solved

Very Simple Login form with MsAccess database

Posted on 2009-05-06
16
620 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:zebrain777
Comment Utility
??? 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Dear Vee,

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

Rob
0
 

Author Closing Comment

by:zebrain777
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

11 Experts available now in Live!

Get 1:1 Help Now