Click here to Skip to main content
15,868,349 members
Please Sign up or sign in to vote.
4.00/5 (1 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.

C#
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
        return;
    }
    Data.VMSDataSet.CustomerRow row = this.VMSDataSet.Customer.NewCustomerRow();

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

    this.VMSDataSet.Customer.Rows.Add(row);
}


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.)
C#
this.GridView.Rows.RemoveAt(this.GridView.Rows.Count - 1);
Posted
Updated 25-Aug-12 0:55am
v9

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.

—SA
 
Share this answer
 
v3
Comments
[no name] 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".
--SA
[no name] 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.
Sergey Alexandrovich Kryukov 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?
--SA
[no name] 24-Aug-12 20:48pm    
Please see changes!
Thanks.
list box data cannot be remove
 
Share this answer
 
Comments
[no name] 12-Jul-13 11:59am    
Really? And can you cite a reference for this inability to ever remove data from list boxes?
iiiuhuhu>
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900