Public Class frmEdit
Dim strID As String = ""
Public Property _strID() As String
Get
Return strID
End Get
Set(ByVal value As String)
strID = value
End Set
End Property
Private Sub frmEdit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myConnection As MySqlConnection
Dim dt As New DataTable
Dim dtAdapter As MySqlDataAdapter
myConnection = New MySqlConnection("Database=testconn;Data Source=localhost;User Id=root;Password=root")
myConnection.Open()
Dim myCommand As MySqlCommand = myConnection.CreateCommand()
myCommand.CommandText = "SELECT * FROM mytable WHERE id = '" & strID & "'"
myCommand.CommandType = CommandType.Text
dtAdapter = New MySqlDataAdapter(myCommand)
dtAdapter.Fill(dt)
myConnection.Close()
If dt.Rows.Count > 0 Then
Me.txtName.Text = dt.Rows(0)("name")
Me.txtEmail.Text = dt.Rows(0)("email")
Me.birthdate.Text = dt.Rows(0)("birth")
End If
dt = Nothing
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
If Me.txtName.Text = "" Then
MessageBox.Show("Please input (Name)")
Me.txtName.Focus()
Exit Sub
End If
If Me.txtEmail.Text = "" Then
MessageBox.Show("Please input (Email)")
Me.txtEmail.Focus()
Exit Sub
End If
Dim myConnection As MySqlConnection
myConnection = New MySqlConnection("Database=testconn;Data Source=localhost;User Id=root;Password=root")
myConnection.Open()
Dim myCommand As MySqlCommand = myConnection.CreateCommand()
myCommand.CommandText = "UPDATE mytable SET " & _
"name = '" & txtName.Text & "' " & _
"email ='" & txtEmail.Text & "' " & _
"birth ='" & birthdate.Text & "' " & _
" WHERE id = '" & strID & "' "
myCommand.CommandType = CommandType.Text
myCommand.ExecuteNonQuery()
myConnection.Close()
MessageBox.Show("Update Successfully")
Me.Hide()
Dim f As New frmHome
f.Show()
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Hide()
Dim f As New frmHome
f.Show()
End Sub
End Class