ERRO ESTA DUPLICANDO OS CADASTROS
Boa tarde a todos amigos do forum, sou novo na programação vb.net e estou com um problema quem puder me ajudar agradeço.( Erro tenho uma aplicação para fazer cadastro de funcionario em vb.2008 com bando dados acess. nesse banco ja tenho dois nomes de exemplo esses mesmo funciona normalmente mais quando eu incluo outro nome ele lança no banco de dados normal o problema fica na hora que preciso alterar esse novo funcionario quando eu salvo ele salva mais duplica o mesmo! ficando 2 do mesmo geito. em baixo segue o codigo
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Imports System.Data.OleDb
Imports System.io
Imports System.drawing
Public Class frmOrganiza
Public Sub limpaFormulario()
tb1.ReadOnly = True
tb2.ReadOnly = True
tb3.ReadOnly = True
tb4.ReadOnly = True
tb5.ReadOnly = True
tb6.ReadOnly = True
cb1.Enabled = True
bt1.Visible = False
btn2.Visible = False
btn3.Visible = False
btn4.Visible = False
End Sub
Public Sub formRefresh()
cb1.Enabled = False
pb1.Show()
cb1.Enabled = False
tb1.ReadOnly = False
tb2.ReadOnly = False
tb3.ReadOnly = False
tb4.ReadOnly = False
tb5.ReadOnly = False
tb6.ReadOnly = False
bt1.Visible = True
btn2.Visible = True
btn3.Visible = True
End Sub
Public Sub tempValue()
If tb2.TextLength = 0 Then tb2.Text = [Ô]----[Ô]
If tb3.TextLength = 0 Then tb3.Text = 0
If tb4.TextLength = 0 Then tb4.Text = [Ô]0[Ô]
If tb5.TextLength = 0 Then tb5.Text = [Ô]---[Ô]
If tb6.TextLength = 0 Then tb6.Text = [Ô]00/00/0000[Ô]
If idno = 0 Then
pb1.Image.Save(Application.StartupPath + [Ô] emp.dat[Ô])
End If
End Sub
Public Sub limpaCampos()
tb1.Clear()
tb2.Clear()
tb3.Clear()
tb4.Clear()
tb5.Clear()
tb6.Clear()
pb1.Image = pb1.InitialImage
End Sub
Public Sub carregaDados()
Dim ds As New DataSet
Dim dr As DataRow
Dim dt As New DataTable
Dim adptr As New OleDbDataAdapter([Ô]select * from Contatos[Ô], con)
adptr.Fill(ds, [Ô]Contatos[Ô])
dt = ds.Tables(0)
For Each dr In dt.Rows
cb1.Items.Add(dr.Item([Ô]nome[Ô]))
Next
Call limpaFormulario()
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
If (MsgBox([Ô]Deseja encerrar o programa ? [Ô], MsgBoxStyle.YesNo) = MsgBoxResult.Yes) Then
Application.Exit()
End If
End Sub
Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Me.Finalize()
End Sub
Private Sub f1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
idno = 0
If con.State = ConnectionState.Open Then con.Close()
con.Open()
Call carregaDados()
pb1.Image = pb1.InitialImage
mrec.Enabled = False
mbtn.Enabled = False
drec.Enabled = False
dbtn.Enabled = False
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub cb1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cb1.SelectedIndexChanged
ts1.Text = [Ô]Organizador : [Ô] + cb1.SelectedItem
Try
Call limpaFormulario()
If con.State = ConnectionState.Closed Then con.Close()
con.Open()
Dim cmd As New OleDbCommand([Ô]select * from Contatos WHERE nome = [ô][Ô] & cb1.SelectedItem & [Ô][ô][Ô], con)
Dim drdr As OleDbDataReader
drdr = cmd.ExecuteReader
drdr.Read()
tb1.Text = drdr.Item([Ô]nome[Ô])
tb2.Text = drdr.Item([Ô]endereco[Ô])
tb3.Text = drdr.Item([Ô]fone[Ô])
tb4.Text = drdr.Item([Ô]celular[Ô])
tb5.Text = drdr.Item([Ô]email[Ô])
tb6.Text = drdr.Item([Ô]nascimento[Ô])
[ô]prepara para exibir a imagem no pictureBox
Dim bt() As Byte
Dim ms As MemoryStream
bt = drdr.Item([Ô]imagem[Ô])
ms = New MemoryStream(bt)
pb1.Image = Image.FromStream(ms)
tb8.Text = drdr.Item([Ô]nome[Ô])
pb1.Show()
cmd.Cancel()
con.Close()
mrec.Enabled = False
drec.Enabled = True
mbtn.Enabled = False
dbtn.Enabled = True
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub nbtn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nbtn1.Click
Call formRefresh()
Call limpaCampos()
[ô]exibe a imagem inicial que foi definida como none
pb1.Image = pb1.InitialImage
mrec.Enabled = False
drec.Enabled = False
mbtn.Enabled = False
dbtn.Enabled = False
ts1.Text = [Ô]Organizador :[Ô]
mno = 1
End Sub
Private Sub nrec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nrec.Click
Call formRefresh()
Call limpaCampos()
pb1.Image = pb1.InitialImage
mrec.Enabled = False
drec.Enabled = False
mbtn.Enabled = False
dbtn.Enabled = False
ts1.Text = [Ô]Organizador :[Ô]
mno = 1
End Sub
Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
Call limpaFormulario()
pb1.Hide()
Call limpaCampos()
nbtn1.Enabled = True
nrec.Enabled = True
mrec.Enabled = False
mbtn.Enabled = False
End Sub
Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
Try
If mno = 1 Then
If tb1.TextLength > 0 Then
If con.State = ConnectionState.Open Then con.Close()
Call tempValue()
Dim fs As New FileStream(Application.StartupPath + [Ô] emp.dat[Ô], FileMode.Open)
Dim arbyte(fs.Length) As Byte
fs.Read(arbyte, 0, arbyte.Length)
fs.Close()
Dim st As String = [Ô]INSERT INTO Contatos(nome,endereco,fone,celular,email,nascimento,[imagem])VALUES([ô][Ô] & tb1.Text & [Ô][ô],[ô][Ô] & tb2.Text & [Ô][ô],[ô][Ô] & tb3.Text & [Ô][ô],[ô][Ô] & tb4.Text & [Ô][ô],[ô][Ô] & tb5.Text & [Ô][ô],[ô][Ô] & tb6.Text & [Ô][ô], ?)[Ô]
Dim cmd As New OleDbCommand(st, con)
Dim mypar As New OleDbParameter([Ô]@picture[Ô], OleDbType.LongVarBinary)
mypar.Value = arbyte
mypar.Size = arbyte.Length
cmd.Parameters.Add(mypar)
con.Open()
cmd.ExecuteNonQuery()
Beep()
MsgBox([Ô]Dados salvos com sucesso[Ô])
cb1.Text = [Ô][Ô]
File.Delete(Application.StartupPath + [Ô] emp.dat[Ô])
idno = 0
cb1.Items.Clear()
Call carregaDados()
con.Close()
Call limpaFormulario()
Call limpaCampos()
Else
MsgBox([Ô]nome invalido[Ô])
End If
Else
If con.State = ConnectionState.Open Then con.Close()
If tb1.TextLength > 0 Then
Call tempValue()
Dim fst As New FileStream(Application.StartupPath + [Ô] emp.dat[Ô], FileMode.Open)
Dim arrbyte(fst.Length) As Byte
fst.Read(arrbyte, 0, arrbyte.Length)
fst.Close()
Dim s1 As String = [Ô]UPDATE Contatos SET nome=[ô][Ô] & tb1.Text & [Ô][ô],endereco=[ô][Ô] & tb2.Text & [Ô][ô],fone=[ô][Ô] & tb3.Text & [Ô][ô],celular=[ô][Ô] & tb4.Text & [Ô][ô],email=[ô][Ô] & tb5.Text & [Ô][ô],nascimento=[ô][Ô] & tb6.Text & [Ô][ô],[imagem]=? WHERE nome=[ô][Ô] & tb8.Text & [Ô][ô][Ô]
Dim cmnd As New OleDbCommand(s1, con)
Dim par As New OleDbParameter([Ô]@Imagem[Ô], OleDbType.LongVarBinary)
par.Value = arrbyte
par.Size = arrbyte.Length
cmnd.Parameters.Add(par)
con.Open()
cmnd.ExecuteNonQuery()
Beep()
MsgBox([Ô]Registro atualizado com sucesso[Ô])
idno = 0
cb1.Items.Clear()
Call carregaDados()
con.Close()
Call limpaFormulario()
Call limpaCampos()
mrec.Enabled = False
mbtn.Enabled = False
nrec.Enabled = True
nbtn1.Enabled = True
ts1.Text = [Ô]Organizador :[Ô]
Else
MsgBox([Ô]Nome Inválido[Ô])
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Dim img As Image
Private Sub bt1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt1.Click
op1.Filter = [Ô]JPG|*.jpg|GIF|*.gif[Ô]
op1.ShowDialog()
End Sub
Private Sub op1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles op1.FileOk
img = Image.FromFile(op1.FileName)
pb1.Image = img
pb1.SizeMode = PictureBoxSizeMode.StretchImage
pb1.Image.Save(Application.StartupPath + [Ô] emp.dat[Ô], Imaging.ImageFormat.Jpeg)
str = op1.FileName
idno = 1
End Sub
Private Sub dbtn_Click(ByVal sender A
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Imports System.Data.OleDb
Imports System.io
Imports System.drawing
Public Class frmOrganiza
Public Sub limpaFormulario()
tb1.ReadOnly = True
tb2.ReadOnly = True
tb3.ReadOnly = True
tb4.ReadOnly = True
tb5.ReadOnly = True
tb6.ReadOnly = True
cb1.Enabled = True
bt1.Visible = False
btn2.Visible = False
btn3.Visible = False
btn4.Visible = False
End Sub
Public Sub formRefresh()
cb1.Enabled = False
pb1.Show()
cb1.Enabled = False
tb1.ReadOnly = False
tb2.ReadOnly = False
tb3.ReadOnly = False
tb4.ReadOnly = False
tb5.ReadOnly = False
tb6.ReadOnly = False
bt1.Visible = True
btn2.Visible = True
btn3.Visible = True
End Sub
Public Sub tempValue()
If tb2.TextLength = 0 Then tb2.Text = [Ô]----[Ô]
If tb3.TextLength = 0 Then tb3.Text = 0
If tb4.TextLength = 0 Then tb4.Text = [Ô]0[Ô]
If tb5.TextLength = 0 Then tb5.Text = [Ô]---[Ô]
If tb6.TextLength = 0 Then tb6.Text = [Ô]00/00/0000[Ô]
If idno = 0 Then
pb1.Image.Save(Application.StartupPath + [Ô] emp.dat[Ô])
End If
End Sub
Public Sub limpaCampos()
tb1.Clear()
tb2.Clear()
tb3.Clear()
tb4.Clear()
tb5.Clear()
tb6.Clear()
pb1.Image = pb1.InitialImage
End Sub
Public Sub carregaDados()
Dim ds As New DataSet
Dim dr As DataRow
Dim dt As New DataTable
Dim adptr As New OleDbDataAdapter([Ô]select * from Contatos[Ô], con)
adptr.Fill(ds, [Ô]Contatos[Ô])
dt = ds.Tables(0)
For Each dr In dt.Rows
cb1.Items.Add(dr.Item([Ô]nome[Ô]))
Next
Call limpaFormulario()
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
If (MsgBox([Ô]Deseja encerrar o programa ? [Ô], MsgBoxStyle.YesNo) = MsgBoxResult.Yes) Then
Application.Exit()
End If
End Sub
Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Me.Finalize()
End Sub
Private Sub f1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
idno = 0
If con.State = ConnectionState.Open Then con.Close()
con.Open()
Call carregaDados()
pb1.Image = pb1.InitialImage
mrec.Enabled = False
mbtn.Enabled = False
drec.Enabled = False
dbtn.Enabled = False
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub cb1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cb1.SelectedIndexChanged
ts1.Text = [Ô]Organizador : [Ô] + cb1.SelectedItem
Try
Call limpaFormulario()
If con.State = ConnectionState.Closed Then con.Close()
con.Open()
Dim cmd As New OleDbCommand([Ô]select * from Contatos WHERE nome = [ô][Ô] & cb1.SelectedItem & [Ô][ô][Ô], con)
Dim drdr As OleDbDataReader
drdr = cmd.ExecuteReader
drdr.Read()
tb1.Text = drdr.Item([Ô]nome[Ô])
tb2.Text = drdr.Item([Ô]endereco[Ô])
tb3.Text = drdr.Item([Ô]fone[Ô])
tb4.Text = drdr.Item([Ô]celular[Ô])
tb5.Text = drdr.Item([Ô]email[Ô])
tb6.Text = drdr.Item([Ô]nascimento[Ô])
[ô]prepara para exibir a imagem no pictureBox
Dim bt() As Byte
Dim ms As MemoryStream
bt = drdr.Item([Ô]imagem[Ô])
ms = New MemoryStream(bt)
pb1.Image = Image.FromStream(ms)
tb8.Text = drdr.Item([Ô]nome[Ô])
pb1.Show()
cmd.Cancel()
con.Close()
mrec.Enabled = False
drec.Enabled = True
mbtn.Enabled = False
dbtn.Enabled = True
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub nbtn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nbtn1.Click
Call formRefresh()
Call limpaCampos()
[ô]exibe a imagem inicial que foi definida como none
pb1.Image = pb1.InitialImage
mrec.Enabled = False
drec.Enabled = False
mbtn.Enabled = False
dbtn.Enabled = False
ts1.Text = [Ô]Organizador :[Ô]
mno = 1
End Sub
Private Sub nrec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nrec.Click
Call formRefresh()
Call limpaCampos()
pb1.Image = pb1.InitialImage
mrec.Enabled = False
drec.Enabled = False
mbtn.Enabled = False
dbtn.Enabled = False
ts1.Text = [Ô]Organizador :[Ô]
mno = 1
End Sub
Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
Call limpaFormulario()
pb1.Hide()
Call limpaCampos()
nbtn1.Enabled = True
nrec.Enabled = True
mrec.Enabled = False
mbtn.Enabled = False
End Sub
Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
Try
If mno = 1 Then
If tb1.TextLength > 0 Then
If con.State = ConnectionState.Open Then con.Close()
Call tempValue()
Dim fs As New FileStream(Application.StartupPath + [Ô] emp.dat[Ô], FileMode.Open)
Dim arbyte(fs.Length) As Byte
fs.Read(arbyte, 0, arbyte.Length)
fs.Close()
Dim st As String = [Ô]INSERT INTO Contatos(nome,endereco,fone,celular,email,nascimento,[imagem])VALUES([ô][Ô] & tb1.Text & [Ô][ô],[ô][Ô] & tb2.Text & [Ô][ô],[ô][Ô] & tb3.Text & [Ô][ô],[ô][Ô] & tb4.Text & [Ô][ô],[ô][Ô] & tb5.Text & [Ô][ô],[ô][Ô] & tb6.Text & [Ô][ô], ?)[Ô]
Dim cmd As New OleDbCommand(st, con)
Dim mypar As New OleDbParameter([Ô]@picture[Ô], OleDbType.LongVarBinary)
mypar.Value = arbyte
mypar.Size = arbyte.Length
cmd.Parameters.Add(mypar)
con.Open()
cmd.ExecuteNonQuery()
Beep()
MsgBox([Ô]Dados salvos com sucesso[Ô])
cb1.Text = [Ô][Ô]
File.Delete(Application.StartupPath + [Ô] emp.dat[Ô])
idno = 0
cb1.Items.Clear()
Call carregaDados()
con.Close()
Call limpaFormulario()
Call limpaCampos()
Else
MsgBox([Ô]nome invalido[Ô])
End If
Else
If con.State = ConnectionState.Open Then con.Close()
If tb1.TextLength > 0 Then
Call tempValue()
Dim fst As New FileStream(Application.StartupPath + [Ô] emp.dat[Ô], FileMode.Open)
Dim arrbyte(fst.Length) As Byte
fst.Read(arrbyte, 0, arrbyte.Length)
fst.Close()
Dim s1 As String = [Ô]UPDATE Contatos SET nome=[ô][Ô] & tb1.Text & [Ô][ô],endereco=[ô][Ô] & tb2.Text & [Ô][ô],fone=[ô][Ô] & tb3.Text & [Ô][ô],celular=[ô][Ô] & tb4.Text & [Ô][ô],email=[ô][Ô] & tb5.Text & [Ô][ô],nascimento=[ô][Ô] & tb6.Text & [Ô][ô],[imagem]=? WHERE nome=[ô][Ô] & tb8.Text & [Ô][ô][Ô]
Dim cmnd As New OleDbCommand(s1, con)
Dim par As New OleDbParameter([Ô]@Imagem[Ô], OleDbType.LongVarBinary)
par.Value = arrbyte
par.Size = arrbyte.Length
cmnd.Parameters.Add(par)
con.Open()
cmnd.ExecuteNonQuery()
Beep()
MsgBox([Ô]Registro atualizado com sucesso[Ô])
idno = 0
cb1.Items.Clear()
Call carregaDados()
con.Close()
Call limpaFormulario()
Call limpaCampos()
mrec.Enabled = False
mbtn.Enabled = False
nrec.Enabled = True
nbtn1.Enabled = True
ts1.Text = [Ô]Organizador :[Ô]
Else
MsgBox([Ô]Nome Inválido[Ô])
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Dim img As Image
Private Sub bt1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt1.Click
op1.Filter = [Ô]JPG|*.jpg|GIF|*.gif[Ô]
op1.ShowDialog()
End Sub
Private Sub op1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles op1.FileOk
img = Image.FromFile(op1.FileName)
pb1.Image = img
pb1.SizeMode = PictureBoxSizeMode.StretchImage
pb1.Image.Save(Application.StartupPath + [Ô] emp.dat[Ô], Imaging.ImageFormat.Jpeg)
str = op1.FileName
idno = 1
End Sub
Private Sub dbtn_Click(ByVal sender A
na verdade o erro esta aqui
if mno = 1 Then
If tb1.TextLength > 0 Then
você tem que verificar se já existe um registro igual no banco , se existir e o botão de alterar estiver clicado então ele da updat, se o botão estiver clicado em novo então ele da o insert
if mno = 1 Then
If tb1.TextLength > 0 Then
você tem que verificar se já existe um registro igual no banco , se existir e o botão de alterar estiver clicado então ele da updat, se o botão estiver clicado em novo então ele da o insert
Obrigado LUIZCOMINO por responder olhei no banco de dados ele esta la sim como ficaria o codigo para se ele estiver ele dar updat? e que sou bem leigo em programação tentei aqui mais não consegui
simples se o botão alterar estiver acionado então use
Dim s1 As String = [Ô]UPDATE Contatos SET nome=[ô][Ô] & tb1.Text & [Ô][ô],endereco=[ô][Ô] & tb2.Text & [Ô][ô],fone=[ô][Ô] & tb3.Text & [Ô][ô],celular=[ô][Ô] & tb4.Text & [Ô][ô],email=[ô][Ô] & tb5.Text & [Ô][ô],nascimento=[ô][Ô] & tb6.Text & [Ô][ô],[imagem]=? WHERE nome=[ô][Ô] & tb8.Text & [Ô][ô][Ô]
se o botão novo estiver
Dim st As String = [Ô]INSERT INTO Contatos(nome,endereco,fone,celular,email,nascimento,[imagem])VALUES([ô][Ô] & tb1.Text & [Ô][ô],[ô][Ô] & tb2.Text & [Ô][ô],[ô][Ô] & tb3.Text & [Ô][ô],[ô][Ô] & tb4.Text & [Ô][ô],[ô][Ô] & tb5.Text & [Ô][ô],[ô][Ô] & tb6.Text & [Ô][ô], ?)[Ô]
entendeu?
Dim s1 As String = [Ô]UPDATE Contatos SET nome=[ô][Ô] & tb1.Text & [Ô][ô],endereco=[ô][Ô] & tb2.Text & [Ô][ô],fone=[ô][Ô] & tb3.Text & [Ô][ô],celular=[ô][Ô] & tb4.Text & [Ô][ô],email=[ô][Ô] & tb5.Text & [Ô][ô],nascimento=[ô][Ô] & tb6.Text & [Ô][ô],[imagem]=? WHERE nome=[ô][Ô] & tb8.Text & [Ô][ô][Ô]
se o botão novo estiver
Dim st As String = [Ô]INSERT INTO Contatos(nome,endereco,fone,celular,email,nascimento,[imagem])VALUES([ô][Ô] & tb1.Text & [Ô][ô],[ô][Ô] & tb2.Text & [Ô][ô],[ô][Ô] & tb3.Text & [Ô][ô],[ô][Ô] & tb4.Text & [Ô][ô],[ô][Ô] & tb5.Text & [Ô][ô],[ô][Ô] & tb6.Text & [Ô][ô], ?)[Ô]
entendeu?
Boa tarde LUIZCOMINO, mais uma vez agredeço pela sua atenção tava olhando o codigo aqui, graças a sua dica conseguir resolver como no inserir estava (if mno = 1 Then) fiz o seguinte no Click do botão alterar coloquei o seguinte ( mno = 2 ) fora do parenteze para se tiver outro fazer o updat. ai deu certo aqui estou fazendo teste para ter certeza que não vai dar futuros erros ate o momento esta ok! se der erro outra vez fasso esse novo procedimento que você diz! mais uma vez muiiiiiitoooo obrigado mesmo valeuuu.
Magina Abraços e não esqueça de encerrar o tópico!
Tópico encerrado , respostas não são mais permitidas