473,394 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

DataBinding generates exception ???

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_curren tRow);

// 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
Nov 16 '05 #1
1 2495
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
"Chris" <ch********@pandora.be> wrote in message
news:vd*********************@phobos.telenet-ops.be...
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_curren tRow);

// 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

Nov 16 '05 #2

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

Similar topics

0
by: Jason S | last post by:
I haven't been able to get an answer to this in the asp.net group, hoping someone here can enlighten me. As pertains to databinding a control in a repeating fashion(datagrid, repeater, etc.)...
1
by: Chris | last post by:
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...
3
by: Kevin Swanson | last post by:
I'm writing what should be a very simple app against an Oracle database. The app has a number of user controls, any one of which is loaded into a main display page using the loadControl method,...
6
by: Nathan Sokalski | last post by:
I am using a DataSet as the DataSource of a DataList in my code. The SQL used to get the data from the database begins with: SELECT...
1
by: Dave A | last post by:
Hi, I am struggling with two way databinding in WinForms and the DataGridView. I am binding to business object classes (rather than datatables). If I have a collection of these business...
9
by: Nathan Sokalski | last post by:
I have a very simple UserControl which contains an Image and a Label, which I use to display an image with a caption. I am using this control inside a DataList, setting the two Public variables...
1
by: Owen Blacker | last post by:
I've spent loads of time Googling to try to work this one out and I'm sure it's something obvious. I get an InvalidOperationException reading "Databinding methods such as Eval(), XPath(), and...
11
by: =?Utf-8?B?R29rdWw=?= | last post by:
I am struck up with a problem and want anyone here to help me out. I am a beginner in .NET trying to learng DataBinding concepts. I have binded 4 text boxes with a dataset but when I say...
1
by: Mundo | last post by:
Hi I'm pulling my hair out with a problem caused when DataBinding a DataSet to a web control. I first tried using a GridView but when that failed, I tried a Repeater. I'm getting the classic...
0
by: theleshie | last post by:
Hi, I am creating an application which dynamically creates forms depending on the information held in a dataset (which also includes the data the application itself uses). As part of dynamically...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.