473,782 Members | 2,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

BindingSource.A ddNew

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 BindingNavigato r 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_Loa d(ByVal sender As System.Object, ByVal e As
System.EventArg s) 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.Ins ertCommand = New SqlCommand("INS ERT INTO Clientes
(Nome,Nasciment o,CPFCNPJ,Email ) VALUES (@Nome,@Nascime nto,'','')", Conexao)

adpClientes.Fil l(Ds, "Clientes")
adpEnderecos.Fi ll(Ds, "Enderecos" )
adpTelefones.Fi ll(Ds, "Telefones" )

Ds.Relations.Ad d(New DataRelation("C lienteEndereco" ,
Ds.Tables("Clie ntes").Columns( "Id"),
Ds.Tables("Ende recos").Columns ("Cliente"), False))
Ds.Relations.Ad d(New DataRelation("C lienteTelefone" ,
Ds.Tables("Clie ntes").Columns( "Id"),
Ds.Tables("Tele fones").Columns ("Cliente"), False))

nDs = New BindingSource(D s, "Clientes")
Navegador.Bindi ngSource = nDs

txtNome.DataBin dings.Add(New Binding("Text", nDs, "Nome"))
dtpNascimento.D ataBindings.Add (New Binding("Value" , nDs,
"Nascimento "))

GradeE.DataSour ce = nDs
GradeE.DataMemb er = "ClienteEnderec o"
GradeE.Columns. RemoveAt(0)
GradeE.Columns. RemoveAt(0)

GradeT.DataSour ce = nDs
GradeT.DataMemb er = "ClienteTelefon e"
GradeT.Columns. RemoveAt(0)
GradeT.Columns. RemoveAt(0)

End Sub

Private Sub cmdCancelar_Cli ck(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles cmdCancelar.Cli ck
nDs.CancelEdit( )
End Sub

Private Sub cmdAdicionar_Cl ick(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles cmdAdicionar.Cl ick
nDs.AddNew() ' <------ Here's the problem
End Sub

Private Sub cmdSalvar_Click (ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles cmdSalvar.Click
nDs.EndEdit()
End Sub
Feb 16 '06 #1
1 12179
Hi,

"Leonardo" <le******@discu ssions.microsof t.com> wrote in message
news:%2******** *******@tk2msft ngp13.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 BindingNavigato r 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.A ddNew 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 _.

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_Loa d(ByVal sender As System.Object, ByVal e As
System.EventArg s) 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.Ins ertCommand = New SqlCommand("INS ERT INTO Clientes
(Nome,Nasciment o,CPFCNPJ,Email ) VALUES (@Nome,@Nascime nto,'','')",
Conexao)

adpClientes.Fil l(Ds, "Clientes")
adpEnderecos.Fi ll(Ds, "Enderecos" )
adpTelefones.Fi ll(Ds, "Telefones" )
AddHandler Ds.Tables("Clie ntes").TableNew Row, AddressOf OnTableNewRow

Ds.Relations.Ad d(New DataRelation("C lienteEndereco" ,
Ds.Tables("Clie ntes").Columns( "Id"),
Ds.Tables("Ende recos").Columns ("Cliente"), False))
Ds.Relations.Ad d(New DataRelation("C lienteTelefone" ,
Ds.Tables("Clie ntes").Columns( "Id"),
Ds.Tables("Tele fones").Columns ("Cliente"), False))

nDs = New BindingSource(D s, "Clientes")
Navegador.Bindi ngSource = nDs

txtNome.DataBin dings.Add(New Binding("Text", nDs, "Nome"))
dtpNascimento.D ataBindings.Add (New Binding("Value" , nDs,
"Nascimento "))

GradeE.DataSour ce = nDs
GradeE.DataMemb er = "ClienteEnderec o"
GradeE.Columns. RemoveAt(0)
GradeE.Columns. RemoveAt(0)

GradeT.DataSour ce = nDs
GradeT.DataMemb er = "ClienteTelefon e"
GradeT.Columns. RemoveAt(0)
GradeT.Columns. RemoveAt(0)

End Sub

Private Sub cmdCancelar_Cli ck(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles cmdCancelar.Cli ck
nDs.CancelEdit( )
End Sub

Private Sub cmdAdicionar_Cl ick(ByVal sender As System.Object, ByVal e
As
System.EventArg s) Handles cmdAdicionar.Cl ick
nDs.AddNew() ' <------ Here's the problem
End Sub

Private Sub cmdSalvar_Click (ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles cmdSalvar.Click
nDs.EndEdit()
End Sub
Private Sub DataTableNewRow EventHandler ( sender As Object, e As
DataTableNewRow EventArgs )
' set defaults
e.Row("Nascimen to") = ...
'....

End Sub


HTH,
Greetings
Feb 17 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1643
by: Michael Rodriguez | last post by:
I have a custom business object collection based on the CSLA.NET framework from Rockfor Lhotka's "Expert C# Business Objects" book. I bound my winform to the object and when I open the screen the records are there and I can scroll through them. The problem is when I call BindingSource.AddNew() it doesn't seem to work. I stepped through the code and a new object is being created, it just doesn't get added to the list. If I manually call...
0
4156
by: Geoff | last post by:
Hi folks Calling BindingSource.ResetCurrentItem() is changing the BindingSource.Position in a way I don't understand. If I'm understanding the docs correctly, ResetCurrentItem() should simply trigger an event which causes controls to refresh with the values of the current item with the index BindingSource.Position. This is not what I'm seeing.
1
3109
by: Pieter | last post by:
Hi, I have a custom List that inherits from BindingList. It has some methods overloaded, like the Add/Insert/etc to add and remove some eventhandlers when adding or removing an item T of the list. The problem happens when: - I use such a BindingList as DataSource for a BindingSource. - Add this BindingSource as DataSource for a DataGridView. - And go to the NewRow and add a new row in the DataGridView.
2
10453
by: Dustin Davis | last post by:
I'm trying to add a new row to my database using the BindingSource. I assume it is possible... Below is my code snippet: Dim drvZone As DataRowView Try drvZone = Me.ZonesBindingSource.AddNew() drvZone.Row.Item(1) = ProcessID
0
3551
by: kevinhd | last post by:
I need to set a value to a particular textbox, but the textbox is binded to a bindingsource, therefore, I couldn't set a default value. Anyone can help? Following is the code: private void StudentDetail_Load(object sender, EventArgs e) { this.studentTableAdapter.Fill(this.iDataSet.Student); studentBindingSource.AddNew(); //set a value in student number textbox ...
5
26279
by: giannis | last post by:
I am a newbie with VB. I'm trying to add a new row to my database using the BindingSource. I use the AddNew() and EndEdit() methonds but my changes dont saved at my database ... What is wrong ? How can i save my changes in the database (datasourse) ? Some code it will acceptable. Sorry for my english and thank you !
2
12509
by: Thammarat charoenchai. | last post by:
After I use BindingSource.AddNew Method. (in my code is OrderBindingSource.AddNew). How can I set field value. like Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
0
1769
nev
by: nev | last post by:
When bindingsource.addnew, it fires positionchanged event. When movenext, moveprevious, etc, it also fires positionchanged event. If I am to place code in positionchanged event, how will I determine if bindingsource state is in addnew mode? If bindingsource.state??? = addnew??? then 'Place code here if bindingsource state is in addnew mode. end if
0
2129
nev
by: nev | last post by:
Have any of you encountered this? And how did you correct it? bs.addnew() automatically moves the position to the new record. But mine doesn't. I have 3 bindingsources in my program, all user-defined withevents. Their datasources come from MySQL tables. Now, in the positionchanged event, I've coded something like the one below... -------------------------------------------------------- buttonAdd_cliked event handles button.click...
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10311
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6733
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4043
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.