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

Newly Added item causes the program to crash.

5 Answers 185 Views
Design Time (Visual Designer & Tools)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Yas
Top achievements
Rank 1
Yas asked on 28 Jul 2010, 05:51 PM
Hello,
I'm currently testing the ORM Express Edition Q2 2010. I have successfully created the Domain Model and retrieved the table from my Database. I have a RadGridView which I bind to a table. When I run my code all data are correctly loaded in the RadGridView. I have an Event (AddingNew) which handle the process when a new row is added to the grid. My code crash when I call dbContext.SaveChanges(). The error I'm having is:
"Index was out of range. Must be non-negative and less than the size of the collection."

When I check my database, I can see that the new row has been successfully created.

Please find the enclosed picture for the Error.

Thanks.

5 Answers, 1 is accepted

Sort by
0
Jordan
Telerik team
answered on 29 Jul 2010, 08:11 AM
Hi Yas,

Thank you for your interest in OpenAccess and also  for  the screen-shot.
Usually exceptions surface through the Application.Run when they happen in some UI control.
So, this and the fact that the new row is successfully saved in the database makes me think that the issue is more probably with the RadGridView.

Anyway, in order to be able to help you further we will need some more information:
1. how exactly are you binding your grid to the data
2. what exactly are you doing in the AddingNew event handler (this is an event of the RadGridView, right?)

It will be best if you can send us a simple project that we can use to reproduce the issue locally.
Looking forward to your reply.

Greetings,
Jordan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Yas
Top achievements
Rank 1
answered on 29 Jul 2010, 06:31 PM
I will send you the sample project tomorrow but in the mean time please find the code below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WinModel;
using System.Linq;
  
namespace OpenAccessSep
{
    public partial class RadRibbonForm1 : Telerik.WinControls.UI.RadRibbonForm
    {
        public RadRibbonForm1()
        {
            InitializeComponent();
              
            WinDbContext dbContext = ContextFactory.ObtainContext();
  
            radGridView1.DataSource = dbContext.TBL_USERs.ToList();  
            radGridView1.Columns[0].HeaderText = "ID";
            radGridView1.Columns[1].HeaderText = "Title";
            radGridView1.Columns[2].HeaderText = "Name";
            radGridView1.Columns[3].HeaderText = "Surname";
            radGridView1.Columns[4].HeaderText = "NID";
            radGridView1.Columns[5].HeaderText = "Address";
            radGridView1.Columns[6].HeaderText = "City";
            radGridView1.Columns[7].HeaderText = "Country";
            radGridView1.Columns[8].HeaderText = "Join Date";
            radGridView1.Columns[9].HeaderText = "Position";
            radGridView1.Columns[10].HeaderText = "Create Date";
            radGridView1.Columns[11].HeaderText = "Last Login";
            radGridView1.Columns[12].HeaderText = "Account Status";
  
            radGridView1.Columns[0].IsVisible = false;
            radGridView1.Columns[0].VisibleInColumnChooser = false;
  
  
  
            radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
        }
  
        private void radGridView1_UserAddingRow(object sender, Telerik.WinControls.UI.GridViewRowCancelEventArgs e)
        {           
            try
            {
                TBL_USER lNewUser = new TBL_USER()
                {
                    USERTITLE = e.Rows[0].Cells[1].Value.ToString(),
                    USERFIRSTNAME = e.Rows[0].Cells[2].Value.ToString(),
                    USERLASTNAME = e.Rows[0].Cells[3].Value.ToString(),
                    USERNIDNUMBER = e.Rows[0].Cells[4].Value.ToString(),
                    USERADDRESS = e.Rows[0].Cells[5].Value.ToString(),
                    USERCITY = e.Rows[0].Cells[6].Value.ToString(),
                    USERCOUNTRY = e.Rows[0].Cells[7].Value.ToString(),
                    USERJOINDATE = (DateTime)e.Rows[0].Cells[8].Value,
                    USERPOSITION = e.Rows[0].Cells[9].Value.ToString(),
                    USERCREATEDATE = (DateTime)e.Rows[0].Cells[10].Value,
                    USERLASTLOGIN = (DateTime)e.Rows[0].Cells[11].Value,
                    USERSTATUS = e.Rows[0].Cells[12].Value.ToString()
                };
  
                WinDbContext dbContext = ContextFactory.ObtainContext();
  
                dbContext.Add(lNewUser);
                dbContext.SaveChanges();                
            }
            catch (Exception ex) 
            
            }
            finally 
            
            }
        }        
    }
}
0
Petko_I
Telerik team
answered on 30 Jul 2010, 07:45 PM
Hello Yas,

We have located the cause of the problem you encounter. It turned out that  RadGridView  is not  throwing the right type of exception and this will be fixed soon. In the meantime, what you can do to bring your project to a working state is:

1) The first binding of the context collection of objects to the RadGridView must be invoked with the ToList() method as it is now. This measure is required as binding directly to the underlying table when it is empty will not make the RadGridView appear.
2) You should cancel the addition of a row to the RadGridView before the actual object was saved to the database.
3) You should check whether the e.Rows[i].Cells[j].Value is null before calling the ToString() method.
4) After you have successfully persisted an object, you need to refresh the RadGridView DataSource as the collection is detached from the list provided by the context.
5) An additional step is required to clear the row which has been used for editing since it has been cached. That step, in fact, is a glitch on our side which will also be fixed.

All in all, your code should look like this:

private void radGridView1_UserAddingRow(object sender, Telerik.WinControls.UI.GridViewRowCancelEventArgs e)
{
    e.Cancel = true;
 
    TBL_USER lNewUser = new TBL_USER();
 
    if (e.Rows[0].Cells[1].Value != null)
    {
        lNewUser.USERTITLE = e.Rows[0].Cells[1].Value.ToString();
    }
 
    if (e.Rows[0].Cells[2].Value != null)
    {
        lNewUser.USERFIRSTNAME = e.Rows[0].Cells[2].Value.ToString();
    }
             
    // and so on...
 
    WinDbContext dbContext = ContextFactory.ObtainContext();
    dbContext.Add(lNewUser);
    dbContext.SaveChanges(); 
 
    this.ClearNewRow();
    radGridView1.DataSource = dbContext.TBL_USERs;
}
  
private void ClearNewRow()
{
    for (int i = 0; i < radGridView1.ColumnCount; i++)
    {
        radGridView1.MasterView.TableAddNewRow.Cells[i].Value = null;
    }
}

I hope this example will solve the problems you experience at the moment. Do not hesitate to contact us for further assistance.


Sincerely yours,
Petko_I
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Julian
Top achievements
Rank 1
answered on 04 Mar 2011, 10:42 AM
Hi Geeks :)

I faced with the same issue, but my scenario is little bit different from the author of this post.

Steps to reproduce:

1. Click "add new item" in the grid.
2. Press "Escape" button (Once), to still have the item in the grid.
3. Click on the header of the column, the "add new item" control will appears.
4. Click on it ("add new item")

Result: The crash will appears.

In attachment you will find all the mentioned steps in graphical example.

Thx,
Julian Ust.
0
Julian
Top achievements
Rank 1
answered on 04 Mar 2011, 10:54 AM
Guys,
I've duplicated this post here:

http://www.telerik.com/community/forums/wpf/gridview/newly-added-item-causes-the-program-to-crash.aspx ,

because it is related to the RadGridView.

Sorry for duplication.

regards,
Julian Ust.
Tags
Design Time (Visual Designer & Tools)
Asked by
Yas
Top achievements
Rank 1
Answers by
Jordan
Telerik team
Yas
Top achievements
Rank 1
Petko_I
Telerik team
Julian
Top achievements
Rank 1
Share this question
or