locked
Insert Generated Values RRS feed

  • Question

  • User-1254308814 posted

    I have a keyword and sequence column which are both unique columsn my project table in the database. I want to them to be genereted on the server when a user clicks to insert a record. I added insertvisible=false on the insert form to the fields. When I add an event handler to the oninserting method to do the value generation but the client-side stops it from getting that far.

      List of validation errors

    • The Keyword field is required.
    • The Sequence field is required.

    Is there a better way to approach what I am trying to do. It gets ugly if I use partial class project.cs to do this validation because there doesn't seem to be an easy way to discern what event is the current object(ie. whether I am in insert, list or edit modes.), plus having a select statement there seems like we are getting away from software architecture.

    Tuesday, October 21, 2008 9:24 PM

Answers

  • User-1005219520 posted

    For the EDM, you need to set StoreGeneratedPattern="Computed" in the SSDL content.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 22, 2008 10:39 PM

All replies

  • User-330204900 posted

    Hi Jwize, are you using Linq to SQL or Entity Framework?

    Wednesday, October 22, 2008 3:22 AM
  • User-1254308814 posted

     I am using Entities framwork.

    Wednesday, October 22, 2008 4:33 AM
  • User-1005219520 posted

    For the EDM, you need to set StoreGeneratedPattern="Computed" in the SSDL content.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 22, 2008 10:39 PM
  • User-1254308814 posted

    Would that not indicate that the sql code is going to insert the code? In fact the server-side code will be doing the formatting. I will go ahead and give it a try anyways. Thanks in advance if it works.

    Thursday, October 23, 2008 5:37 PM
  • User-1005219520 posted

    I want to them to be genereted on the server when a user clicks to insert a record.

    Would that not indicate that the sql code is going to insert the code? In fact the server-side code will be doing the formatting. I will go ahead and give it a try anyways. Thanks in advance if it works.

    It will do what you requested, they will be generated on the server.

    Thursday, October 23, 2008 5:54 PM
  • User-1254308814 posted

    Even with the scaffoldcolumn set to false for ID and Keyword columns and the ID set to StoreGeneratedPattern="Identity" the Keyword column set to  StoreGeneratedPattern="Computed" in  the SSDL I still get the following errors when I try to insert a new field.

    List of validation errors

    • The ID field is required.
    • The Keyword field is required.

    Therefore my oninserting code still doesn' have a chance to generate the values neccessary to my needs. I am still stuck. If there is something that I seem to be missing please let me know. I have only been working with this stuff for a couple days so I have much to learn.

    Thursday, October 23, 2008 10:42 PM
  • User-1005219520 posted

    The client side validator doesn't know these are server generated. You need to add [ScaffoldColumn(false)]  to the partial class for the entity. That will hide them from the validator. You could also edit the SSDL to indicate they are not required, but [ScaffoldColumn(false)]  is probably a better approach.

    Friday, October 24, 2008 12:30 AM
  • User-1254308814 posted

    Due to my detailed post being deleted by an unexpected error in the add code [:(] window the code works as follows. Bottom line don't put a StoreGeneratedPattern="Computed" unless your sql server is really going to make it happen. Your changes in the oninserting event won't be recognized.   

     

      protected void DetailsDataSource_Inserting(object sender, EntityDataSourceChangingEventArgs e)
        {
            ((cbadataModel.People)e.Entity).Keyword = "Generated Input";
        }

        public class PeopleMetadata
        {

            [ScaffoldColumn(false)]
            public Object ID { get; set; }

            [ScaffoldColumn(false)]
            public Object Keyword { get; set; }
        }

            <EntityType Name="People">
              <Key>
                <PropertyRef Name="ID" />
              </Key>
              <Property Name="ID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
              <Property Name="Name" Type="nvarchar" Nullable="false" MaxLength="256" />
              <Property Name="Title" Type="nvarchar" MaxLength="256" />
              <Property Name="Bio" Type="ntext" />
              <Property Name="Photo" Type="nvarchar" Nullable="false" MaxLength="256" />
              <Property Name="IsActive" Type="bit" Nullable="false" />
              <Property Name="Keyword" Type="nvarchar" Nullable="false" MaxLength="256" <STRIKE>StoreGeneratedPattern="Computed"</STRIKE> />
              <Property Name="IsDefault" Type="bit" Nullable="false" />
            </EntityType>

    Friday, October 24, 2008 2:25 PM