Home All Groups Group Topic Archive Search About
Author
16 Feb 2006 7:09 PM
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

Author
17 Feb 2006 11:56 AM
Bart Mermuys
Hi,

Show quoteHide quote
"Leonardo" <leona***@discussions.microsoft.com> wrote in message
news:%23WKibQyMGHA.420@tk2msftngp13.phx.gbl...
> 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.

What do you exactly mean with i can't 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?

BindingSource.AddNew should work, but it's possible you have problems with
the new row because it contains null values and DateTimePicker may crash
with null values (as can CheckBoxes) so you need to set a default value.

In NET2.0 you can use the TableNewRow event to set the defaults, _ see
additions to your code _.

Show quoteHide quote
>
> 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")

AddHandler Ds.Tables("Clientes").TableNewRow, AddressOf OnTableNewRow

Show quoteHide quote
>
>        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

Private Sub DataTableNewRowEventHandler ( sender As Object,  e As
DataTableNewRowEventArgs )
    ' set defaults
    e.Row("Nascimento") = ...
    '....

End Sub

>
>

HTH,
Greetings