This is a migrated thread and some comments may be shown as answers.

Application Exception

26 Answers 464 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Guy
Top achievements
Rank 1
Guy asked on 01 Nov 2010, 05:26 PM
Hi,

First of all I hope I'm posting this in the right place.

I have just finished building my first application, published it and have now (as best as I can tell) installed it on a couple of machines.

Unfortunately when the users are trying to add a row on a gridview they are getting the following error message.

System.InvalidOperationException: Items cannot be added to the RadListSource when is in data-bound mode

I have no idea what this is or how to resolve it. I've tried various searches with little results. The application works perfectly when I'm in design mode but I too get the same error so I think I have ruled out any DotNet issues.

I'm really hoping someone here can help.

Kind regards,


Guy

26 Answers, 1 is accepted

Sort by
0
Guy
Top achievements
Rank 1
answered on 02 Nov 2010, 10:09 AM
Just to give a bit more background into what I'm doing which will hopefully identify the issue more clearly.

I have several gridviews which the user updates from a second form. When the user saves the info, the data is passed back to the parent form as an object which is then used to add the row.

Once this is complete I am using this.TableAdapter.Update(this.DataSet.Table) to then save the changes.

It is during this process that I receive the error.

Although the users receive the error, they are able to continue and the changes are being saved to the Access database.

Kind regards,

Guy
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 02 Nov 2010, 10:38 AM
Morning Guy, 

It sounds as though you are trying to add a new row to a RadGridView directly when the grid is bound to a datasource. 

If you want to add new rows in that way, you need to work in unbound mode. See this link for more information on working in unbound mode. 

Otherwise, you would need to add the new data row to your dataset and update that which would be reflected in the grid. 

If this doesn't help, please can you post some code, formatted in the correct language and also state what version of the controls you are using. 
Thanks
Richard
0
Guy
Top achievements
Rank 1
answered on 02 Nov 2010, 03:08 PM
Afternoon Richard,

Thanks for getting back to me.

Below is some example code of what I am doing in my project.

Form1
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls.UI;
using Telerik.WinControls.Data;
  
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
         
        public Form1()
        {
            InitializeComponent();
        }
  
        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'nameListDataSet.ListOfNames' table. You can move, or remove it, as needed.
            this.listOfNamesTableAdapter.Fill(this.nameListDataSet.ListOfNames);
        }
  
        private void AddNameButton_Click(object sender, EventArgs e)
        {
            object[] NewName = new object[1];
            NewName[0] = NameEntryTextBox.Text;
            radGridView1.Rows.Add(NewName);
            this.listOfNamesTableAdapter.Update(this.nameListDataSet.ListOfNames);
        }
  
        private void PopupEntryButton_Click(object sender, EventArgs e)
        {
            Form2 EntryForm = new Form2(this);
            EntryForm.ShowDialog();
        }
  
        public void UpdateName(object[] NameArray)
        {
            radGridView1.Rows.Add(NameArray);
            this.listOfNamesTableAdapter.Update(this.nameListDataSet.ListOfNames);
        }
            
  
  
    }
}


This form simply has a gridview, a textbox for name entry (to test if there was an issue updating the gridview from another form) and two buttons. One to save the name from the text box and another to show the second form.

I am connecting to an Access database called NameList which has a table called ListOfNames and a column called Name.

The second form looks like this.

Form2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
  
namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        private Form1 m_parent;
  
        public Form2(Form1 MainForm)
        {
            InitializeComponent();
            m_parent = MainForm;
        }
  
        private void Form2_Load(object sender, EventArgs e)
        {
  
        }
  
        private void EnterNamePopupButton_Click(object sender, EventArgs e)
        {
            object[] NewName = new object[1];
            NewName[0] = PopupNameEntryTextBox.Text;
            m_parent.UpdateName(NewName);
            MessageBox.Show("Changes made");
            PopupNameEntryTextBox.Text = "";
        }
    }
}


When I publish and test this, it works perfectly but for some reason (and now with some more testing) I am seeing random errors like in my OP.

I've not worked with Access much so i'm wondering if there is going to be an issue with concurrent users accessing the database over the network.

From seeing the error myself, it seems to occur after the user has pressed the save button on the second form and before the confirmation message of the changes being made.

The version of the controls that I am using are 2010.2.10.914.

Kind Regards,

Guy

0
Richard Slade
Top achievements
Rank 2
answered on 02 Nov 2010, 03:29 PM
Hi Guy, 

I think that the issue here is that you are trying to add a row to the GridView when it is in bound mode. I.e. It is bound to a datasource, and then you are adding the row directly to the grid. 
radGridView1.Rows.Add(NewName);

We also add data to our grids from another form. You should be gathering the data into a new row object (of your data table), adding that new row to your data table and updating your dataset. Because the Grid is bound to the dataset, it will reflect the change. You don't need to be adding a row to the grid itself. 

Hope that helps, but let me know if that's not what you meant. 
Richard
0
Guy
Top achievements
Rank 1
answered on 02 Nov 2010, 03:39 PM
Hi Richard,

Again, thanks for your reply.

I think I know what you mean; so instead of me adding the row to the grid and then saving, I should be directly adding to the datatable and then those changes will be automatically reflected in the grid itself?

I had a look at the follwing msdn page earlier after your first post but couldn't seem to get my head round it and implement it into my code.

How to: Add Rows to a DataTable

Is this the way I should be doing it?

Thanks,

Guy
0
Richard Slade
Top achievements
Rank 2
answered on 02 Nov 2010, 03:48 PM
Hi Guy, 

Yep, add the row as described and then you can refresh the grid to see the changes. 

Here is an extract (in VB0 of one of our application's add methods that gets the row data from the form that has had the new data input into it. The table is updated and the grid is refreshed to reflect the changes. 

Private Sub Add()
 
    Dim form As Forms.PricingPolicies.VatMaintenanceEdit
    Dim rowData As DataSet.VatDescRow
 
    rowData = Me.DataSet.VatDesc.NewVatDescRow()
    rowData = Data.DefaultValue.Vat(rowData, My.User.UserName)
    Me.DataSet.VatDesc.AddVatDescRow(rowData)
 
    form = New Forms.PricingPolicies.VatMaintenanceEdit(CompletionMode.Add, rowData)
    If form.ShowDialog() = Windows.Forms.DialogResult.OK Then
        rowData = form.Row
 
        Me.TableAdapterVatDesc.Update(rowData)
        Me.DataSet.AcceptChanges()
 
        Multepos.Controls.GridView.GridViewSupport.SetBestFitAutoFill(Me.GridView)
    Else
        Me.DataSet.RejectChanges()
    End If
End Sub
0
Guy
Top achievements
Rank 1
answered on 02 Nov 2010, 04:10 PM
Hi Richard,

Sorry, but how do I translate this into C#?

How would I fit it into the code example I used so that obviously I can test it with the code example I've made?

I'm assuming I would still use the passed object from Form2 and then update the datatable from the called function in Form1 and then use Fill to display the updated information.

My data is already filtered in the gridview that the rows are added to so will this default the gridview or will the filtering still be applied?

Thanks,

Guy
0
Richard Slade
Top achievements
Rank 2
answered on 02 Nov 2010, 04:21 PM
Hi Guy, 

You can use the Telerik Code Coverter to covert code from VB to C# and back again. 

In this example, your form2 would be the form where you would add a row.
For example, in the constructor of Form2, it would accept an empty row which you would then populate with data in Form2 (from the things that the user has completed) and then expose a property in Form2 that would return the row (with the data completed). 

You can then update the table with the row data and accept the changes on the dataset. 

E.g. 
Form1 - Add Method
* Create Form2
* Create an empty / new row
* Pass the new, empty row to Form2

* User completes Form2
* You update the row data in form2 with whatever the user has completed
* Form2 exposes a property with the completed row data
* When user closes Form2, you can get to the property and update with the row data. 

Hope this helps
Richard




0
Guy
Top achievements
Rank 1
answered on 02 Nov 2010, 05:31 PM
Hi Richard,

First off, thanks for that converter link, very helpful!

In my example I will be using textboxes and dropdowns to eventually populate the row. (see attachment)

From reading the msdn link I gave above, I think I have some working code that I wondered if you could tell me is correct.

private void AddNameButton_Click(object sender, EventArgs e)
        {
            NameListDataSet.ListOfNamesRow newListOfNamesRow = nameListDataSet.ListOfNames.NewListOfNamesRow();
            newListOfNamesRow.Name = NameEntryTextBox.Text;
            nameListDataSet.ListOfNames.Rows.Add(newListOfNamesRow);
    }

This example works using the example code I gave above and currently from Form1. What I plan to hopefully do is from Form2 the user will fill in the required fields from those available then once they submit them a function from the parent form will fire and using the controls from Form2 populate the row.

I'd be quite keen to pass the object from From2 as I currently have but I am unsure how to do that.

Would the above ideas work or am I setting myself up for more problems?

Thanks,

Guy
0
Richard Slade
Top achievements
Rank 2
answered on 02 Nov 2010, 08:17 PM
Hello Guy,

this would work for your situation I imagine. If I have it right what you are trying to do, then your Form2 would be launched as a dialog box. Form2 would have a property that would be the objdect (the row) that you want to pass back. When the user closes the form, you can capture the dialog result and, if it's good, get the object (the property that is the row) and do what you need to do with it,

it's not so much telerik stuff this, but I'll be glad to help if you need it.
Richard
0
Guy
Top achievements
Rank 1
answered on 02 Nov 2010, 08:50 PM
Hi Richard,

I apologise if now my request has fallen outside the scope of the telerik controls and I most appreciative of your help.

That's exactly what i'm hoping to achieve but i'm looking to do it without being fired when the user closes the form. Where i'm stuck (and I don't know which is the best method to use) is deciding between either sending the object back to Form1 and then using that or from Form2 fire a function in Form1 which reads the values of the required controls on Form2.

Also, with my above code will that now stop my original problem from happeneing?

Thanks,

Guy
0
Richard Slade
Top achievements
Rank 2
answered on 02 Nov 2010, 09:15 PM
No problem Guy.

In my view, you don't have to send it. You just need to expose the object you want in a property of the form. For as long as you have access to the form, you have access to the property (your object).

As for your original issue, as long as you are not trying to directly add rows to the grid when it is bound to a datasource, there should be no problem.

Hope that helps
Richard
0
Guy
Top achievements
Rank 1
answered on 03 Nov 2010, 12:18 PM
Hi Richard,

That worked perfectly! Cannot thank you enough.

I am getting some odd results though; despite the array being in the correct order for the headers, the order it is being added in is jumbled up.

Is there any reason for this as it was working fine before?

Thanks,

Guy
0
Richard Slade
Top achievements
Rank 2
answered on 03 Nov 2010, 12:26 PM
Good morning Guy, 

Glad that this is now working for you (remember when we have completed sorting this to mark as answer so others can quickly find the solution too)

With regards to the order, I'm a little unsure as to what you mean. 
Do you mean that you have, (for example) a grid of Names and when you add a new one in, they are not in the correct order? 
Let me know, and I'll try and help. 

All the best
Richard
0
Guy
Top achievements
Rank 1
answered on 03 Nov 2010, 12:35 PM
Richard,

I have marked one of your earlier answer as it was more within the scope of my original question as to help others.

Regarding my other issue, I have a grid which has the following 14 headers:

* Account Number
* Title
* Initial
* First Name
* Last Name
* Job Title
* Tel Number
* Email
* Responsibility1
* Responsibility2
* Responsibility3
* Responsibility4
* Responsibility5
* Notes

In my code I get these in to the object using the following code

object[] NewCustomer = new object[14];
                NewCustomer[0] = AddDetailContactsAcctNumText.Text;
                NewCustomer[1] = TitleText.Text;
                NewCustomer[2] = InitialText.Text;
                NewCustomer[3] = FirstNameText.Text;
                NewCustomer[4] = LastNameText.Text;
                NewCustomer[5] = JobTitleText.Text;
                NewCustomer[6] = TelNumText.Text;
                NewCustomer[7] = EmailText.Text;
                NewCustomer[8] = Responsibility1Text.Text;
                NewCustomer[9] = Responsibility2Text.Text;
                NewCustomer[10] = Responsibility3Text.Text;
                NewCustomer[11] = Responsibility4Text.Text;
                NewCustomer[12] = Responsibility5Text.Text;
                NewCustomer[13] = CustNotesText.Text;

I have checked to make sure that all the controls are named correctly but when I add them the order is lost and they come out in the following order:

* Notes
* Job Title
* Tel Number
* Email
* Title
* initial
* First Name
* Responsibility1
* Responsibility2
* Responsibility3
* Responsibility4
* Responsibility5
* Last Name
0
Richard Slade
Top achievements
Rank 2
answered on 03 Nov 2010, 01:14 PM
Hi Guy, 

So, you have a grid with the headers that you have described. How are you creating these columns? Via code, or through the designer? 

The order of the columns should not change. Can you show me how you are adding these columns? 
Thanks
Richard
0
Guy
Top achievements
Rank 1
answered on 03 Nov 2010, 01:40 PM
Hi Richard,

The column are from the designer.

The code that I use to take the object and then add the row is

public void UpdateContacts(object[] ContactsArray)
{
    MercuryDBDataSet.ContactsRow newContactRow = mercuryDBDataSet.Contacts.NewContactsRow();
    mercuryDBDataSet.Contacts.Rows.Add(ContactsArray);
    this.contactsTableAdapter.Update(this.mercuryDBDataSet.Contacts);
}


Thanks,

Guy
0
Richard Slade
Top achievements
Rank 2
answered on 03 Nov 2010, 01:51 PM
Hi Guy,

I haven't been able to replicate getting the columns to move at the moment. I have just added some columns and then added a new row.
Me.RadGridView1.Columns.Add("Account Number")
Me.RadGridView1.Columns.Add("Title")
Me.RadGridView1.Columns.Add("Initial")
Me.RadGridView1.Columns.Add("First Name")
Me.RadGridView1.Columns.Add("Last Name")
Me.RadGridView1.Columns.Add("Job Title")
Me.RadGridView1.Columns.Add("Tel Number")
Me.RadGridView1.Columns.Add("Email")
Me.RadGridView1.Columns.Add("Responsibility1")
Me.RadGridView1.Columns.Add("Responsibility2")
Me.RadGridView1.Columns.Add("Responsibility3")
Me.RadGridView1.Columns.Add("Responsibility4")
Me.RadGridView1.Columns.Add("Responsibility5")
Me.RadGridView1.Columns.Add("Notes")
Dim NewCustomer As Object() = New Object(13) {}
NewCustomer(0) = "123456"
NewCustomer(1) = "mr"
NewCustomer(2) = "J"
NewCustomer(3) = "richard"
NewCustomer(4) = "slade"
NewCustomer(5) = "developer"
NewCustomer(6) = "01202"
NewCustomer(7) = "me@me.com"
NewCustomer(8) = "res"
NewCustomer(9) = "res"
NewCustomer(10) = "res"
NewCustomer(11) = "res"
NewCustomer(12) = "res"
NewCustomer(13) = "Notes"
Me.RadGridView1.Rows.Add(NewCustomer)

What version of the controls are you using?
richard
0
Guy
Top achievements
Rank 1
answered on 03 Nov 2010, 01:56 PM
Hi Richard,

I'm currently using version 2010.2.10.914.

The strange thing is that it's only started to do this once I stopped adding the rows to the grid which was giving me my original problem. I might be wrong, but is that how you've added the row in your example?

Guy
0
Richard Slade
Top achievements
Rank 2
answered on 03 Nov 2010, 03:01 PM
Hi Guy, 

I'm afraid I haven't been able to replicate it it in any scenario. Our applications do not add the row directly to the grid and we've never seen this happen before. (the only thing I can think of is that you are loading a layout file which is maintaining an old order)

The example I gave does add the row directly to the grid, and again, I can't get it to change the order on that either. 

Richard
0
Guy
Top achievements
Rank 1
answered on 03 Nov 2010, 03:31 PM
Hi Richard,

Thanks again for taking time out of your day to help me.

As a test I have used my old method of addings rows (which is where I had my initial problems of adding to the gridview directly when its bound) and the new method to add directly to the datatable.

I used the following code

public void UpdateContacts(object[] ContactsArray)
{
    MercuryDBDataSet.ContactsRow newContactRow = mercuryDBDataSet.Contacts.NewContactsRow();
    mercuryDBDataSet.Contacts.Rows.Add(ContactsArray); // new add row method produces issues
    radGridView2.Rows.Add(ContactsArray); // old method displays correctly
    this.contactsTableAdapter.Update(this.mercuryDBDataSet.Contacts);
}


Whats interesting is that the new method will not display the data in the correct columns whilst the old method does.

I'm very reluctant to get round the issue by changing the values to do a half baked job but i'm seeing little alternative.

I've also attached a screenshot of the columns i'm using.

Guy
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 03 Nov 2010, 04:16 PM
Hi, 

Have you checked your dataset designer too? I'd advise removing and re-adding your dataset via the designer if that's how you're building your dataset, checking that you are not loading any layouts, and giving it a go in a small sample project. 

Let me know if any of that helps
Richard
0
Guy
Top achievements
Rank 1
answered on 03 Nov 2010, 06:07 PM
Hi Richard,

Well spotted! It was the dataset designer which was show the columns in a different order. Must've not retained the order from where i've made changes in the database or is this normal behaviour?

Is there an easy way to changing the column layout? I read a few sites suggesting I need to replace the dataset but it's just broken all my references and layouts. If I do this how should I handle the fact that some of my columns are manual as I swwapped some out for dropdowns?

I have also been testing the layout by putting numbers in each field to see if they are in sequence. From the numbers they were I then told the object to reference the controls in that order and then retested the sequence. When I did this I got a different set of numbers which were still not in order.

Thanks,

Guy
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 03 Nov 2010, 06:23 PM
Hi Guy,

Glad that worked for you!

I'm a little confused as to the issue you are left with, but from experience in our applications (the ones that are using datasets), then if we change the dataset, then we often need to re-add the dataset to the form, and re-bind everything.

You can also save a layout of the grid, so that the user will always have a familiar layout.
Have a look at this link for information about saving and loading layouts.

Does that help?
Richard
0
Guy
Top achievements
Rank 1
answered on 04 Nov 2010, 12:49 PM
Hi Richard,

It was just a case of simply rebinding everything and then setting up all the properties of the columns again.

Thank you again for all your help on this, I know we went a little off topic from the original post so thank you for helping me understand things better and taking time out from your day.

Best regards,

Guy
0
Richard Slade
Top achievements
Rank 2
answered on 04 Nov 2010, 12:52 PM
No problem. Glad you have this part sorted. 
All the best
Richard
Tags
General Discussions
Asked by
Guy
Top achievements
Rank 1
Answers by
Guy
Top achievements
Rank 1
Richard Slade
Top achievements
Rank 2
Share this question
or