Click here to Skip to main content
13,555,266 members
Rate this:
Please Sign up or sign in to vote.
See more:
My Tables in dataset don't accept null value.
There is DataBound ListBox and a BindingNavigator. They have a same BindingSource. When I Click on AddNewItem a dialog will open to get entries from user.

public void AddNewItem_Click(object sender, EventArgs e)
    CustomerDialog dialog = new CustomerDialog();

    if (dialog.ShowDialog() == DialogResult.Cancel)
        this.Customer.Items.RemoveAt(this.Customer.Items.Count - 1);// Error
    Data.VMSDataSet.CustomerRow row = this.VMSDataSet.Customer.NewCustomerRow();

    row.FirstName = dialog.FirstName;
    row.LastName = dialog.LastName;


At specified line I've got this error:
Items collection cannot be modified when the DataSource property is set.

How can I avoid it?

(Note: I had same problem with DataGridView before, in that case I write the below code without error.)
this.GridView.Rows.RemoveAt(this.GridView.Rows.Count - 1);
Posted 20-Aug-12 9:00am
Updated 25-Aug-12 0:55am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Handling this kind of exception does not make any practical sense. You rather need to prevent it.

First of all, this type of exceptions is one of the easiest to detect, to fix the problem. The nature of it is that you try to dereference a null reference. A work-around should be based on elimination of such dereferencing, which never makes sense. You can make sure that some variable or member is always initialized to refer to some actually existing (created) object. Alternatively, test а variable or member in question for null and, if it is null, don't do any operations using any of its instance members. All other cases make no sense.

SAKryukov 21-Aug-12 13:13pm
Not only you should not handle the exception, you don't need to handle it, because you shall avoid it. So your question could be about how to avoid throwing this exception, nothing else. Both of your RemoveAt calls are valid, because the indices go 0 .. Count-1 -- they remove last item; so the problem could be somewhere else.
Your "does not work" is not descriptive. This is not a language a developer should use. What, same exception?
After all, create a complete but minimal code sample and try to reproduce this problem only, isolated from everything else. If you cannot see how to work it out, show this sample using "Improve question".
SAKryukov 22-Aug-12 0:48am
I trust you, just don't understand. A logic error... Exception full type and message, please... :-)
No exception? Then what is that error, exactly?
SAKryukov 25-Aug-12 2:10am
Now it's clear. Isn't it clear enough? DataSource property should be null to be able to change the GridView. Or modify your grid view through binding only. The control does not allow you to do both ways. This is pretty much understandable; as it would create a disaster. What to do? Depending on what you want.
SAKryukov 5-Sep-12 3:29am
No, I got it, only did not see exact scenario which causes the problem. At the same time, if you look at your own fix, you will see that it follows my general recommendation to avoid mixing binding with direct manipulation with the control's data -- please see my comment above.

Anyway, it's great that you did the right fix.
Good luck,
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

list box data cannot be remove
NotPolitcallyCorrect 12-Jul-13 11:59am
Really? And can you cite a reference for this inability to ever remove data from list boxes?
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

TheRealSteveJudge 2-Dec-14 10:55am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02-2016 | 2.8.180515.1 | Last Updated 2 Dec 2014
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100