Solved

MS Access with ADO.NET in VB.NET

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

706 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

21 Experts available now in Live!

Get 1:1 Help Now