4 Replies - 4264 Views - Last Post: 04 February 2010 - 09:36 PM Rate Topic: -----

#1 mayoman7   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-December 09

Retrieving Data from an MS Access Database

Posted 04 February 2010 - 07:42 PM

I've seen lots of other topics started on this, and I have already researched them, but none seem to accomplish quite what I would like. I'll be honest and say I'm completely new to ASP.NET but I do know .NET so I'm not completely at a loss.

Basically I want a webpage with a login form on it. Two textboxes and a submit. That's already done (I'm not that slow). I'm trying to compare the inputed username and password to those stored in a table in an Access database. By utilizing a Data Repeater, I have the one, exact piece of information I need, but I don't know how to store it from the repeater and compare it to "txtPassword.Text" to validate the user.

Through multiple trial and error, I've realized just how picky ASP.NET script is. One wrong character and the page won't even load.

Here is my code.
<%@ Import Namespace="System.Data.OleDb" %>

<script language="VB" runat="server">

sub Page_Load
    dim dbconn,sql,dbcomm,dbread
    dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & server.mappath(~database))
    dbconn.Open()
    sql="SELECT password FROM tblUsers where username = '" & txtUsername.Text & "';"
    dbcomm=New OleDbCommand(sql,dbconn)
    dbread=dbcomm.ExecuteReader()
    users.DataSource=dbread
    users.DataBind()
    dbread.Close()
    dbconn.Close()
end sub

</script>

<html>
<body>
<form runat="server">
<asp:Repeater id="users" runat="server">

</asp:Repeater>
</form>
</body>
</html>



In the HTML section, there were "print" statements to display it as a table. Obviously I don't want the page to show the world the password, so I simply removed it. Now using the above code renders ONE thing in the data container which is the exact thing I need. So how do I store that ONE result into a variable and compare it to txtPassword.Text?

This post has been edited by mayoman7: 04 February 2010 - 07:44 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Retrieving Data from an MS Access Database

#2 Jayman   User is offline

  • Student of Life
  • member icon

Reputation: 423
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Retrieving Data from an MS Access Database

Posted 04 February 2010 - 08:20 PM

This can all be done in the Page_Load event without the use of a Repeater.

sub Page_Load
    dim dbconn,sql,dbcomm,dbread

    Dim password As String = ""

    dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & server.mappath(~database))
    dbconn.Open()
    sql="SELECT password FROM tblUsers where username = '" & txtUsername.Text & "';"
    dbcomm=New OleDbCommand(sql,dbconn)
    dbread=dbcomm.ExecuteReader()

    If dbread.HasRows Then
       dbread.Read()
       password = dbread("password")
       
       If password.Equals(txtPassword.Text) Then
            'Password matches, allow user into site
       Else
            'inform user of invalid username/password
       End If
    Else
       'inform user of invalid username/password
    End If
    
    dbread.Close()
    dbconn.Close()
end sub

Was This Post Helpful? 0
  • +
  • -

#3 mayoman7   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-December 09

Re: Retrieving Data from an MS Access Database

Posted 04 February 2010 - 08:56 PM

View PostJayman, on 04 February 2010 - 07:20 PM, said:

This can all be done in the Page_Load event without the use of a Repeater.


Right, but txtPassword.Text doesn't have anything on Page_Load. So this needs to take place under, btnLogin_Click. So does the method work in that case? Sorry I forgot to mention that.
Was This Post Helpful? 0
  • +
  • -

#4 Jayman   User is offline

  • Student of Life
  • member icon

Reputation: 423
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Retrieving Data from an MS Access Database

Posted 04 February 2010 - 09:20 PM

Oopsie. Not your fault, it was mine. Totally went over my head that you said a Login page. :blink:

Yes, the code will work just fine in the btnLogin_Click event.
Was This Post Helpful? 0
  • +
  • -

#5 mayoman7   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-December 09

Re: Retrieving Data from an MS Access Database

Posted 04 February 2010 - 09:36 PM

View PostJayman, on 04 February 2010 - 08:20 PM, said:

Oopsie. Not your fault, it was mine. Totally went over my head that you said a Login page. :blink:

Yes, the code will work just fine in the btnLogin_Click event.


No big. I tried it anyway. Got an error message. Took me a minute to figure out how to actually show the error message (wasn't configured) then took me another minute to configure the debug message. Turns out, that Map.Path wasn't working right (I have no idea why) and so I hardcoded the absolute path. It works and I am forever grateful.

Thanks Jayman!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1