In-line validation problem

Last post 06-04-2009 3:00 AM by sjnaughton. 5 replies.

Sort Posts:

  • In-line validation problem

    06-02-2009, 5:04 PM
    • Member
      13 point Member
    • mdesoysa
    • Member since 12-03-2008, 9:04 PM
    • Posts 60

    Hi there,

    I am currently using a method from an earlier post (http://forums.asp.net/t/1358255.aspx) to validate on insert and update. In this post we are using the partial class and throwing a new exception.

    Currently these validations show in a validation summary and not in-line.

    The problem is that other validations applied via metadata on each column show up in-line.

    I want to have all validations shown inline, is there anyway to do this? because otherwise it is very untidy.

     

    Appreciate any feedback.

     

     


    Cheers.
    Mel
  • Re: In-line validation problem

    06-03-2009, 5:16 AM
    • Star
      12,328 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,567
    • TrustedFriends-MVPs

    Unless the validation is done at column level

    OnXXXChanging(string value)
    there is no way for the system to know which column the error is on and so is only shown at table level.
    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
  • Re: In-line validation problem

    06-03-2009, 4:38 PM
    • Member
      13 point Member
    • mdesoysa
    • Member since 12-03-2008, 9:04 PM
    • Posts 60

     I thought the only way to do it at column level is by applying attributes in metadata.cs ?

    But how can I entered values with values in the database when using attributes in metadata?

    Cheers.
    Mel
  • Re: In-line validation problem

    06-03-2009, 5:00 PM
    • Star
      12,328 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,567
    • TrustedFriends-MVPs

    For

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
  • Re: In-line validation problem

    06-03-2009, 6:29 PM
    • Member
      13 point Member
    • mdesoysa
    • Member since 12-03-2008, 9:04 PM
    • Posts 60

    Hi,

    Thanks for that it was what I was looking for.

    One small problem though - it works fine on inserting but on updating a field it validates even when I haven't changed anything or when I have changed one field but not the other it still gives an error for both fields.

    Does anybody know if there is another method similar to OnXXXChanging such as OnXXXUpdating ?

    My code from metadata.cs is below:

    [MetadataType(typeof(SupplierMetaData))]
        public partial class Supplier
        {
            partial void OnFCCSupplierIdentifierChanging(string value)
            {
                var Fcc = new ParentFccOnlineNettingDataContext();

                // FCCSupplierIdentifier
                var supplierid = Fcc.Suppliers.FirstOrDefault(c => c.FCCSupplierIdentifier ==  value);

              

                if (supplierid != null)
                {
                    throw new ValidationException(String.Format("The Supplier Id {0} already exists. Please specify a different identifier.", value));
                }
              
            }

            partial void OnSupplierNameChanging(string value)
            {
                var Fcc = new ParentFccOnlineNettingDataContext();

                // SupplierName
                var suppliername = Fcc.Suppliers.FirstOrDefault(c => c.SupplierName == value);
               
                if (suppliername != null)
                {
                    throw new ValidationException(String.Format("The Supplier Name {0} already exists. Please specify a different name.", value));
                }
            }

        }

    Cheers.
    Mel
  • Re: In-line validation problem

    06-04-2009, 3:00 AM
    Answer
    • Star
      12,328 point Star
    • sjnaughton
    • Member since 04-29-2008, 1:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,567
    • TrustedFriends-MVPs

    If you have a look at the Extensibility Method Definitions: 

        #region Extensibility Method Definitions
        partial void OnLoaded();
        partial void OnValidate(System.Data.Linq.ChangeAction action);
        partial void OnCreated();
        partial void OnCategoryIDChanging(int value);
        partial void OnCategoryIDChanged();
        partial void OnCategoryNameChanging(string value);
        partial void OnCategoryNameChanged();
        partial void OnDescriptionChanging(string value);
        partial void OnDescriptionChanged();
        partial void OnPictureChanging(System.Data.Linq.Binary value);
        partial void OnPictureChanged();
        #endregion  

    As you can see these are the only partial methods you can implements 

    CategoryName field of Northwind DB: 

    [Column(Storage="_CategoryName", DbType="NVarChar(15) NOT NULL", CanBeNull=false)]
    public string CategoryName
    {
    	get
    	{
    		return this._CategoryName;
    	}
    	set
    	{
    		if ((this._CategoryName != value))
    		{
    			this.OnCategoryNameChanging(value);
    			this.SendPropertyChanging();
    			this._CategoryName = value;
    			this.SendPropertyChanged("CategoryName");
    			this.OnCategoryNameChanged();
    		}
    	}
    }

    As you can see these partial methods are called every time the setter is called and of course because it's a web app the get called every time you edit or create an item.

    So the best approach is to check the value to see if it is the same as the property and then exit.

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
Page 1 of 1 (6 items)