0% found this document useful (0 votes)
13 views6 pages

MySQL Data Handling in VB.NET

The document contains a Visual Basic code for a Windows Forms application that manages user data, including connecting to a MySQL database, displaying data in a DataGridView, and handling image uploads. It includes error handling for database connections and user input validation, as well as methods for saving and updating user information. The code also manages UI elements such as buttons and panels for user interaction.

Uploaded by

Samito Warrota
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

MySQL Data Handling in VB.NET

The document contains a Visual Basic code for a Windows Forms application that manages user data, including connecting to a MySQL database, displaying data in a DataGridView, and handling image uploads. It includes error handling for database connections and user input validation, as well as methods for saving and updating user information. The code also manages UI elements such as buttons and panels for user interaction.

Uploaded by

Samito Warrota
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Private Sub ShowData()

Try
[Link]()
Catch ex As Exception
[Link]("Conexão falhou" & vbCrLf & "Por favor, verifique se o
servidor está pronto!", "Erro",
[Link])
Return
End Try

Try
If LoadImagesStr = False Then
[Link] = [Link]
[Link] = "SELECT Name, ID, Address, City, Country FROM "
& Table_Name & " ORDER BY Name"
MySQLDA = New MySqlDataAdapter([Link], Connection)
DT = New DataTable
Data=[Link](DT)
If [Link] > 0 Then
[Link] = Nothing
[Link] = DT
[Link](2).[Link] = "c"
[Link] = [Link]
[Link]()
Else
[Link] = DT
End If
Else
[Link] = [Link]
[Link] = "SELECT Image FROM " & Table_Name & " WHERE ID LIKE
'" & IDram & "'"
MySQLDA = New MySqlDataAdapter([Link], Connection)
DT = New DataTable
[Link](DT)
If Data > 0 Then
Dim ImgArray() As Byte = [Link](0).Item("Image").ToArray()
Dim ImgStream As New [Link](ImgArray)
[Link] = [Link](ImgStream)
[Link] = [Link]
[Link]()
End If
LoadImagesStr = False
End If
Catch ex As Exception
[Link]("Failed to load data" & vbCrLf & [Link], "Nagoya System",
[Link], [Link])
[Link]()
Return
End Try

DT = Nothing
[Link]()
End Sub
Private Sub ClearInputUpdateData()
[Link] = ""
[Link] = ""
[Link] = ""
[Link] = ""
[Link] = [Link].Click_to_browse
End Sub

Private Sub ButtonConnection_Click(sender As Object, e As EventArgs) Handles


[Link]
[Link] = [Link]
[Link] = False
[Link] = False
[Link] = True
End Sub

Private Sub ButtonUserData_Click(sender As Object, e As EventArgs) Handles


[Link]
If [Link] = False Then
[Link]("Failed to open User Data !!!" & vbCrLf & "Click the
Connection menu then click the Connect button.", "Nagoya System",
[Link], [Link])
Return
Else
StrSerialin = ""
ViewUserData = True
[Link] = [Link]
[Link] = False
[Link] = False
[Link] = True
End If
End Sub

StrSerialIn=""
ViewUserDate= False
[Link] = [Link]
[Link] = False
[Link] = False
[Link] = True
ShowData()

End Sub

Private Sub PanelUserData_Paint(sender As Object, e As PaintEventArgs) Handles


[Link]
[Link](New Pen([Link], 2),
[Link])
End Sub

Private Sub PanelUserData_Resize(sender As Object, e As EventArgs) Handles


[Link]
[Link]()
End Sub

Private Sub PanelRegistrationandEditUserData_Paint(sender As Object, e As


PaintEventArgs) Handles [Link]
[Link](New Pen([Link], 2),
[Link])
End Sub

Private Sub PanelRegistrationandEditUserData_Resize(sender As Object, e As


EventArgs) Handles [Link]
[Link]()
End Sub
[Link]()
Dim myPort As Array
Dim i As Integer
myPort = [Link]()
[Link](myPort)
i = [Link] - 1
i=i-i
Try
[Link] = 1
Catch ex As Exception
MsgBox("Com port not detected", [Link], "Error Message")
[Link] = ""
[Link]()
Return
End Try
[Link] = True

Private Sub ButtonScanPort_MouseLeave(sender As Object, e As EventArgs) Handles


[Link]
[Link] = [Link](6, 71, 163)
End Sub

Private Sub ButtonConnect_Click(sender As Object, e As EventArgs) Handles


[Link]
If [Link] = "Connect" Then
[Link] = [Link]
[Link] = [Link]
Try
[Link]()
[Link]()
[Link] = "Disconnect"
[Link] = [Link]
[Link] = "Connection Status: Connected"
Catch ex As Exception
MsgBox("Failed to connect !!!" & vbCrLf & "Arduino is not detected.",
[Link], "Error Message")
[Link] = [Link]
End Try
Else ' Se o botão estiver com o texto "Disconnect"
[Link] = [Link]
[Link] = "Connect"
[Link] = "Connection Status: Disconnected"
[Link]()
[Link]()
End If
End Sub

Private Sub ButtonConnect_MouseHover(sender As Object, e As EventArgs) Handles


[Link]
[Link] = [Link]
End Sub

Private Sub ButtonConnect_MouseLeave(sender As Object, e As EventArgs) Handles


[Link]
[Link] = [Link](6, 71, 165)
End Sub
Private Sub ButtonClear_Click(sender As Object, e As EventArgs) Handles
[Link]
[Link] = "____________"
[Link] = "Waiting..."
[Link] = "Waiting..."
[Link] = "Waiting..."
[Link] = "Waiting..."
[Link] = Nothing
End Sub

Private Sub ButtonClear_MouseHover(sender As Object, e As EventArgs) Handles


[Link]
[Link] = [Link]
End Sub

Private Sub ButtonClear_MouseLeave(sender As Object, e As EventArgs) Handles


[Link]
[Link] = [Link](6, 71, 165)
End Sub

Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles


[Link]
Dim mstream As New [Link]()
Dim arrImage() As Byte

Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles


[Link]
Dim mstream As New [Link]()
Dim arrImage() As Byte

If [Link] = "" Then


[Link]("Name cannot be empty !!!", "Error Message",
[Link], [Link])
Return
End If
If [Link] = "" Then
' ... (códigos semelhantes para outros campos)
End If

If StatusInput = "Save" Then


If IMG_Filename_Input <> "" Then
[Link](mstream,
[Link])
arrImage = [Link]()
End If
End If

End Sub

If [Link] = [Link] Then


[Link]("The image was not selected.", "Error!", [Link],
[Link])
Return
End If

If StatusInput="Save" Then
If IMG_Filename_Input <> "" Then
[Link](mstream,
[Link])
arrImage = [Link]()

Try

[Link]()
Catch ex As Exception
[Link]("Connection failed! " & vbCrLf & "Please check if the server is
ready.", "Try Again", [Link],
[Link])
Return
End Try

Try
MySqlCommand = New MySqlCommand()
With MySqlCommand
.CommandText = "INSERT INTO Table_Name (Nome, Departamento, Time Im, Time
Out, Posto, Image) VALUES (@Nome, @Departamento, @Time Im, @Time Out,@Posto,
@Image)"
.Connection = Connection
.[Link]("@Nome", [Link])
.[Link]("@id", [Link])
.[Link]("@Departamentoe", [Link])
.[Link]("@Time In", [Link])
.[Link]("@Time Out", [Link])
.[Link]("@Posto", [Link])
.[Link]("@Images", arrImage)
.ExecuteNonQuery()
End With
[Link]("Data saved successfully!", [Link],
[Link])
IMG_FileNameInput=""
ClearInputUpdateData()
Catch ex As Exception
[Link]("Failed to save data! " & vbCrLf & [Link],
[Link], [Link])
[Link]()
Return
End Try
[Link]()

Else
Try
[Link]()
Catch ex As Exception
[Link]("Connection failed" & vbCrLf & "Please check that the server is
ready.", "Error", [Link], [Link])
Return
End Try

Try
Dim MySQLCmd As New MySqlCommand()
With MySQLCmd
.CommandText = "UPDATE Table_Name SET Nome = @Nome, Departamento=
@Departamento, Time Im=@Time Im, Time Out=@Time Out, Posto=@Posto, Image= @Image
.Connection = Connection
.[Link]("@Nome", [Link])
.[Link]("@id", [Link])
.[Link]("@Departamentoe", [Link])
.[Link]("@Time In", [Link])
.[Link]("@Time Out", [Link])
.[Link]("@Posto", [Link])
.[Link]("@Images", arrImage)
.ExecuteNonQuery()
End With
[Link]("Data updated successfully", "Information",
[Link], [Link])
[Link] = "Save"
ClearInputUpdateData()
Catch ex As Exception
ClearInputUpdateData()
Catch ex As Exception
[Link]("Update failed" & vbCrLf & [Link], "Error",
[Link], [Link])
[Link]()
Return
End Try
[Link]()
End If
StatusInput = "Save"
end If
[Link] = Nothing
ShowData()

Private Sub ButtonSave_MouseHover(sender As Object, e As EventArgs) Handles


[Link]
[Link] = [Link]
End Sub

Private Sub ButtonSave_MouseLeave(sender As Object, e As EventArgs) Handles


[Link]
[Link] = [Link](16, 71, 165)
End Sub

Private Sub ButtonClearForm_Click(sender As Object, e As EventArgs) Handles


[Link]
ClearInputUpdateData()
End Sub

Private Sub ButtonClearForm_MouseHover(sender As Object, e As EventArgs) Handles


[Link]
[Link] = [Link]
End Sub

Private Sub ButtonClearForm_MouseLeave(sender As Object, e As EventArgs) Handles


[Link]
[Link] = [Link](16, 71, 165)
End Sub

You might also like