Solved

MS Access with ADO.NET in VB.NET

Posted on 2003-11-16
5
935 Views
Last Modified: 2010-04-17
Hi,

I am trying to write a simple login screen where the code takes the name entered by the user and password in  a textbox and does an sql query to see if the database has it. I am using Access. The funny thing is that when I say select * from Login, it works with out an error. The error is An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll

Please Help, I am stumped.
Thanks


I have a Connection class.

The Connection code is :

Option Explicit On
'Option Strict
Imports System
Imports System.Data
Imports System.Data.OleDb
Namespace DBConnection
    Public Class DBConnect

        Dim ConnectString As String = "Provider=Microsoft.Jet.OleDb.4.0;" & _
                                                   "data source=C:\OmniTest\Test.mdb;"
        Dim myConnection As New OleDbConnection(ConnectString)
        Dim objCmd As OleDbCommand
        Dim objReader As OleDbDataReader
        Dim ds As DataSet

        Public Sub New()
            myConnection.Open()
        End Sub

        Public Function getDataSet(ByVal SqlSelect As String) As DataSet

            Dim myDataAdapter As New OleDbDataAdapter(SqlSelect, myConnection)
            Dim myDataSet As New DataSet()
            myDataAdapter.Fill(myDataSet)
            ds = myDataSet
            Return myDataSet
        End Function

        Public Function getDataReader(ByVal SqlSelect As String) As OleDbDataReader
            objCmd = New OleDbCommand(SqlSelect, myConnection)
            Return objCmd.ExecuteReader()
        End Function

        Public Function doUpdate(ByVal SqlManipulate As String) As Integer
            objCmd = New OleDbCommand(SqlManipulate, myConnection)
            Return objCmd.ExecuteNonQuery()
        End Function

        Public Function getRow(ByVal ds As DataSet, ByVal row As Integer) As DataRow
            Dim objTable As DataTable = ds.Tables(0)
            Return objTable.Rows(row)
        End Function

        Public Function getField(ByVal FieldName As String, ByVal I As Integer) As VariantType
            Dim objRow As DataRow
            Dim objTable As DataTable = ds.Tables(0)
            objRow = objTable.Rows(I)
            Return objRow(FieldName)
        End Function

        Protected Overrides Sub Finalize()
            myConnection.Close()
        End Sub

    End Class

End Namespace


The code on my button click is:

         Dim name As String = txtUser.Text
        MsgBox(name)
        Dim pwd As String = txtPwd.Text
        MsgBox(pwd)
        SQLString1 = "SELECT Username, Password FROM Login WHERE Username=" + name
         objDBConnect.getDataSet(SQLString1)

I even tried:

Dim strConnection As String = "Provider=Microsoft.Jet.OleDb.4.0;" & _
                                   "data source=C:\OmniTest\Test.mdb;"
        Dim objConnection As New OleDbConnection(strConnection)
        objConnection.Open()
        MsgBox("Open")

        Dim strSQL As String = "SELECT Username, Password FROM Login WHERE Username = " + name
        Dim objAdapter As New OleDbDataAdapter(strSQL, objConnection)
        Dim objDataSet As New DataSet()
        objAdapter.Fill(objDataSet, "Login")
        Dim Login As DataTable = objDataSet.Tables("Login")
       

        objConnection.Close()

0
Comment
Question by:PhillyZoo
  • 3
  • 2
5 Comments
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 9758645
You need to surround the content of the variable with queotes:

Dim strSQL As String = "SELECT Username, Password FROM Login WHERE Username = '" + name + "'"
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 9759870
will you close that one too?
0
 

Author Comment

by:PhillyZoo
ID: 9759884
Yes, thanks
0
 

Author Comment

by:PhillyZoo
ID: 9759887
I knew it was something small that I was forgeting. For some reason I thought that only numbers needed the quotes around the parameter.

Have a Good Day
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 9760095
>>For some reason I thought that only numbers needed the quotes around the parameter.

Numbers do not need to quotes. Strings need quotes. Dates needs #.
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

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 70
How  do I get an older program to run in Windows 10? 20 104
python sqlite question 11 46
Auto-indent certain lines in Notepad++ 10 35
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
A short article about a problem I had getting the GPS LocationListener working.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

863 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

23 Experts available now in Live!

Get 1:1 Help Now