L
Leonardo
Hi. I'm trying to build my first application with database access using VB
2005. I'm a VB 6 programmer and learning everything again has been
challenging. I managed to write a code using some tips from a book I
recently bought. The navigation works fine. My problem starts when it comes
to adding new registers. I have three tables: clients, addresses and
telephones, so the client can have multiple address and telephones. The
client data is shown in textboxes, while his addresses and telephones are
shown in a DataGridView each. I have been using the class BindingSource
associated with a BindingNavigator to browse throught the table. I thought
about using the AddNew method to add new registers, but it didn't work as I
expected. Looks like this method inserts a blank register. I can't even type
what I want. And I couldn't figure out how to solve this problem. What
should I do? How can I add new registers using this class? Is there any
other way?
Thank you.
Here's my code, if you want to take a look at:
Dim Conexao As SqlConnection, Ds As New DataSet()
Dim adpClientes As SqlDataAdapter
Dim adpEnderecos As SqlDataAdapter
Dim adpTelefones As SqlDataAdapter
Dim nDs As BindingSource
Private Sub FrmPrograma_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Conexao = New
SqlConnection("Server=(local);Database=Banco;UId=sa;Pwd=xxx")
Conexao.Open()
adpClientes = New SqlDataAdapter("SELECT Id, Nome, Nascimento FROM
Clientes WHERE Excluido=0 ORDER BY Nome", Conexao)
adpEnderecos = New SqlDataAdapter("SELECT * FROM Enderecos",
Conexao)
adpTelefones = New SqlDataAdapter("SELECT * FROM Telefones",
Conexao)
adpClientes.InsertCommand = New SqlCommand("INSERT INTO Clientes
(Nome,Nascimento,CPFCNPJ,Email) VALUES (@Nome,@Nascimento,'','')", Conexao)
adpClientes.Fill(Ds, "Clientes")
adpEnderecos.Fill(Ds, "Enderecos")
adpTelefones.Fill(Ds, "Telefones")
Ds.Relations.Add(New DataRelation("ClienteEndereco",
Ds.Tables("Clientes").Columns("Id"),
Ds.Tables("Enderecos").Columns("Cliente"), False))
Ds.Relations.Add(New DataRelation("ClienteTelefone",
Ds.Tables("Clientes").Columns("Id"),
Ds.Tables("Telefones").Columns("Cliente"), False))
nDs = New BindingSource(Ds, "Clientes")
Navegador.BindingSource = nDs
txtNome.DataBindings.Add(New Binding("Text", nDs, "Nome"))
dtpNascimento.DataBindings.Add(New Binding("Value", nDs,
"Nascimento"))
GradeE.DataSource = nDs
GradeE.DataMember = "ClienteEndereco"
GradeE.Columns.RemoveAt(0)
GradeE.Columns.RemoveAt(0)
GradeT.DataSource = nDs
GradeT.DataMember = "ClienteTelefone"
GradeT.Columns.RemoveAt(0)
GradeT.Columns.RemoveAt(0)
End Sub
Private Sub cmdCancelar_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdCancelar.Click
nDs.CancelEdit()
End Sub
Private Sub cmdAdicionar_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdAdicionar.Click
nDs.AddNew() ' <------ Here's the problem
End Sub
Private Sub cmdSalvar_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdSalvar.Click
nDs.EndEdit()
End Sub
2005. I'm a VB 6 programmer and learning everything again has been
challenging. I managed to write a code using some tips from a book I
recently bought. The navigation works fine. My problem starts when it comes
to adding new registers. I have three tables: clients, addresses and
telephones, so the client can have multiple address and telephones. The
client data is shown in textboxes, while his addresses and telephones are
shown in a DataGridView each. I have been using the class BindingSource
associated with a BindingNavigator to browse throught the table. I thought
about using the AddNew method to add new registers, but it didn't work as I
expected. Looks like this method inserts a blank register. I can't even type
what I want. And I couldn't figure out how to solve this problem. What
should I do? How can I add new registers using this class? Is there any
other way?
Thank you.
Here's my code, if you want to take a look at:
Dim Conexao As SqlConnection, Ds As New DataSet()
Dim adpClientes As SqlDataAdapter
Dim adpEnderecos As SqlDataAdapter
Dim adpTelefones As SqlDataAdapter
Dim nDs As BindingSource
Private Sub FrmPrograma_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Conexao = New
SqlConnection("Server=(local);Database=Banco;UId=sa;Pwd=xxx")
Conexao.Open()
adpClientes = New SqlDataAdapter("SELECT Id, Nome, Nascimento FROM
Clientes WHERE Excluido=0 ORDER BY Nome", Conexao)
adpEnderecos = New SqlDataAdapter("SELECT * FROM Enderecos",
Conexao)
adpTelefones = New SqlDataAdapter("SELECT * FROM Telefones",
Conexao)
adpClientes.InsertCommand = New SqlCommand("INSERT INTO Clientes
(Nome,Nascimento,CPFCNPJ,Email) VALUES (@Nome,@Nascimento,'','')", Conexao)
adpClientes.Fill(Ds, "Clientes")
adpEnderecos.Fill(Ds, "Enderecos")
adpTelefones.Fill(Ds, "Telefones")
Ds.Relations.Add(New DataRelation("ClienteEndereco",
Ds.Tables("Clientes").Columns("Id"),
Ds.Tables("Enderecos").Columns("Cliente"), False))
Ds.Relations.Add(New DataRelation("ClienteTelefone",
Ds.Tables("Clientes").Columns("Id"),
Ds.Tables("Telefones").Columns("Cliente"), False))
nDs = New BindingSource(Ds, "Clientes")
Navegador.BindingSource = nDs
txtNome.DataBindings.Add(New Binding("Text", nDs, "Nome"))
dtpNascimento.DataBindings.Add(New Binding("Value", nDs,
"Nascimento"))
GradeE.DataSource = nDs
GradeE.DataMember = "ClienteEndereco"
GradeE.Columns.RemoveAt(0)
GradeE.Columns.RemoveAt(0)
GradeT.DataSource = nDs
GradeT.DataMember = "ClienteTelefone"
GradeT.Columns.RemoveAt(0)
GradeT.Columns.RemoveAt(0)
End Sub
Private Sub cmdCancelar_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdCancelar.Click
nDs.CancelEdit()
End Sub
Private Sub cmdAdicionar_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdAdicionar.Click
nDs.AddNew() ' <------ Here's the problem
End Sub
Private Sub cmdSalvar_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdSalvar.Click
nDs.EndEdit()
End Sub