Click here to Skip to main content
13,402,824 members (42,959 online)
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 10:00am
Updated 25-Aug-12 1: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.

Meysam Tolouee 21-Aug-12 3:35am
I've already detected that. I know what cause the exception, but I don't know how handle it.
When I click On AddNewItem Button on form there is no problem but when I click on AddNewItem Button on BindingNavigator a row(item) will add to ListBox even there is No code in handler method. If the consumer was GridView I do this:

this.GridView.Rows.RemoveAt(this.GridView.Rows.Count - 1);

and the null row's gone.but when I do that for LIstBox like this:

this.ListBox.Items.RemoveAt(this.ListBox.Items.Count - 1);

It doesn't remove the null item, added to items.
Sergey Alexandrovich Kryukov 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".
Meysam Tolouee 21-Aug-12 22:22pm
OK, I try to speak more sufficient.
Both of RemoveAt() calls are valid, but the second one make a logic error(instead of saying "doesn't work".)
I wish you trust me on what I say.
I trust you, just don't understand. A logic error... Exception full type and message, please... :-)
No exception? Then what is that error, exactly?
Meysam Tolouee 24-Aug-12 20:48pm
Please see changes!
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.
Meysam Tolouee 25-Aug-12 6:53am
Probably You didn't read my note, because DataGridView allows me but ListBox doesn't. I solved that anyway.
I write the correct code for guys who probably will have the same error.

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
ThePhantomUpvoter 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 |
Web03 | 2.8.1802120.2 | 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