DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   ASP.NET (http://www.daniweb.com/forums/forum18.html)
-   -   login with ms access table (http://www.daniweb.com/forums/thread131231.html)

plusplus Jun 26th, 2008 5:45 am
login with ms access table
 
I have a login control, when user presses login I want to check if he exists in the table.
I made a connection to database, and now how do I check if he exists?

Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate
Dim sqlstring As String
sqlstring = "SELECT id FROM tbl_users WHERE name = " & Login1.UserName & "AND password= " & Login1.Password
Dim cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\Dokumente und Einstellungen\USER\Eigene Dateien\Login.mdb;User Id=admin;Password=;")
Dim dbcomm As New OleDbCommand(sqlstring, cn)


cn.Dispose()
End Sub

bala24 Jun 26th, 2008 6:38 am
Re: login with ms access table
 
Call Executescalar with your command object. It returns a single value like id in your case if the query is successful.

If it is nothing then the login fails, otherwise, allow the user.

plusplus Jun 26th, 2008 5:10 pm
Re: login with ms access table
 
I need some more help, I don't know how to go about it
In web.config I have
<authorization>
<deny users="?"/>
<allow users="*"/>
</authorization>
<authentication mode="Forms"/>

In my login form I have a login control with this code

Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles Login1.Authenticate
Dim sqlstring As String
sqlstring = "SELECT id FROM tbl_users WHERE name = " & Login1.UserName & "AND password= " & Login1.Password
Dim cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\Dokumente und Einstellungen\USER\Eigene Dateien\Login.mdb;User Id=admin;Password=;")
Dim dbcomm As New OleDbCommand(sqlstring, cn)


cn.Dispose()
End Sub

Now if I add to the above code
if dbcomm.executescalar <> nothing then ??????? what do I do to tell my web.config login was successfull?

bala24 Jun 27th, 2008 2:35 am
Re: login with ms access table
 
Try this article.
It will clarify your doubts about form authentication.

http://www.eggheadcafe.com/articles/20020906.asp

If you think that's difficult to digest, go through this one..

http://www.csse.monash.edu.au/course...004/login.html
Hope this helps.

plusplus Jun 27th, 2008 2:43 am
Re: login with ms access table
 
I'll take a look at it, but maybe you know a place with vb code not c?

plusplus Jun 27th, 2008 2:59 am
Re: login with ms access table
 
Quote:

Originally Posted by bala24 (Post 635642)
Try this article.
It will clarify your doubts about form authentication.

http://www.eggheadcafe.com/articles/20020906.asp

If you think that's difficult to digest, go through this one..

http://www.csse.monash.edu.au/course...004/login.html
Hope this helps.

I'll try it out and get back if I need some more help.

bala24 Jun 27th, 2008 3:02 am
Re: login with ms access table
 
Well the second url I sent is in vb and its quite easy to understand as well.

Try that one.

plusplus Jun 30th, 2008 5:00 am
Re: login with ms access table
 
Here is what I have so far, the bold line gives me an error, besides does this code look right?

Dim myid As Long
Dim sqlstring As String
Dim cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\Dokumente und Einstellungen\USER\Eigene Dateien\Login.mdb;User Id=admin;Password=;")
Dim dr As New OleDbDataReader
sqlstring = "SELECT id FROM tbl_users WHERE name = ? AND password= ?"
Dim dbcomm As New OleDbCommand(sqlstring, cn)

dbcomm.Parameters.AddWithValue("name", Login1.UserName)
dbcomm.Parameters.AddWithValue("password", Login1.Password)

If dbcomm.ExecuteScalar <> Nothing Then
dr = dbcomm.ExecuteReader()
myid = dr(1)
System.Web.Security.FormsAuthentication.RedirectFromLoginPage(myid, False)
End If

cn.Dispose()

bala24 Jun 30th, 2008 8:04 am
Re: login with ms access table
 
Well you don't need that line at all.
The ExecuteScalar method returns a single value eg the number of records affected in an update query.
In your case it holds the id of the user which is an authenticated user.

If he is not, the ExecuteScalar will return nothing.

So what you could do id this.

Dim strid as string 'Assuming your id is a string value

strid=dbcomm.ExecuteScalar()

If not strid is nothing Then
        System.Web.Security.FormsAuthentication.RedirectFromLoginPage(strid, False)
End If

Hope its clear.


All times are GMT -4. The time now is 1:40 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC