Unable to update database

  • Thread starter Thread starter Wladek
  • Start date Start date
W

Wladek

Hi,

I have three Access tables: tblInvestor, tblProject and
tblInvestorProject. It is a many to many relation between
tblProject and tblInvestor.

The tables look something like this:

tblInvestor tblProject tblInvestorProject
----------- ------------ ------------------
IDInvestor (PK) IDProject (PK) IDInvestor (PK)
Name Title IDProject (PK)
InvestorType

I have two buttons: LoadData and SaveData, which
appropriately loads data from database and saves changes
back to database.
I have a combo on my form (cmbInvestor) which allows me
to select investor for a given project. The chosen
investor ID
(IDInvestor) should be written into tblInvestorProject.

The problem I have is that when I select investor from
cmbInvestor,
the appropriate data (IDInvestor) is written into dataset
but when
I read the DataViewRowState of the given row from
tblInvestorProject
it is Unchanged. Therefore I can't update the database.

Here is the code snippet:

// cmbInvestor
cmbInvestor.DataBindings.Add(new
System.Windows.Forms.Binding(
"SelectedValue",
dsTest1, "tlbInvestorProject.IDInvestor"));
cmbInvestor.DataSource = dsTest1.tblInvestor;
cmbInvestor.DisplayMember = "Name";
cmbInvestor.ValueMember = "IDInvestor";

....

private void btnLoadData_Click(object sender,
System.EventArgs e)
{
daInvestor.Fill(dsTest1);
daInvestorProject.Fill(dsTest1);
}

private void btnSaveData_Click(object sender,
System.EventArgs e)
{
// to do: try / catch
daInvestor.Update(dsTest1);
daInvestorProject.Update(dsTest1);
dsTest1.AcceptChanges();

}


This method works when I have one to many relationship
but when I have
many to many relationship the rowstate is unchanged when
I use the
combobox.
On the other hand if I use DataGrid for
tblInvestorProject, updates work.
Does anybody have any idea what could be the cause? I
have tried using
EndCurrentEdit but without effect. Maybe I'm doing
something wrong?
 
Pleae include the code used to write the selected investor to th project


--

Regards - One Handed Man

Author : Fish .NET & Keep .NET

==============================
 
Sorry, i mean the code which takes the value from the combo box and puts
updates the table

--

Regards - One Handed Man

Author : Fish .NET & Keep .NET

==============================
 
Zip you project directory and access database up and send it to me and i'll
try and fix it

(e-mail address removed)

--
Regards - One Handed Man

Author : Fish .NET & Keep .NET

==============================
 
I have found the solution. One line of code was missing.
It has something to do with bindingcontext and
EndCurrentEdit method. It has to be called before Update()

Here's the code:

private void btnSaveData_Click(object sender,
System.EventArgs e)
{
// this line of code was missing
BindingContext
[dsTest1, "tlbInvestorProject"].EndCurrentEdit();

daInvestorProject.Update(dsTest1);
dsTest1.AcceptChanges();

}
 
Good, at least u solved it :)

--
Regards - One Handed Man

Author : Fish .NET & Keep .NET

==============================
Wladek said:
I have found the solution. One line of code was missing.
It has something to do with bindingcontext and
EndCurrentEdit method. It has to be called before Update()

Here's the code:

private void btnSaveData_Click(object sender,
System.EventArgs e)
{
// this line of code was missing
BindingContext
[dsTest1, "tlbInvestorProject"].EndCurrentEdit();

daInvestorProject.Update(dsTest1);
dsTest1.AcceptChanges();

}


-----Original Message-----
Wladek,

Please post the answer to this when you get it. I am
having very similar problems. (Since you have been offered
to post your code - I thought you might not get back to
the group.
Thanks
.
 
Back
Top