Solved

MS Access with ADO.NET in VB.NET

Posted on 2003-11-16
5
937 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 70

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 70

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 70

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Path of Workbook 3 77
Scripting vs. Programming languages 25 165
Java Loop 6 59
iframe detection of parent window scale 20 72
This is an explanation of a simple data model to help parse a JSON feed
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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 …
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…

776 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