Código Button MySql
Casos: Código Button MySql. Pesquise 862.000+ trabalhos acadêmicosPor: Geruzza • 25/5/2014 • 1.434 Palavras (6 Páginas) • 280 Visualizações
Imports MySql.Data.MySqlClient
Public Class frmcategoria
Private Sub bloquear()
txtcodigo.Enabled = True
btnalterar.Enabled = False
btnexcluir.Enabled = False
btnincluir.Enabled = False
End Sub
Private Sub frmcategoria_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
bloquear()
End Sub
Private Sub txtcodigo_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtcodigo.LostFocus
If txtcodigo.Text = Nothing Then
Exit Sub
End If
Dim cn As New MySqlConnection
cn.ConnectionString = My.Settings.lojaConnectionString
Dim cmd As New MySqlCommand
cmd.Connection = cn
cmd.CommandText = "select * from categoria where id_categoria=" & txtcodigo.Text
Try
cn.Open()
Dim dr As MySqlDataReader
dr = cmd.ExecuteReader
If dr.Read = True Then
txtcategoria.Text = dr("categoria")
cn.Close()
txtcodigo.Enabled = False
'liberamos os botões alterar e excluir
btnalterar.Enabled = True
btnexcluir.Enabled = True
btnincluir.Enabled = False
Else
txtcodigo.Text = "<AUT>"
txtcodigo.Enabled = False
' os botões são invertidos em realação a opção anterior'
btnalterar.Enabled = False
btnexcluir.Enabled = False
btnincluir.Enabled = True
End If
Catch ex As Exception
MsgBox("Ocorreu o erro: " & ex.Message, MsgBoxStyle.Critical)
End Try
End Sub
Private Sub txtcodigo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtcodigo.TextChanged
End Sub
Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter
End Sub
Private Sub limpar()
txtcodigo.Clear()
txtcategoria.Clear()
txtcodigo.Focus()
End Sub
Private Sub btnincluir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnincluir.Click
Dim cn As New MySqlConnection
cn.ConnectionString = My.Settings.lojaConnectionString
Dim comando As New MySqlCommand
comando.CommandText = "insert into categoria (categoria)" & _
"values ('" & txtcategoria.Text & "')"
Try
cn.Open()
comando.Connection = cn
comando.ExecuteNonQuery()
MessageBox.Show("Categoria incluída com sucasso!", "Inclusão")
cn.Close()
Catch ex As Exception
MessageBox.Show("Ocorreu o erro:" & ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End Try
bloquear()
limpar()
End Sub
Private Sub btnalterar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnalterar.Click
Dim cn As New MySqlConnection
...