CRUD Tutorial With Search
CRUD Tutorial With Search
Creating the Connection between the Visual studio and the Database
7. Then click on the project menu.
8. Click Add Reference
9. Then a dialog box will appear
10. Then Click on the Browse Button
11. Then locate MySql.Data.dll
12. After locating, the MySql.Data.dll must be added on the list, then check it
Imports MySql.Data.MySqlClient
Module connection
Public cn As New MySqlConnection("Server=localhost; userid=root; password=;
database=YourDatabaseName")
Public cmd As New MySqlCommand
Public da As New MySqlDataAdapter
Public dt As New DataTable
Public dr As MySqlDataReader
End If
End Sub
End Module
cn.Open()
With cmd
.Connection = cn
.CommandText = "SELECT * from tblUsers where username ='" & txtusername.Text & "'
and password='" & txt_password.Text & "'"
End With
dr = cmd.ExecuteReader
dr.Read()
If dr.HasRows Then
Me.Hide()
'MsgBox("Welcome!", MsgBoxStyle.Information, "Successful Login")
Else
MsgBox("Incorrect Username and Password", MsgBoxStyle.Critical, "Login")
End If
cn.Close()
This is a DataGridView
***Here we will load the data from
the database
After Clicking the Edit Columns. This dialog box will appear
Preview:
We will now load the
data of our tblUsers to the
datagridview
DataGridView1.Rows.Clear()
If dr.HasRows Then
While dr.Read
DataGridView1.Rows.Add()
DataGridView1.Item(0, i).Value = dr.Item(1)
DataGridView1.Item(1, i).Value = dr.Item(2)
DataGridView1.Item(2, i).Value = dr.Item(3)
DataGridView1.Item(3, i).Value = dr.Item(4)
i = i + 1
End While
End If
cn.Close()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information)
End Try
End Sub
Step 3: Then inside the Private Sub frmUser_Load… paste the following code
LoadData()
Step 4: You can now test the program if it load the data from the database
'clear textboxes
txtName.Clear()
txtUsername.Clear()
txtPassword.Clear()
cmbRole.Text = ""
cn.Close()
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, "Ooooops Duplication of Record")
Finally
cn.Dispose()
End Try
Dim i As Integer
i = DataGridView1.CurrentRow.Index
btnSave.enabled=false
'clear textboxes
txtName.Clear()
txtUsername.Clear()
txtPassword.Clear()
cmbRole.Text = ""
End If
End Try
*******************************************************************
Like This!!!!
Note!!!
Do not forget to set the VISIBLE
property of the id label and id
textbox to FALSE
'clear textboxes
txtName.Clear()
txtUsername.Clear()
txtPassword.Clear()
cmbRole.Text = ""
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information)
Finally
cn.Dispose()
End Try
End Sub
txtName.enabled=true
txtUsername.enabled =true
txtPassword.enabled =true
cmbRole.enabled =true
Then double click the textbox and then paste the following codes:
Dim i As Integer = 0
Try
cn.Open()
With cmd
.Connection = cn
.CommandText = "select * from tblUsers where name like '%" & txtSearch.Text &
"%' or Username like '%" & txtSearch.Text & "%'"
dr = cmd.ExecuteReader
End With
DataGridView1.Rows.Clear()
If dr.HasRows Then
While dr.Read
DataGridView1.Rows.Add()
DataGridView1.Item(0, i).Value = dr.Item(1)
DataGridView1.Item(1, i).Value = dr.Item(2)
DataGridView1.Item(2, i).Value = dr.Item(3)
DataGridView1.Item(3, i).Value = dr.Item(4)
DataGridView1.Item(4, i).Value = dr.Item(0)
i = i + 1
End While
End If
cn.Close()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information)
End Try
Reflection Paper:
1. As an accountancy students, what can you say that you
can now program?