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

DataForm for new items only and insert error?

1 Answer 66 Views
DataForm
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Eric asked on 08 Sep 2011, 08:59 PM

I would like to use the DataForm to have a form that is only for entering new items. However, even when I tell it to effectively click the 'NewItem' button once the form is loaded it still loads the data from the database. Is this possible?

Coincidentally the inserts I have here are also failing and I don't know if it is related or not. When the 'OK' button is clicked the JIT debugger returns System.InvalidOperationException: Submit error is not handled! at Telerik.Windows.Data.QueryableDomainServiceCollectionView'1.OnDomain...submitOperation) ...

Any ideas what I am doing wrong here?

I will include the code from the DomainService, the Xaml, and the .cs file. Any help would be greatly appreciated!

Here is the section from the DomainService:

 public IQueryable<Administrators> GetAdministrators()
        {
            return this.ObjectContext.Administrators;
        }

 

        public void InsertAdministrators(Administrators administrators)
        {
            if ((administrators.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(administrators, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Administrators.AddObject(administrators);
            }
        }

        public void UpdateAdministrators(Administrators currentAdministrators)
        {
            this.ObjectContext.Administrators.AttachAsModified(currentAdministrators, this.ChangeSet.GetOriginal(currentAdministrators));
        }

 

.CS File:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
namespace BusinessApplication3.Views.Administrator_Views
{
    public partial class Admin_new2 : UserControl
    {
        public Admin_new2()
        {
            InitializeComponent();
        }

        private void myRadDataForm_Loaded(object sender, RoutedEventArgs e)
        {
            (sender as RadDataForm).AddNewItem();
        }

        private void myRadDataForm_AddingNewItem(object sender, Telerik.Windows.Controls.Data.DataForm.AddingNewItemEventArgs e)
        {
           
            try
            { AdministratorsDataSource.SubmitChanges(); }
            catch (Exception ex)
            {
            }
        }

        private void myRadDataForm_BindingValidationError(object sender, ValidationErrorEventArgs e)
        {

        }

        private void myRadDataForm_EditEnded(object sender, Telerik.Windows.Controls.Data.DataForm.EditEndedEventArgs e)
        {
            try
            {
                if(AdministratorsDataSource.HasChanges)
                    AdministratorsDataSource.SubmitChanges();

            }
            catch (Exception ex)
            {
            }
        }
    }
}


XAML:

<UserControl x:Class="BusinessApplication3.Views.Administrator_Views.Admin_new2"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
 xmlns:telerikQuickStart="clr-namespace:Telerik.Windows.Controls.QuickStart;assembly=Telerik.Windows.Controls"
    xmlns:service ="clr-namespace:BusinessApplication3.Web.Services"
    HorizontalAlignment="Center" VerticalAlignment="Center">

    <Grid x:Name="LayoutRoot" >
        <telerik:RadDomainDataSource x:Name="AdministratorsDataSource" QueryName="GetAdministratorsQuery" AutoLoad="True">
            <telerik:RadDomainDataSource.DomainContext>
                <service:EducationSchedulingDomainContext/>
            </telerik:RadDomainDataSource.DomainContext>
        </telerik:RadDomainDataSource>
        <telerik:RadDataForm x:Name="myRadDataForm" Loaded="myRadDataForm_Loaded" EditEnded="myRadDataForm_EditEnded" CommandButtonsVisibility="Add,Commit,Cancel" ItemsSource="{Binding DataView, ElementName=AdministratorsDataSource}" Height="Auto"  Width="Auto" AutoGenerateFields="False" BindingValidationError="myRadDataForm_BindingValidationError">
            <telerik:RadDataForm.NewItemTemplate>
                <DataTemplate>
                    <telerik:RadUniformGrid>
                        <telerik:DataFormDataField x:Name="firstNameNewItemField" DataMemberBinding="{Binding firstName,Mode=TwoWay}" Label="First Name:" Width="Auto"/>
                        <telerik:DataFormDataField DataMemberBinding="{Binding lastName,Mode=TwoWay}" Label="Last Name:" Width="Auto"/>
                        <telerik:DataFormDataField DataMemberBinding="{Binding officePhoneNumber,Mode=TwoWay}" Label="Office Phone:" Width="Auto"/>
                        <telerik:DataFormDataField DataMemberBinding="{Binding address1,Mode=TwoWay}" Label="Address 1:" Width="Auto"/>
                        <telerik:DataFormDataField DataMemberBinding="{Binding city,Mode=TwoWay}" Label="City:" Width="Auto"/>
                        <telerik:DataFormDataField DataMemberBinding="{Binding state,Mode=TwoWay}" Label="State:" Width="Auto"/>
                        <telerik:DataFormDataField DataMemberBinding="{Binding zipcode,Mode=TwoWay}" Label="Zipcode:" Width="Auto"/>
                        <telerik:DataFormDataField DataMemberBinding="{Binding emailAddress,Mode=TwoWay}" Label="Email Address:" Width="Auto"/>
                        <telerik:DataFormCheckBoxField DataMemberBinding="{Binding canApproveAdministrators,Mode=TwoWay}" Label="Approve Administrators:" Width="Auto"/>
                        <telerik:DataFormCheckBoxField DataMemberBinding="{Binding canApproveInstructors,Mode=TwoWay}" Label="Approve Instructors:" Width="Auto"/>
                        <telerik:DataFormCheckBoxField DataMemberBinding="{Binding canApproveCourses,Mode=TwoWay}" Label="Approve Courses:" Width="Auto"/>
                        <telerik:DataFormCheckBoxField DataMemberBinding="{Binding canApproveClasses,Mode=TwoWay}" Label="Approve Classes:" Width="Auto"/>
                        <telerik:DataFormDataField DataMemberBinding="{Binding trainingCenterID,Mode=TwoWay}" Label="Training Center:" Width="Auto" Description="Enter 1 for Corporate Office"/>
                        <telerik:DataFormDataField DataMemberBinding="{Binding createdByAdministratorID, Mode=TwoWay}" Label="Created By:" Width="Auto" Content="1"/>
                    </telerik:RadUniformGrid>
                </DataTemplate>
            </telerik:RadDataForm.NewItemTemplate>
            <telerik:RadDataForm.ReadOnlyTemplate>
                <DataTemplate>
                    <telerik:RadUniformGrid>
                        <telerik:DataFormDataField DataMemberBinding="{Binding firstName}" Label="First Name:" Width="Auto"/>
                        <telerik:DataFormDataField DataMemberBinding="{Binding lastName}" Label="Last Name:" Width="Auto"/>
                        <telerik:DataFormDataField DataMemberBinding="{Binding officePhoneNumber}" Label="Office Phone:" Width="Auto"/>
                        <telerik:DataFormDataField DataMemberBinding="{Binding address1}" Label="Address 1:" Width="Auto"/>
                        <telerik:DataFormDataField DataMemberBinding="{Binding city}" Label="City:" Width="Auto"/>
                        <telerik:DataFormDataField DataMemberBinding="{Binding state}" Label="State:" Width="Auto"/>
                        <telerik:DataFormDataField DataMemberBinding="{Binding zipcode}" Label="Zipcode:" Width="Auto"/>
                        <telerik:DataFormDataField DataMemberBinding="{Binding emailAddress}" Label="Email Address:" Width="Auto"/>
                        <telerik:DataFormCheckBoxField DataMemberBinding="{Binding canApproveAdministrators}" Label="Approve Administrators:" Width="Auto"/>
                        <telerik:DataFormCheckBoxField DataMemberBinding="{Binding canApproveInstructors}" Label="Approve Instructors:" Width="Auto"/>
                        <telerik:DataFormCheckBoxField DataMemberBinding="{Binding canApproveCourses}" Label="Approve Courses:" Width="Auto"/>
                        <telerik:DataFormCheckBoxField DataMemberBinding="{Binding canApproveClasses}" Label="Approve Classes:" Width="Auto"/>
                        <telerik:DataFormDataField DataMemberBinding="{Binding trainingCenterID}" Label="Training Center:" Width="Auto"/>
                    </telerik:RadUniformGrid>
                </DataTemplate>
            </telerik:RadDataForm.ReadOnlyTemplate>
        </telerik:RadDataForm>
    </Grid>
</UserControl>

 

1 Answer, 1 is accepted

Sort by
0
Pavel Pavlov
Telerik team
answered on 15 Sep 2011, 01:09 PM
Hello Eric,

If you do not intend to use the RadDataForm for collection navigation and only for entering new items, it would be better to use the CurrentItem property  , instead of feeding it with the whole collection trough the ItemsSource property. This way you will edit one item only and have better control on when to add it to the original collection.

Regards,
Pavel Pavlov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
DataForm
Asked by
Eric
Top achievements
Rank 1
Answers by
Pavel Pavlov
Telerik team
Share this question
or