Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AddItem() throwing "Item cannot be added to a read-only or fixed-size"

Status
Not open for further replies.

newbie1001

Programmer
Mar 5, 2007
3
CA
Hi Guys,

I have the following code for a bound data source:

private OrderEntryWS.OrderEntryWS objServer = new OrderEntryWS.OrderEntryWS();

private void Form_Load(object sender, EventArgs e)
{
// Populate the binding source with
// all the data from the orders table.
// This method is exposed from the Web Service
orderBS.DataSource = objServer.GetOrders();
}

private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
{
// Add a new record to the list
orderBindingSource.AddNew();
recordState = "add";
}



The add new routine should clear the form (by way of moving to the last record plus one) for new data to be entered, but when the AddNew routine is called, I get the following error:

"Item cannot be added to a read-only or fixed-size list."

Can someone twll me what is going on here.

Thanks,
Dave
 
Without wanting to sound silly, basically the collection you're dealing with is a 'read-only or fixed-size list' as per the exception. In response tho', a few questions as some things are unclear from the posted code;

i. What type is the orderBindingSource (collection) object?
ii. What object (collection) type is returned by the GetOrders() web method?

Rhys
The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offense Edsgar Dijkstra

Church of the Flying Spaghetti Monster
 
Hi Rhys666,

Thank yo for your response. They are both of BindingList<Orders> type. Order is a class type.

Is there any way to change the list from a fixed length to a non fixed length... I am thinking maybe casting. Your thoughts.

Cheers
 
I'd probably look at the underlying implementation of the Binding type collection classes, and the methods being utilised for populating them.

My last project did almost exactly what you're doing except we implented the ability to utilise remoting over http, (or not - configured in the config file strangely enough :)), and didn't even consider a web service as this was an entirely internal application. I've never had the problem you're getting which is why I'm thinking the methods you're calling or the implementation of the BindingList collection is marking the collection read only or of a fixed size, hence the exception...

Rhys
The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offense Edsgar Dijkstra

Church of the Flying Spaghetti Monster
 
Hi Rhys666,

I found the problem, or rather my prof did. SOAP serialized my BindingList<Order> into an array of Order. So what I had to do was convert the resultset back to a binding list. Here is the code I used:

private OrderEntryWS.OrderEntryWS objServer =
new OrderEntryWS.OrderEntryWS();

private void BindDataSource()
{
orderBS.DataSource =
ConvertToList(objServer.GetOrders());
}

public BindingList<Order> ConvertToList(Order[] oList)
{
BindingList<Order> nList = new BindingList<Order>();

for (int i = 0; i < oList.Length; i++)
{
nList.Add(oList);
}
return nList;
}


Then in the form load I simply call the BindDataSource() method as so:

private void Form1_Load(object sender, EventArgs e)
{
BindDataSource();
}


This is actually my profs code, not mine... I don't think I would have figured this out on my ouw... but then I am very new to c#.

Thanks for you help though. I appreciate it.

Cheers,
Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top