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

Read MMMM

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)
11 views6 pages

Read MMMM

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
You are on page 1/ 6

Private Sub ShowData()

Try
Connection.Open()
Catch ex As Exception
MessageBox.Show("Conexão falhou" & vbCrLf & "Por favor, verifique se o
servidor está pronto!", "Erro",
MessageBoxIcon.Error)
Return
End Try

Try
If LoadImagesStr = False Then
MySQLCMD.CommandType = CommandType.Text
MySQLCMD.CommandText = "SELECT Name, ID, Address, City, Country FROM "
& Table_Name & " ORDER BY Name"
MySQLDA = New MySqlDataAdapter(MySQLCMD.CommandText, Connection)
DT = New DataTable
Data=MySQLDA.Fill(DT)
If DT.Rows.Count > 0 Then
DataGridView1.DataSource = Nothing
DataGridView1.DataSource = DT
DataGridView1.Columns(2).DefaultCellStyle.Format = "c"
DataGridView1.DefaultCellStyle.ForeColor = Color.Black
DataGridView1.ClearSelection()
Else
DataGridViewl.DataSource = DT
End If
Else
MySQLCMD.CommandType = CommandType.Text
MySQLCMD.CommandText = "SELECT Image FROM " & Table_Name & " WHERE ID LIKE
'" & IDram & "'"
MySQLDA = New MySqlDataAdapter(MySQLCMD.CommandText, Connection)
DT = New DataTable
MySQLDA.Fill(DT)
If Data > 0 Then
Dim ImgArray() As Byte = DT.Rows(0).Item("Image").ToArray()
Dim ImgStream As New System.IO.MemoryStream(ImgArray)
PictureBoxImagePreview.Image = Image.FromStream(ImgStream)
PictureBoxImagePreview.SizeMode = PictureBoxSizeMode.Zoom
ImgStream.Close()
End If
LoadImagesStr = False
End If
Catch ex As Exception
MessageBox.Show("Failed to load data" & vbCrLf & ex.Message, "Nagoya System",
MessageBoxButtons.OK, MessageBoxIcon.Error)
Connection.Close()
Return
End Try

DT = Nothing
Connection.Close()
End Sub
Private Sub ClearInputUpdateData()
TextBoxName.Text = ""
LabelDetID.Text = ""
TextBoxCity.Text = ""
TextBoxCountry.Text = ""
PictureBoxImageInput.Image = My.Resources.Click_to_browse
End Sub

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


ButtonConnection.Click
PictureBoxSelect.Top = ButtonConnection.Top
PanelUserData.Visible = False
PanelRegistrationandEditUserData.Visible = False
PanelConnection.Visible = True
End Sub

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


ButtonUserData.Click
If Timerserialin.Enabled = False Then
MessageBox.Show("Failed to open User Data !!!" & vbCrLf & "Click the
Connection menu then click the Connect button.", "Nagoya System",
MessageBoxButtons.OK, MessageBoxIcon.Information)
Return
Else
StrSerialin = ""
ViewUserData = True
PictureBoxSelect.Top = ButtonUserData.Top
PanelRegistrationandEditUserData.Visible = False
PanelConnection.Visible = False
PanelUserData.Visible = True
End If
End Sub

StrSerialIn=""
ViewUserDate= False
PictureboxSelect.Top = ButtonRegistration.Top
PanelConnection.Visible = False
PanelUserData.Visible = False
PanelRegistrationendEditUserData.Visible = True
ShowData()

End Sub

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


PanelUserData.Paint
e.Graphics.DrawRectangle(New Pen(Color.LightGray, 2),
PanelUserData.ClientRectangle)
End Sub

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


PanelUserData.Resize
PanelUserData.Invalidate()
End Sub

Private Sub PanelRegistrationandEditUserData_Paint(sender As Object, e As


PaintEventArgs) Handles PanelRegistrationandEditUserData.Paint
e.Graphics.DrawRectangle(New Pen(Color.LightGray, 2),
PanelRegistrationandEditUserData.ClientRectangle)
End Sub

Private Sub PanelRegistrationandEditUserData_Resize(sender As Object, e As


EventArgs) Handles PanelRegistrationandEditUserData.Resize
PanelRegistrationandEditUserData.Invalidate()
End Sub
ComboBoxPort.Items.Clear()
Dim myPort As Array
Dim i As Integer
myPort = IO.Ports.SerialPort.GetPortNames()
ComboBoxPort.Items.AddRange(myPort)
i = ComboBoxPort.Items.Count - 1
i=i-i
Try
ComboBoxPort.SelectedIndex = 1
Catch ex As Exception
MsgBox("Com port not detected", MsgBoxStyle.Critical, "Error Message")
ComboBoxPort.Text = ""
ComboBoxPort.Items.Clear()
Return
End Try
ComboBoxPort.DroppedDown = True

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


ButtonScanPort.MouseLeave
ButtonScanPort.ForeColor = Color.FromArgb(6, 71, 163)
End Sub

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


ButtonConnect.Click
If ButtonConnect.Text = "Connect" Then
SerialPort1.BaudRate = ComboBoxBaudRate.SelectedItem
SerialPort1.PortName = ComboBoxPort.SelectedItem
Try
SerialPort1.Open()
TimerSerialIn.Start()
ButtonConnect.Text = "Disconnect"
PictureBoxStatusConnect.Image = My.Resources.Connected
LabelConnectionStatus.Text = "Connection Status: Connected"
Catch ex As Exception
MsgBox("Failed to connect !!!" & vbCrLf & "Arduino is not detected.",
MsgBoxStyle.Critical, "Error Message")
PictureBoxStatusConnect.Image = My.Resources.Disconnected
End Try
Else ' Se o botão estiver com o texto "Disconnect"
PictureBoxStatusConnect.Image = My.Resources.Disconnected
ButtonConnect.Text = "Connect"
LabelConnectionStatus.Text = "Connection Status: Disconnected"
TimerSerialIn.Stop()
SerialPort1.Close()
End If
End Sub

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


ButtonConnect.MouseHover
ButtonConnect.ForeColor = Color.White
End Sub

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


ButtonConnect.MouseLeave
ButtonConnect.ForeColor = Color.FromArgb(6, 71, 165)
End Sub
Private Sub ButtonClear_Click(sender As Object, e As EventArgs) Handles
ButtonClear.Click
LabelID.Text = "____________"
LabelName.Text = "Waiting..."
LabelAddress.Text = "Waiting..."
LabelCity.Text = "Waiting..."
LabelCountry.Text = "Waiting..."
PictureBoxUserImage.Image = Nothing
End Sub

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


ButtonClear.MouseHover
ButtonClear.ForeColor = Color.White
End Sub

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


ButtonClear.MouseLeave
ButtonClear.ForeColor = Color.FromArgb(6, 71, 165)
End Sub

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


ButtonSave.Click
Dim mstream As New System.IO.MemoryStream()
Dim arrImage() As Byte

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


ButtonSave.Click
Dim mstream As New System.IO.MemoryStream()
Dim arrImage() As Byte

If TextBoxName.Text = "" Then


MessageBox.Show("Name cannot be empty !!!", "Error Message",
MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
If TextBoxAddress.Text = "" Then
' ... (códigos semelhantes para outros campos)
End If

If StatusInput = "Save" Then


If IMG_Filename_Input <> "" Then
PictureBoxImageInput.Image.Save(mstream,
System.Drawing.Imaging.ImageFormat.Jpeg)
arrImage = mstream.GetBuffer()
End If
End If

End Sub

If FileDialog1.ShowDialog = DialogResult.Cancel Then


MessageBox.Show("The image was not selected.", "Error!", MessageBoxButtons.OK,
MessageBoxIcon.Error)
Return
End If

If StatusInput="Save" Then
If IMG_Filename_Input <> "" Then
PictureBoxImageInput.Image.Save(mstream,
System.Drawing.Imaging.ImageFormat.Jpeg)
arrImage = mstream.GetBuffer()

Try

Connection.Open()
Catch ex As Exception
MessageBox.Show("Connection failed! " & vbCrLf & "Please check if the server is
ready.", "Try Again", MessageBoxButtons.OK,
MessageBoxIcon.Error)
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
.Parameters.AddWithValue("@Nome", TextBoxNome.Text)
.Parameters.AddWithValue("@id", LabelGetID.Text)
.Parameters.AddWithValue("@Departamentoe", TextBoxDepartamento.Text)
.Parameters.AddWithValue("@Time In", TextBoxTimein.Text)
.Parameters.AddWithValue("@Time Out", TextBoxTimeOut.Text)
.Parameters.AddWithValue("@Posto", TextBoxTimePosto.Text)
.Parameters.AddWithValue("@Images", arrImage)
.ExecuteNonQuery()
End With
MessageBox.Show("Data saved successfully!", MessageBoxButtons.OK,
MessageBoxIcon.Information)
IMG_FileNameInput=""
ClearInputUpdateData()
Catch ex As Exception
MessageBox.Show("Failed to save data! " & vbCrLf & ex.Message,
MessageBoxButtons.OK, MessageBoxIcon.Critical)
Connection.Close()
Return
End Try
Connection.Close()

Else
Try
Connection.Open()
Catch ex As Exception
MessageBox.Show("Connection failed" & vbCrLf & "Please check that the server is
ready.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
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
.Parameters.AddWithValue("@Nome", TextBoxNome.Text)
.Parameters.AddWithValue("@id", LabelGetID.Text)
.Parameters.AddWithValue("@Departamentoe", TextBoxDepartamento.Text)
.Parameters.AddWithValue("@Time In", TextBoxTimein.Text)
.Parameters.AddWithValue("@Time Out", TextBoxTimeOut.Text)
.Parameters.AddWithValue("@Posto", TextBoxTimePosto.Text)
.Parameters.AddWithValue("@Images", arrImage)
.ExecuteNonQuery()
End With
MessageBox.Show("Data updated successfully", "Information",
MessageBoxButtons.OK, MessageBoxIcon.Information)
ButtonSave.Text = "Save"
ClearInputUpdateData()
Catch ex As Exception
ClearInputUpdateData()
Catch ex As Exception
MessageBox.Show("Update failed" & vbCrLf & ex.Message, "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
Connection.Close()
Return
End Try
Connection.Close()
End If
StatusInput = "Save"
end If
pictureBoxImagePreview.Image = Nothing
ShowData()

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


ButtonSave.MouseHover
ButtonSave.ForeColor = Color.White
End Sub

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


ButtonSave.MouseLeave
ButtonSave.ForeColor = Color.FromArgb(16, 71, 165)
End Sub

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


ButtonClearForm.Click
ClearInputUpdateData()
End Sub

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


ButtonClearForm.MouseHover
ButtonClearForm.ForeColor = Color.White
End Sub

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


ButtonClearForm.MouseLeave
ButtonClearForm.ForeColor = Color.FromArgb(16, 71, 165)
End Sub

You might also like