DataBinding generates exception ???

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

Chris

Hi,

I have a DB-table 'AirplaneTypes' that has to 2 fields that form the primary
key :
'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 this binding is not executed at Form_Load() :
no exception thrown by 'AcceptChanges' (???)

// Following DataBindings don't create any problems (only the
previous??)
txtModel.DataBindings.Add("Text", myTable, "at_model");
txtLength.DataBindings.Add("Text", myTable, "at_length");
}

Below is the buttonInsert-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 'sqlDataAdapter1.Update() ' performs well.
}

Any help GREATLY appreciated !!

Chris
 
Chris, AcceptChange will end any existing edits out there, so if you had a
row in edit mode, it wouldn't be sent back to the db so the update would
work, when you call AcceptChanges, it will end the edit, reset the rowstate
etc. Make sure that the current edit is finished before calling update b/c
I think the value may be getting into the db, then when you try
AcceptChanges, it's trying to add what's already there.. Before you call
AcceptChanges, do a DataTable.Select on the primary key
http://www.knowdotnet.com/articles/adopartiii.html and see if that value
already exists. You may just need to do an endcurrentedit or EndEdit method
before calling update. http://www.knowdotnet.com/articles/adopartiii.html
 

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

Similar Threads


Back
Top