Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MS Access with ADO.NET in VB.NET

Posted on 2003-11-16
5
Medium Priority
?
945 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Starting up a Project
Loops Section Overview

876 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