Imports System.IO
Imports System.Data.OleDb.OleDbException

Public Class Form2
    Inherits System.Windows.Forms.Form
    Dim inc As Integer
    Dim MaxRows As Integer
    Dim con As New OleDb.OleDbConnection
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String
    Dim username As String

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Login.Click

        con.ConnectionString = "PROVIDER=Microsoft.JET.OLEDB.4.0;Data Source = E:\vasim\Cloun\database table\school.accdb"
      
 con.Open()
       
   If sql = "SELECT * FROM table1 WHERE Username ID and Password              = 'UserName.text' ,'PassWord.text'" Then
            MsgBox("login successfull")
            Me.Hide()
    Else
            MsgBox("Incorrect Login Details - Please try again.")
        End If

        con.Close()

This is code for login form, while runnig this show msg "OledbExecption was unhandaled"

Recommended Answers

All 12 Replies

Imports System.Data.OleDb
Public Class Form1

   'Code From :- RajSoftCreation - SriLanka - NIBM  - R.H.H.D.Kumara
   'Mr.vasim jada Main Problem is your database Table school.accdb Field name called
   'Username ID separate by space delete that space and rename it like  Username_ID
   'below coding will help you / if not work create database as school.mdb (Ms Access 2003 Format)
   'post me if this code not worked Good Luck
     

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim con1 As OleDbConnection
        Dim mad As OleDbDataAdapter
        Dim mread As OleDbDataReader

        con1 = New OleDbConnection("PROVIDER=Microsoft.JET.OLEDB.4.0;Data Source = <path of the database is saved>")
        con1.Open()


        mad = New OleDbDataAdapter("select * from table1 where Username_ID = @id and Password = @pass", con1)

       

        mad.SelectCommand.Parameters.AddWithValue("@id", UserName.Text)
        mad.SelectCommand.Parameters.AddWithValue("@pass", PassWord.Text)

        mread = mad.SelectCommand.ExecuteReader()

        If mread.HasRows = True Then
            MsgBox("Log On Successfully")
            Me.Hide()

        Else
            MsgBox("Incorrect Login Details - Please try again.")

        End If

    End Sub
End Class

Thanks i will try tomorrow

Thanks I will try tomorrow

Mr. vasim jada Is My Code is Worked??

Hello Mr.R.H.H.D.Kumara

Code is not working shows following errors.
1).'Text' is not a member of 'string'
2).'Text' is not a member of 'string'

Mr. vasim jada ,

I think your GUI windows Application Design having a Two Text Boxes with the name property

{ Text Box 1 :- UserName 
                Text Box 2 :- PassWord  }

And we cannot code as

  'UserName.text' ,'PassWord.text' 

Correct Code is : -

     UserName.Text
     PassWord.Text

Dont Type Like :-

UserName.'Text' or 'UserName.Text'

In this case we pass the parameters to the assigned variable @id - UserName.Text and @pass - PassWord.Text

    mad.SelectCommand.Parameters.AddWithValue("@id", UserName.Text)
    mad.SelectCommand.Parameters.AddWithValue("@pass", PassWord.Text)

I think This will definitely work, reply me If This Code Not worked,

Ok Good Luck
Mr.vasim jada.

Hi Mr.R.H.H.D.Kumara
Show problem at
con1.Open()
"OledbExecption was unhandaled"

Hi Mr.vasim jada
you have to close your database when your .net application is running...
if you are open your table when the application is running you will get that kind of error , indication in line ( con.open () )

Hi Mr.R.H.H.D.Kumara

When I go with my code using if statement as

If sql = "Select User_id,Password from table1 WHERE [User_id] ='" & Trim(user_id.Text) & "' AND [Password =]'" & Trim(Password.Text) & "' " Then

Cursor direclty focused on else part of if statement under any condition.

Hi Mr.R.H.H.D.Kumara

When i used code provided by you then OledbExecption was unhandaled

'Place all declarations before below codes
'am sure you would see your errors especialy in your sql statement

Dim cmd As OleDbCommand
Dim sql = "SELECT * FROM Table1 WHERE Username = '" & Username.Text & "' AND Password = '" & Password.Text & "'"

cmd = New OleDbCommand(sql, con)
Dim dr As OleDbDataReader = cmd.ExecuteReader

Try

conn.Open()
If dr.Read = False Then
MessageBox.Show("Authentication Failed...")
Else
MessageBox.Show("Login Successful...")

End If

Catch ex As Exception
MsgBox(ex.Message)

End Try

'Place all declarations before below codes
'am sure you would see your errors especialy in your sql statement

Dim cmd As OleDbCommand
Dim sql = "SELECT * FROM Table1 WHERE Username = '" & Username.Text & "' AND Password = '" & Password.Text & "'"

cmd = New OleDbCommand(sql, con)
Dim dr As OleDbDataReader = cmd.ExecuteReader

Try

conn.Open()
If dr.Read = False Then
    MessageBox.Show("Authentication Failed...")
Else
    MessageBox.Show("Login Successful...")
End If

Catch ex As Exception
	MsgBox(ex.Message)

End Try
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.