error when changing currencyManager.Position

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

Hi,

after inserting a record in a DataSet and committing it to the DB using the
sqlDataAdapter.Update(), do I want to set the position of my
currency-manager to the last row in the dataSet (using the same working code
that is executed when I click on a Lastrecord-button).


m_CurrencyManager.Position = m_dsAirplaneTypes1.AirplaneTypes.Rows.Count -
1;

But changing the position result in an error :
"Colunm <name of primarykeyfield> is constrained to be unique. Value
<avalue> is already present"

who can help me please ?

thnx
Chris
 
Chris:

Can you navigate anywhere or is it only the last row?

From the error, it looks like something is trying to add a row.... is there
anything in the code that might be doing this? Usually you see this error
when you try to add a row with a value on the PK that already exists
 
Hi William,

the insert works fine (the record is added to the table) but just navigating
to the last row (indeed) generates the exception.

Is it maybe because the currency manager is out sync because a new record
has been added, so that both must be synchronised in some way (just
guessing).

Any ideas ?
thnx
Chris
 
Chris:

Are you adding a new row? That's the problem. Whatever the default value
that's going into the PK is is already used. Is it AutoIncrement? If so,
check the seed. It's not the BindingContext that's causing the exception,
it's probably just causing the currentrow to change and since the values
aren't valid, it's barking..

To narrow this down, do an insert, then navigate to any other row, not the
last one. Will this work? I'm guessing it's not going to b/c the rowchange
will fire and the vaule will take.

The currencymanager is aware of the underlying datastrcuture so if you add
a a valid value to it, its count changes immediately and you cna navigate to
it. I do it all the time. That error you are getting is specifically with
adding a new record and that's almost surely the problem, the currency
manager is just making it bark.

Navigate somewhere else after the insert and lets see what happens.

Bill
 
Hi William,

I've simplified my app a little : I have removed all navigation items
(including the CurrencyManager-code) and I have the exception when I perform
a DataBind on one of key fields ???

Here's what I did :
I had to redesign my DB first. The 2 fields that form the primary key are
'at_name' and 'at_model' both in the DB defined as type :
char 15

MyWinform client :

void Form_Load(...) {
// m_dsAirplaneTypes1 is the DataSet
DataTable myTable = m_dsAirplaneTypes1.AirplaneTypes;
txtName.DataBindings.Add("Text", myTable, "at_name");
==> when this databind is executed will an exception be thrown from
the moment
I perform an 'AcceptChanges' on my DataSet (see below)
If commented : no problem ???

// Following DataBindings don't create any problems : only the previous
one ???
txtModel.DataBindings.Add("Text", myTable, "at_model");
txtLength.DataBindings.Add("Text", myTable, "at_length");
}
Below is the button-click event that will generate an exception, not after I
enter values for the first time :
txtName.Text = "1" txtModel.Text = "1" txtLength.Text
= "1"
but when entering values the 2nd time :
txtName.Text = "2" txtModel.Text = "2" txtLength.Text
= "2"

void btnInsert_Click(...) {
m_currentRow = m_dsAirplaneTypes1.AirplaneTypes.NewRow();

// Edit the fields
m_currentRow["at_name"] = txtName.Text.Trim();
m_currentRow["at_model"] = txtModel.Text.Trim();
m_currentRow["at_length"] = txtLength.Text.Trim();

// Add to DataSet
m_dsAirplaneTypes1.AirplaneTypes.Rows.Add(m_currentRow);

// Add to DB
int n = sqlDataAdapter1.Update(m_dsAirplaneTypes1, "AirplaneTypes");
if (n > 0)
m_dsAirplaneTypes1.AirplaneTypes.AcceptChanges();

==> EXCEPTION thrown when I execute the 2nd time (and I enter
different

values !!)
"Column 'at_name, at_model' is constrained to be unqiue.
Value '2, 2' is already present"

BUT THE DATA HAS BEEN ENTERED IN THE DB CORRECTLY !
So the sqlDataAdapter1.Update() performs well.
}

Can you help me please ?

thnx

Chris
 
Back
Top