|
-
May 16th, 2008, 04:56 PM
#1
Thread Starter
New Member
Extract Data from Access DB to Page ASP.NET
Hi,
I am really stuck on the most simplist of tasks you would think.
I have a MS Access database with users firstnames lastnames etc...
All I want to do is pass it a variable e.g. [firstname] and pull all information in the DB and display it.
Im using:
Visual Web Developer 2005
MS Access Database
Code:
<script runat="server">
Sub OnbtnLoginClicked(ByVal s As Object, ByVal e As EventArgs)
Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("tblUsers.mdb") & ";"
Dim mySQL As String = "SELECT * FROM tblUsers WHERE firstname= & txtFirstname"
Dim MyConn As New OleDBConnection(strConn)
Dim cmd As New OleDBCommand(mySQL, MyConn)
MyConn.Open()
Dim objReader As OleDbDataReader
objReader = cmd.ExecuteReader()
While objReader.Read()
Dim firstname As String = objReader("firstname")
lblOutput.Text = firstname
End While
objReader.Close()
MyConn.Close()
End Sub
</script>
Also I am not sure which namespaces to use, I have trawled the MSDN website, but find it extremely difficult to find what im looking for.
(At the top of the .aspx page)
Code:
<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDB" %>
<%@ Import Namespace="System.Data.OleDB.OleDbDataReader" %>
Any help would be greatly appreciated.
Thanks
-
May 17th, 2008, 04:05 AM
#2
Thread Starter
New Member
Re: Extract Data from Access DB to Page ASP.NET
-
May 17th, 2008, 04:34 AM
#3
Re: Extract Data from Access DB to Page ASP.NET
Let's look at your query.
Code:
"SELECT * FROM tblUsers WHERE firstname= & txtFirstname"
Because the entire thing is in double quotes, this will be translated, for MS Access to be
SELECT * FROM tblUsers WHERE firstname = & txtFirstname
So MS Access is going to get confused. It is expecting a string with a name such as 'John', but instead you've passed it an ampersand and a crypting - what it assumes - column name.
Code:
"SELECT * FROM tblUsers WHERE firstname='" & txtFirstname &"';"
-
May 17th, 2008, 05:55 AM
#4
Thread Starter
New Member
Re: Extract Data from Access DB to Page ASP.NET
Hi Mendhak,
Thank you for the reply.
I have made the changes as you suggested,
Code:
Dim mySQL As String = "SELECT * FROM tblUsers WHERE firstname=" '& txtFirstname &'";"
now I have another error:
Code:
Syntax error (missing operator) in query expression 'firstname='.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.OleDb.OleDbException: Syntax error (missing operator) in query expression 'firstname='.
Source Error:
Line 26:
Line 27: Dim objReader As OleDbDataReader
Line 28: objReader = cmd.ExecuteReader()
Line 29:
Line 30: While objReader.Read()
Thanks again for your help.
-
May 17th, 2008, 06:39 AM
#5
Fanatic Member
Re: Extract Data from Access DB to Page ASP.NET
 Originally Posted by stuartornum
Code:
Dim mySQL As String = "SELECT * FROM tblUsers WHERE firstname=" '& txtFirstname &'";"
Code:
Dim mySQL As String = "SELECT * FROM tblUsers WHERE firstname="' & txtFirstname.text &'";"
-
May 17th, 2008, 06:50 AM
#6
Thread Starter
New Member
Re: Extract Data from Access DB to Page ASP.NET
Hi killer7k,
Sorry that didn't work either, I have also tried:
Code:
Dim input As String = txtFirstname.Text
Dim mySQL As String = "SELECT * FROM tblUsers WHERE firstname=" ' & input &'";"
Still no luck..
-
May 17th, 2008, 11:15 AM
#7
Re: Extract Data from Access DB to Page ASP.NET
Guys. Look at post #3 closely. Look at how I have created the string there. Look at the single quote and the double quote. If it still isn't obvious, just post back.
Hell, just copy and paste it.
-
May 17th, 2008, 11:24 AM
#8
Thread Starter
New Member
Re: Extract Data from Access DB to Page ASP.NET
mendhak,
I must say I am totally ashamed of myself. lol!
Thank you for your Help!!
All working hunky doory!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|