How to simultaneously update an Access table and a DataGridView control? - sql

I have a .NET application, where some of its methods updates Access database tables.
My problem is the DataGridView control that displays data, is not updating.
To do it, I have to restart the application, which is not something I desire.
Public Class frmUsers
Dim cnn3 = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Renz\Documents\Visual Studio 2012\FINAL\Database\AuditDB.mdb;")
Dim sql2 As String
Dim ds1 As New DataSet
Dim adptr As OleDbDataAdapter
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim Command1 As New OleDbCommand
Dim i2 As Integer
Dim sql1 As String
Dim Status As String
Try
Dim cnn3 = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Renz\Documents\Visual Studio 2012\FINAL\Database\AuditDB.mdb;")
cnn3.Open()
If ComboBox1.SelectedValue = "Admin" Then
Status = "Admin"
Else
Status = "Student"
End If
sql1 = "INSERT INTO Users ([ID],[PASSWORD],[LASTNAME],[FIRSTNAME],[LOGINTYPE]) VALUES('" & txtUID.Text & "','" & txtUPassword.Text & "','" & txtULastname.Text & "','" & txtUFirstName.Text & "','" & Status & "')"
Command1 = New OleDbCommand(sql1, cnn3)
i2 = Command1.ExecuteNonQuery
MessageBox.Show("Users Added Successfull")
Catch ex As Exception
End Try
End Sub
Private Sub btnback_Click(sender As Object, e As EventArgs) Handles btnback.Click
Me.Hide()
frmFaculty.Show()
End Sub
Private Sub frmUsers_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim cnn4 = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Renz\Documents\Visual Studio 2012\FINAL\Database\AuditDB.mdb;")
sql2 = "Select * from Users"
Try
cnn4.Open()
adptr = New OleDbDataAdapter(sql2, cnn4)
adptr.Fill(ds1)
DataGridView1.DataSource = ds1.Tables(0)
Catch ex As Exception
End Try
cnn4.Close()
End Sub
End Class

You need to update it mannually.
Try this:
Dim cnn3 = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Renz\Documents\Visual Studio 2012\FINAL\Database\AuditDB.mdb;")
Dim sql2 As String
Dim ds1 As New DataSet
Dim adptr As OleDbDataAdapter
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim Command1 As New OleDbCommand
Dim i2 As Integer
Dim sql1 As String
Dim Status As String
Try
Dim cnn3 = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Renz\Documents\Visual Studio 2012\FINAL\Database\AuditDB.mdb;")
cnn3.Open()
If ComboBox1.SelectedValue = "Admin" Then
Status = "Admin"
Else
Status = "Student"
End If
sql1 = "INSERT INTO Users ([ID],[PASSWORD],[LASTNAME],[FIRSTNAME],[LOGINTYPE]) VALUES('" & txtUID.Text & "','" & txtUPassword.Text & "','" & txtULastname.Text & "','" & txtUFirstName.Text & "','" & Status & "')"
Command1 = New OleDbCommand(sql1, cnn3)
i2 = Command1.ExecuteNonQuery
MessageBox.Show("Users Added Successfull")
Refresh()
Catch ex As Exception
End Try
End Sub
Private Sub btnback_Click(sender As Object, e As EventArgs) Handles btnback.Click
Me.Hide()
frmFaculty.Show()
End Sub
Private Sub frmUsers_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Refresh()
End Sub
Private Sub Refresh()
Dim cnn4 = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Renz\Documents\Visual Studio 2012\FINAL\Database\AuditDB.mdb;")
sql2 = "Select * from Users"
Try
cnn4.Open()
adptr = New OleDbDataAdapter(sql2, cnn4)
adptr.Fill(ds1)
DataGridView1.DataSource = ds1.Tables(0)
Catch ex As Exception
End Try
cnn4.Close()
End Sub

Related

Vb.net cant save picture using the ms.access Database

Okay please forgive me if this sound annoying but i keep getting an error nullreference whenever im trying to input my data.. Im sorry if im doing this wrong This is my first time posting here :
Imports System.IO
Imports System.Data.OleDb
Public Class halaman_daftar_anggota
Dim Conn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim CMD As OleDbCommand
Dim bytimage As Byte()
Dim LokasiDB As String
Sub Koneksi()
LokasiDB = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=perpustakaan.accdb"
Conn = New OleDbConnection(LokasiDB)
If Conn.State = ConnectionState.Closed Then Conn.Open()
End Sub
Private Sub halaman_daftar_anggota_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Koneksi()
da = New OleDbDataAdapter("Select * from data_anggota", Conn)
ds = New DataSet
ds.Clear()
da.Fill(ds, "data_anggota")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim dialog As OpenFileDialog = New OpenFileDialog()
dialog.Title = "Browse Foto"
dialog.Filter = "image files(*.png; *.bmp; *.jpg;*.jpeg; *.gif; |*.png; *.bmp; *.jpg;*.jpeg; *.gif;)"
If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
PictureBox1.Image = Image.FromFile(dialog.FileName)
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim ms As New System.IO.MemoryStream
Dim bmpimage As New Bitmap(PictureBox1.Image)
bmpimage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
bytimage = ms.ToArray()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Call Koneksi()
Dim simpan As String = "INSERT INTO data_buku (nomor_induk,nama_siswa,kelas_siswa,foto) values ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "',#image)"
CMD.Parameters.AddWithValue("#image", bytimage) <-"Error object reference not set to an instance of an object"
CMD = New OleDbCommand(simpan, Conn)
CMD.ExecuteNonQuery()
MsgBox("Berhasil")
End Sub
End Class
Whenever im trying to save a picture.in my database j already set foto as ole object... Im sorry..

Forgot Password Form Issues VB.net

I am trying to create a forgot password screen for my application. I am using the tab control for the different pages. My current code is able to create a user but it is able to create duplicates (Which is an issue needed to be rectified) I then have an issue with the forget password screen not working completely.
My code is:
Imports System.Data.OleDb
Public Class Form2
Dim connection As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim MyDocumentsFolder As String
Dim TheDatabase As String
Dim FullDatabasePath As String
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
Dim DBTest1 As String
Dim DBTestP1 As String
Dim cmd As New OleDbCommand(sql, connection)
Dim connStr As String
Dim usernamevalid As Integer
Dim passwordvalid As Integer
Public Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
If User.Text.Length < 4 Then
usernamevalid = 0
ElseIf User.Text.Length > 4 Then
usernamevalid = 1
End If
If Pass.Text.Length < 5 Then
passwordvalid = 0
ElseIf Pass.Text.Length > 5 Then
passwordvalid = 1
End If
If usernamevalid = 0 Then
MsgBox("Username Must Be At Least 5 Characters")
End If
If passwordvalid = 0 Then
MsgBox("Password Must Be At Least 5 Characters")
End If
If passwordvalid And usernamevalid = 1 And Pass.Text = RePass.Text Then
dbProvider = "Provider=Microsoft.ACE.OLEDB.12.0;"
dbSource = "Data Source = C:\Users\Daniel\Documents\Robocopy.accdb"
Dim connStr = dbProvider & dbSource
DBTest1 = User.Text
DBTestP1 = Pass.Text
sql = "INSERT INTO Robocopy(username,[password],sq,sqa) VALUES('" & DBTest1 & "','" & DBTestP1 & "','" & SQREG.Text & "', '" & SQAREG.Text & "')"
Using connection = New OleDb.OleDbConnection(connStr)
Using cmd = New OleDb.OleDbCommand(sql, connection)
connection.Open()
cmd.ExecuteNonQuery()
connection.Close()
MsgBox("User Created!")
'With cmd.Parameters
'.AddWithValue("usernamer", DBTest.Text)
'.AddWithValue("password", DBTestP.Text)
'.AddWithValue("email", txtsub.text)
'.AddWithValue("contactnum", txtau.text)
'End With
'cmd.ExecuteNonQuery()
End Using
End Using
ElseIf Not Pass.Text = RePass.Text Then
MsgBox("Passwords did not match")
End If
End Sub
Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click
Dim result = MessageBox.Show(" Are you sure you want to quit", "Are you sure?", MessageBoxButtons.YesNoCancel)
Me.Close()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
If NewPass.Text = ReNewPass.Text Then
Try
connection.Open()
cmd = New OleDbCommand("update robocopy set [password] = '" & NewPass.Text & "' where username = '" & UserFGT.Text & "'", connection)
cmd.ExecuteNonQuery()
MessageBox.Show("PASSWORD CHANGE SUCCESSFULLY")
connection.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
End Class
The exception it catches is The ConnectionString property has not been initialized
Currently I changed my code to what I think you mean:
Button4's code has been changed as such:
Private Sub ResetPassword_Click(sender As Object, e As EventArgs) Handles ResetPassword.Click
If NewPass.Text = ReNewPass.Text Then
Using connection = New OleDb.OleDbConnection(connStr)
Using cmd = New OleDb.OleDbCommand(sql, connection)
connection.Open()
cmd = New OleDbCommand("update robocopy set [password] = '" & NewPass.Text & "' where username = '" & UserFGT.Text & "'", connection)
cmd.ExecuteNonQuery()
MessageBox.Show("PASSWORD CHANGE SUCCESSFULLY")
connection.Close()
End Using
End Using
End If
End Sub
I now get the error for cmd = New OleDbCommand("update robocopy set [password] = '" & NewPass.Text & "' where username = '" & UserFGT.Text & "'", connection)
I am getting the error for cmd where it says 'Read Only' Variable cannot be the target of assignment
The only thing I can see wrong with the connection string are extra spaces after 'Data Source='.
Here's an example from connectionstrings.com:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;Persist Security Info=False;

How to get count of records in a table?

Someone help me
I am working on our project and I need to check if my DB has already 20 records.
If so, then it will not accept records anymore.
I've been trying the codes below:
Public Class Form1
Dim con As New OleDb.OleDbConnection
Dim ds, ds2 As New DataSet
Dim da, da2 As OleDb.OleDbDataAdapter
Dim sql, sql1 As String
Dim int As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
con.ConnectionString = "Provider=Microsoft.jet.OLEDB.4.0; data source = |datadirectory|\Database6.mdb"
con.Open()
Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand("SELECT * FROM Accounts WHERE Username='" & TextBox1.Text & "'", con)
Dim sdr As OleDb.OleDbDataReader = cmd.ExecuteReader
Dim cmd1 As OleDb.OleDbCommand = New OleDb.OleDbCommand("SELECT * FROM Accounts")
sql = "INSERT INTO Accounts ([Username], [Password], [FirstName], [LastName]) VALUES ('" & TextBox1.Text & "','" & TextBox2.Text & "', '" & TextBox3.Text & "','" & TextBox4.Text & "') "
sql1 = "SELECT Count([AccountID]) FROM Accounts"
cmd = New OleDb.OleDbCommand(sql, con)
cmd1 = New OleDb.OleDbCommand(sql1, con)
Convert.ToInt32(sql1)
cmd1.ExecuteScalar()
If sql1 < 20 Then
MsgBox("Cannot accept records")
ElseIf sdr.HasRows = False Then
cmd.ExecuteNonQuery()
MsgBox("Account Added")
ElseIf sdr.HasRows = True Then
MsgBox("Username is taken")
End If
con.Close()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Hide()
Form2.Show()
End Sub
End Class
But the convert code fires an error :
Input string was in incorrect format
But if I delete the convert code it gives me the error
Conversion from string "SELECT Count([AccountID]) FROM A" to type 'Double' is not valid."
Help me please.
TIA
I dont know VB all that well, this is from the top of my head. Your trying to convert your SQL text, which will never work. Try something like this:
dim result as object
result = cmd1.ExecuteScalar()
dim count as int
count = Convert.ToInt32(result)
If count < 20 Then

vb.net Data Gridview REfresh

i am having some issues updating a table that i have in a VB.net Windows Forms in VS 2012
i have setup the database from a service Data base, it just need a small amount of data stored locally
what i am trying to do when i create a new user it update the data grid view
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
BDate1.Format = DateTimePickerFormat.Custom
BDate1.CustomFormat = "MM/dd/yyyy"
If fname.Text <> "" And lname.Text <> "" Then
If Not cn.State = ConnectionState.Open Then
cn.Open()
End If
' cn1.Open()
If rb1.Checked Then
gen = rb1.Text.ToString
ElseIf rb2.Checked Then
gen = rb2.Text.ToString
End If
cmd.CommandText = "INSERT INTO StudentTB (FirstName,LastName,Birthday,Avatar,Gender,Grade) values('" & fname.Text & "', '" & lname.Text & "', '" & BDate1.Text & "', '" & AvtarNM.Text & "', '" & gen & "', '" & TxtGrade.Text & "')"
Dim dt As New DataTable
dt.Load(cmd.ExecuteReader())
DataGridView1.DataSource = dt
' cmd.ExecuteNonQuery()
cn.Close()
fname.Text = ""
lname.Text = ""
the issue is that i can clear the table and reload it with button but it does not show the updated values with out closing the application then reopening it, i have tried a few thing and nothing seems to refresh the connection and or update the database. any help would help.
thanks
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
'DataGridView1.DataSource = Nothing
Database1DataSet1.StudentTB.Clear()
' Database1DataSet1.refresh()
' Dim myda As String
' cmd.Dispose()
' cmd.Connection = cn
'Me.Database1DataSet1.Clear()
Me.StudentTBTableAdapter.Fill(Me.Database1DataSet1.StudentTB)
' 'myda = New SqlDataAdapter(cmd)
End Sub
here is the full code
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlClient.SqlDataReader
Imports System.Windows.Forms
Public Class Student
Dim cn As New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Nate\documents\visual studio 2012\Projects\WindowsApplication9\WindowsApplication9\Database1.mdf;Integrated Security=True")
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
Private dataAdapter As New SqlDataAdapter()
Dim gen As String
Dim bs As New BindingSource
Dim dt As New DataTable
Private Sub Student_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Database1DataSet1.avTable' table. You can move, or remove it, as needed.
Me.AvTableTableAdapter.Fill(Me.Database1DataSet1.avTable)
'TODO: This line of code loads data into the 'Database1DataSet1.StudentTB' table. You can move, or remove it, as needed.
Me.StudentTBTableAdapter.Fill(Me.Database1DataSet1.StudentTB)
cmd.Connection = cn
Form1.Hide()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If AvTableBindingSource.Position + 1 < AvTableBindingSource.Count Then
AvTableBindingSource.MoveNext()
Button7.PerformClick()
' Otherwise, move back to the first item.
Else
AvTableBindingSource.MoveFirst()
Button7.PerformClick()
End If
' Force the form to repaint.
Me.Invalidate()
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
PictureBox1.Image = System.Drawing.Bitmap.FromFile(ID.Text)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
BDate1.Format = DateTimePickerFormat.Custom
BDate1.CustomFormat = "MM/dd/yyyy"
If fname.Text <> "" And lname.Text <> "" Then
If Not cn.State = ConnectionState.Open Then
cn.Open()
End If
' cn1.Open()
If rb1.Checked Then
gen = rb1.Text.ToString
ElseIf rb2.Checked Then
gen = rb2.Text.ToString
End If
cmd.CommandText = "INSERT INTO StudentTB (FirstName,LastName,Birthday,Avatar,Gender,Grade) values('" & fname.Text & "', '" & lname.Text & "', '" & BDate1.Text & "', '" & AvtarNM.Text & "', '" & gen & "', '" & TxtGrade.Text & "')"
cmd.ExecuteNonQuery()
dt.Load(cmd.ExecuteReader())
bs.DataSource = dt
DataGridView1.DataSource = bs
cn.Close()
fname.Text = ""
lname.Text = ""
End If
End Sub
Private Sub genPCI()
If rb1.Checked Then
gen = rb1.Text.ToString
ElseIf rb2.Checked Then
gen = rb2.Text.ToString
End If
End Sub
Public Function ChangeFormat(ByVal dtm As DateTime, ByVal format As String) As String
Return dtm.ToString(format)
End Function
Private Sub Closestudent_Click(sender As Object, e As EventArgs) Handles Closestudent.Click
Form1.Show()
Me.Close()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
bs.EndEdit()
dataAdapter.Update(dt)
DataGridView1.DataSource = dt
' dt.Load(Command.ExecuteReader())
'Database1DataSet1.Reset()
'DataGridView1.Columns.Clear()
' DataGridView1.DataSource = Nothing
' Me.StudentTBTableAdapter.Fill(Me.Database1DataSet1.StudentTB)
' StudentTBBindingSource.ResetBindings(True)
' Database1DataSet1.refresh()
' Dim myda As String
' cmd.Dispose()
' cmd.Connection = cn
' StudentTBBindingSource.ResetBindings(False)
'Database1DataSet1.StudentTB.Clear()
' 'myda = New SqlDataAdapter(cmd)
' DataGridView1.DataSource = Database1DataSet1.StudentTB
End Sub
End Class
You can try like this
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
bs.EndEdit()
dataadapter.Update(dt)
DataGridView1.DataSource = dt
End Sub
UPDATE
or you can use bindingdatasource and delare it on your form class
Dim bs As New BindingSource
and in datagridview .. in button1_click
Dim dt As New DataTable
dt.Load(cmd.ExecuteReader())
bs.DataSource = dt
DataGridView1.DataSource = bs
So, if updated then will show after button4_click
Datagridview1.datasource = nothing
Dataset.reset
dim cmd as new sqldataadapter
cmd = "SELECT * FROM YOUR_DATABSE", con
con.open
cmd.fill(Dataset)
datagridview1.datasource = dataset(0).defaultview
Add these code to you button insert
okay after a lot of playing around this seem to fix my issue http://msdn.microsoft.com/en-us/library/fbk67b6z.aspx well all i can say it is working no idea why but it works.
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Me.DataGridView1.DataSource = Me.StudentTBBindingSource
GetData("select * from StudentTB")
End Sub
Private Sub GetData(ByVal selectCommand As String)
Try
' Specify a connection string. Replace the given value with a
' valid connection string for a Northwind SQL Server sample
' database accessible to your system.
Dim connectionString As String = "Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Nate\documents\visual studio 2012\Projects\WindowsApplication9\WindowsApplication9\Database1.mdf;Integrated Security=True"
' Create a new data adapter based on the specified query.
Me.dataAdapter = New SqlDataAdapter(selectCommand, connectionString)
' Create a command builder to generate SQL update, insert, and
' delete commands based on selectCommand. These are used to
' update the database.
Dim commandBuilder As New SqlCommandBuilder(Me.dataAdapter)
' Populate a new data table and bind it to the BindingSource.
Dim table As New DataTable()
table.Locale = System.Globalization.CultureInfo.InvariantCulture
Me.dataAdapter.Fill(table)
Me.StudentTBBindingSource.DataSource = table
' Resize the DataGridView columns to fit the newly loaded content.
' Me.DataGridView1.AutoResizeColumns( _
' DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader)
Catch ex As SqlException
MessageBox.Show("To run this example, replace the value of the " + _
"connectionString variable with a connection string that is " + _
"valid for your system.")
End Try
End Sub
By Using ComboBox To Search
sql = " select * from english_language where lang=' " & ComboBox2.Text & " ' "
da = New OleDb.OleDbDataAdapter(sql, con)
Dim dt As New DataTable
da.Fill(dt)
DataGridView1.DataSource = dt
DataGridView1.Refresh()
Try this code to btnClear.
DataGridView.DataSource = 0
Press clear button. After that click the reload button that you coded.
I think it will reset and reload the data in data grid view.

login form using ms access in vb.net

I'm creating a login form for vb.net using ms access 2003 as database. But it only checks for the username and bypasses the password. Meaning that if the username is correct and the password doesn't jive with the username, the user can still enter the system. Here is my code:
Try
Dim NoAcc As String
Dim NoAccmod2 As String
Dim NoPas As String
Dim cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\db1.mdb;Jet OLEDB:Database Password=nrew123$%^;")
Dim cmd As OleDbCommand = New OleDbCommand("Select * from admintable where AdminName= '" & TextBox4.Text & "' ", cn)
cn.Open()
rdr = cmd.ExecuteReader
If rdr.HasRows Then
rdr.Read()
NoAcc = rdr("AdminName")
NoPas = rdr("AdminPass")
If (TextBox4.Text = NoAcc And TextBox3.Text = NoPas) Then NoAccmod2 = NoAcc
adminview.Show()
Me.Hide()
Else
MsgBox("Incorrect Username/Password")
TextBox4.Clear()
TextBox3.Clear()
End If
Catch
MsgBox("Error logging in, please try again", MsgBoxStyle.Exclamation)
End Try
How do I do it so that it checks both username and password?
You are using a single line IF .. THEN :
If (TextBox4.Text = NoAcc And TextBox3.Text = NoPas) Then NoAccmod2 = NoAcc
so the next line will always be executed:
adminview.Show()
you have to rearrange your IF .. THEN conditions
I will share about login system in vb.net using binding navigator that less of coding. just following the link below!
http://www.tesear.com/2011/09/login-system-in-vbnet.html
You could have BOTH the uname and pword in the database and "WHERE" on both, if you get no record back, then you have your answer.
Try using System.String.Compare(String str1,String str2, Boolean ) As Integer like:
If (System.String.Compare(TextBox4.Text, NoAcc, false) And System.String.Compare(TextBox3.Text, NoPas, false)) Then NoAccmod2 = NoAcc
here is the code:
Imports System.Data
Imports System.Data.OleDb
Public Class Form5
Inherits System.Windows.Forms.Form
Dim mypath = Application.StartupPath & "\login.mdb"
Dim mypassword = ""
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mypath & ";Jet OLEDB:Database Password=" & mypassword)
Dim cmd As OleDbCommand
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Hide()
Dim sql = "SELECT UserID ,PassID FROM MYTAB WHERE USERID='" & TextBox1.Text & "' AND PASSID='" & TextBox2.Text & "'"
cmd = New OleDbCommand(sql, conn)
conn.Open()
Dim dr As OleDbDataReader = cmd.ExecuteReader
Try
If dr.Read = False Then
MessageBox.Show("Authentication failed...")
Me.Show()
Else
MessageBox.Show("Login successfully...")
Dim frmDialogue As New Form11
frmDialogue.ShowDialog()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
conn.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Close()
End Sub
Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
Me.Hide()
Dim frmDialogue As New Form1
frmDialogue.ShowDialog()
End Sub
Private Sub Form5_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Dim frm As New Form1
frm.Show()
End Sub
End Class
Try
Dim NoAcc As String
Dim NoPas As String
Dim rdr As OleDbDataReader
Dim cnn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\mobilestore.accdb;Persist Security Info=False;")
Dim cmd As OleDbCommand = New OleDbCommand("Select * from logindata where Username= '" & TextBox1.Text & "' and password='" & TextBox2.Text & "'", cnn)
'NoAcc = TextBox1.Text
'NoPas = TextBox2.Text
cnn.Open()
rdr = cmd.ExecuteReader
If (rdr.Read()) Then
NoAcc = rdr("Username")
NoPas = rdr("password")
If (TextBox1.Text = NoAcc And TextBox2.Text = NoPas) Then
Adminpage.Show()
Me.Hide()
Else
MsgBox("Incorrect Username/Password")
TextBox1.Clear()
TextBox2.Clear()
End If
End If
Catch
MsgBox("Error logging in, please try again", MsgBoxStyle.Exclamation)
End Try
End Sub