Add "a new record for a table" problem

  • Thread starter Thread starter HS1
  • Start date Start date
H

HS1

Hello



I have Win application used to add records into a table (called Clients) in
an Access database. I use a DataGrid1 to present records in this table and
use some TextBox(es) to display the details of a selected record.



DataGrid1.DataSource = ds 'ds is a datasource

da1.Fill(ds) 'da1 is a OleDbDataAdapter



I "Set" the content of each TextBox in the Design Properties (at DataBinding
cell) window as the following



TextBox1.setText= ds - Clients.ClientName 'set text of the TextBox1

TextBox2.setText = ds - Clients.ClientAddress



Everything works well. In the textbox1 and textbox2, I can view details of
each record in the DataGrid1



To add record, I create a button Add. When I click this button, a new record
(empty) is added at the end of the DataGrid1. Also, the TexBox1 and TexBox2
are empty. I have this code for Add button event

-----------

Me.BindingContext(ds, "Clients").AddNew()



----------

Then I have another button "Save" to save the new record into database. I
have the code for Save button event

-----------

Dim newClient As DataRow = ds.Tables("Clients").NewRow

newClient("ClientName") = TextBox1.Text

newClient("ClientAddress") = TextBox2.Text

da1.Update(ds, "Clients")

MessageBox.Show(" One record is inserted ")

------------



When I did as above, I can see some records that are added into the
DataGrid1. However only the first record is "really" saved in the database.
That means, after I close the application and open it again, I only see the
first record displayed in the DataGrid1.



Could you please help

Thank you very much

SH1
 
HS,

One of the most classic problems in these dotnet newsgroups. It is strange
you did not get an earlier answer.

When you have added information to the texboxes it is not pushed in the
datatable.
That happens with a rowchange or with forcing that by "standard sample"

BindingContext(ds.Tables(0)).EndCurrentEdit()

I hope this helps?

Cor
 
Thank you for your help (many times before)

When I did as your suggestion:

When I click Add button: the textboxes are empty and there is a new,
empty record in the DataGrid1.

After I enter values in the textboxes and click Save, there two similar
records are presented in the DataGrid1.

I click "Add" again, there is new and empty record in the DataGrid1.
when I click Save, again, there are two more similar records are added
in the DataGrid1.

When I close the application, and run the application again, there are 3
records are saved. The firts two are of the first "Save" and the third
is of the second "Save"

I do not know why. Here is my code:

-------------------
Button Add
----------------

Me.BindingContext(ds, "ClienMe.BindingContext(ds, "Clients").AddNew()
'for obtaining a new and empty row in the DataGrid1

-------
Button SAVE
--------

Dim newClient As DataRow = ds.Tables("Clients").NewRow
newClient("C_FName") = TextBox1.Text
newClient("C_LName") = TextBox2.Text
ds.Tables(0).Rows.Add(newClient)
da1.Update(ds, "Clients")
Me.BindingContext(ds, "Clients").EndCurrentEdit()

------------------------

My application is very simple but I do not know what happen

Please help
Thank you
SH1
 
John,
' Dim newClient As DataRow = ds.Tables("Clients").NewRow
'newClient("C_FName") = TextBox1.Text
'newClient("C_LName") = TextBox2.Text
'ds.Tables(0).Rows.Add(newClient)
The ones above should not be needed
Me.BindingContext(ds, "Clients").EndCurrentEdit()
da1.Update(ds, "Clients")

I hope this helps?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top