
Programando Botão Gravar Parte 2 em VB.net
Neste episódio vamos programar a parte de alteração do cliente, fazendo a consulta do cliente utilizando a caixa de texto ID para carregar todas as informação referente ao ID selecionado, após isso vamos criar o comando update para atualizar as informações.
Veja o episódio completo abaixo:
Código de pesquisa do TextBox ID:
Private Sub txtID_KeyDown(sender As Object, e As KeyEventArgs) Handles txtID.KeyDown
If txtID.TextLength > 0 And e.KeyCode = Keys.Enter Then
Dim cliente As New CAD_CLIENTE
Dim par As New List(Of SqlParameter)
par.Add(New SqlParameter("@ID", txtID.Text))
If cliente.ConsultaDataReader("SELECT * FROM CAD_CLIENTE WHERE ID = @ID", par) = True Then
txtNome.Text = cliente.NOME
txtEndereco.Text = cliente.ENDERECO
txtNumero.Text = cliente.NUMERO
txtTelefone.Text = cliente.TELEFONE
If intOpcao = Opcao.Editar Then
txtID.Enabled = False
AtivaDesativa(True)
tsbGravar.Enabled = True
End If
Else
MessageBox.Show("Código do cliente não cadastro.")
End If
End If
End Sub
Código completo do botão gravar:
Private Sub tsbGravar_Click(sender As Object, e As EventArgs) Handles tsbGravar.Click
Dim cliente As New CAD_CLIENTE
Dim par As New List(Of SqlParameter)
Dim SQL As String
Dim blnEndereco As Boolean = False
Dim blnNumero As Boolean = False
Dim blnTelefone As Boolean = False
Try
Select Case intOpcao
Case Opcao.Incluir
SQL = "INSERT INTO CAD_CLIENTE ("
If txtNome.TextLength = 0 Then
epValida.SetError(txtNome, "Nome do cliente é obrigatório.")
txtNome.Focus()
Exit Sub
Else
par.Add(New SqlParameter("@NOME", txtNome.Text))
SQL = SQL & "NOME"
epValida.SetError(txtNome, "")
End If
If txtEndereco.TextLength > 0 Then
par.Add(New SqlParameter("@ENDERECO", txtEndereco.Text))
SQL = SQL & ", ENDERECO"
blnEndereco = True
End If
If txtNumero.TextLength > 0 Then
par.Add(New SqlParameter("@NUMERO", txtNumero.Text))
SQL = SQL & ", NUMERO"
blnNumero = True
End If
If txtTelefone.TextLength > 0 Then
par.Add(New SqlParameter("@TELEFONE", txtTelefone.Text))
SQL = SQL & ", TELEFONE"
blnTelefone = True
End If
SQL = SQL & ") VALUES (@NOME"
If blnEndereco = True Then
SQL = SQL & ", @ENDERECO"
End If
If blnNumero = True Then
SQL = SQL & ", @NUMERO"
End If
If blnTelefone = True Then
SQL = SQL & ", @TELEFONE"
End If
SQL = SQL & ")"
If cliente.NovoCliente(SQL, par) = True Then
MessageBox.Show("Cliente adicionado com sucesso!")
intOpcao = Opcao.Cancelar
tsbCancelar_Click(Nothing, Nothing)
Else
MessageBox.Show("Problema ao tentar adicionar o cliente!")
End If
Case Opcao.Editar
SQL = "UPDATE CAD_CLIENTE SET "
par.Add(New SqlParameter("@ID", txtID.Text))
If txtNome.TextLength = 0 Then
epValida.SetError(txtNome, "Nome do cliente é obrigatório.")
txtNome.Focus()
Exit Sub
Else
par.Add(New SqlParameter("@NOME", txtNome.Text))
SQL = SQL & "NOME = @NOME"
epValida.SetError(txtNome, "")
End If
If txtEndereco.TextLength > 0 Then
par.Add(New SqlParameter("@ENDERECO", txtEndereco.Text))
SQL = SQL & ", ENDERECO = @ENDERECO"
blnEndereco = True
End If
If txtNumero.TextLength > 0 Then
par.Add(New SqlParameter("@NUMERO", txtNumero.Text))
SQL = SQL & ", NUMERO = @NUMERO"
blnNumero = True
End If
If txtTelefone.TextLength > 0 Then
par.Add(New SqlParameter("@TELEFONE", txtTelefone.Text))
SQL = SQL & ", TELEFONE = @TELEFONE"
blnTelefone = True
End If
SQL = SQL & " WHERE ID = @ID"
If cliente.AtualizaCliente(SQL, par) = True Then
MessageBox.Show("Cliente alterado com sucesso!")
intOpcao = Opcao.Cancelar
tsbCancelar_Click(Nothing, Nothing)
Else
MessageBox.Show("Problema ao tentar alterar o cliente!")
End If
End Select
Catch ex As Exception
End Try
End Sub


