Sample VB code

Posted on at


Imports MySql.Data.MySqlClient
Public Class Form1
Public dbcomm As MySqlCommand
Public dbconn As New MySqlConnection
Public sql As String
Public dbread As MySqlDataReader


Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtname.TextChanged

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dbconn = New MySqlConnection("data source=localhost; user id=root; database=computers")

Try
dbconn.Open()
Catch ex As Exception
MsgBox("Error in connection" & ex.Message)
End Try
refresh_list()
End Sub
Sub refresh_list()
sql = "SELECT id, name FROM computers"
Try
dbcomm = New MySqlCommand(sql, dbconn)
dbread = dbcomm.ExecuteReader
While dbread.Read
list.Items.Add(dbread("id"))
list.Items(list.Items.Count - 1).SubItems.Add(dbread("name"))

End While
dbread.Close()
Catch ex As Exception
MsgBox("error in display" & ex.Message)
End Try
dbread.Close()

End Sub

Private Sub btnup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnup.Click
If MsgBox("are you sure you want to update?", vbOKCancel) = vbOK Then
sql = "UPDATE computers set name='" & txtname.Text & _
"'WHERE id='" & list.SelectedItems(0).Text & "'"
Try
dbcomm = New MySqlCommand(sql, dbconn)
dbread = dbcomm.ExecuteReader
dbread.Close()
refresh_list()
btnup.Enabled = False
txtname.Text = ""
Catch ex As Exception
MsgBox("error" & ex.Message)
dbread.Close()
End Try
dbread.Close()
list.Items.Clear()
refresh_list()
MsgBox("save")
End If
End Sub

Private Sub btnup_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnup.MouseDoubleClick
If MsgBox("are you want to edit the record?" & list.SelectedItems(0).Text & "?", vbOKCancel) = vbOK Then
sql = "SELECT id, name FROM computers WHERE id='" & list.SelectedItems(0).Text & "'"
btnup.Enabled = True
Try
dbcomm = New MySqlCommand(sql, dbconn)
dbread = dbcomm.ExecuteReader
While dbread.Read
txtname.Text = dbread("name")

End While
dbread.Close()

Catch ex As Exception
MsgBox("error in displaying data")
dbread.Close()
End Try

dbread.Close()
End If

End Sub

Private Sub list_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles list.MouseDoubleClick
sql = "SELECT id, name FROM computers WHERE id='" & list.SelectedItems(0).Text & "'"
btnup.Enabled = True
Try
dbcomm = New MySqlCommand(sql, dbconn)
dbread = dbcomm.ExecuteReader
While dbread.Read
txtname.Text = dbread("name")

End While
dbread.Close()

Catch ex As Exception
MsgBox("error in displaying data")
dbread.Close()
End Try
End Sub

Private Sub list_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles list.SelectedIndexChanged
ListView1.Items.Clear()
'sql = "SELECT id, code, hd, ram FROM computer_types WHERE id='" & list.SelectedItems(0).Text & "'"

Try
sql = "SELECT ID, CODE, HD, RAM FROM computer_types WHERE COMPUTER_ID='" & list.SelectedItems(0).Text & "'"
' MsgBox(sql)
dbcomm = New MySqlCommand(sql, dbconn)
dbread = dbcomm.ExecuteReader
While dbread.Read
ListView1.Items.Add(dbread("ID"))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dbread("CODE"))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dbread("HD"))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(dbread("RAM"))


End While
dbread.Close()
Catch ex As Exception
' MsgBox("error in display" & ex.Message)
End Try
dbread.Close()
End Sub
End Class

TAGS:


About the author

160