locked
DynamicData Instert is running wrong?? RRS feed

  • Question

  • User116907111 posted

    Hello,

    Problem 1:

     I have the following Problem. If I make an Insert with a field that the user can't change and put the value "1" into it and the user makes an instert. The field that is set to "1" is not "1" in DataBase. If I make a field that the user can change and the user insert into this field the value "1" the field is in DataBase "1", too. My Question: WHY?!

    Problem 2: 

    I gave the user 4 fields that he can edit. One field in the database (a 5th Field is an Data Field thats called RowInserted. This field has the standard method "(getdate())". If I send an insert on the Database the field gets the correct value. If the user fill the fields and post it. The Field RowInserted is empty. Why doesn't work it?

    I hope somebody can help me out. thx
    BK

    Monday, September 8, 2008 1:24 PM

Answers

  • User-330204900 posted

    I think I was doing something like you are here but took a different approch and put the code into business logic (Linq to SQL) and here'smy code: 

    public partial class NorthwindDataContext : System.Data.Linq.DataContext
    {
    	partial void InsertCustomer(Customer instance)
    	{
    		var user = HttpContext.Current.User;
    		instance.CreatedBy = user.Identity.Name;
    		instance.CreatedOn = DateTime.Now;
    		instance.UpdatedBy = user.Identity.Name;
    		instance.UpdatedOn = DateTime.Now;
    
    		// finally send this to the DB
    		this.ExecuteDynamicInsert(instance);
    	}
    
    	partial void UpdateCustomer(Customer instance)
    	{
    		var user = HttpContext.Current.User;
    		instance.UpdatedBy = user.Identity.Name;
    		instance.UpdatedOn = DateTime.Now;
    
    		// finally send this to the DB
    		this.ExecuteDynamicUpdate(instance);
    	}
    }
    Here when a new record is created I add the relavent data to mark who created the record and when and when Updating a record I do the same but only to the UpdateXX fields thus having a record of who created and who last updated any given record. Note I marked all the columns I used here with a [ScaffoldColumn(false)] attribute so the would not show.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 9, 2008 4:42 AM

All replies

  • User-797310475 posted

    Problem 1:

     I have the following Problem. If I make an Insert with a field that the user can't change and put the value "1" into it and the user makes an instert. The field that is set to "1" is not "1" in DataBase. If I make a field that the user can change and the user insert into this field the value "1" the field is in DataBase "1", too. My Question: WHY?!

    How are you "putting" the value 1 into it? Is the field supposed to be autogenerated in the database? Could you provide some more details?

    Problem 2: 

    I gave the user 4 fields that he can edit. One field in the database (a 5th Field is an Data Field thats called RowInserted. This field has the standard method "(getdate())". If I send an insert on the Database the field gets the correct value. If the user fill the fields and post it. The Field RowInserted is empty. Why doesn't work it?

    Once again I'm not sure I understand how your DB is set up and what you are trying to achieve. Could you elaborate?

    Monday, September 8, 2008 3:15 PM
  • User116907111 posted

    To Problem 1:

    I put the value "1" in by setting it like that: DetailsView1.Rows[0].Cells[1].Text = UserID.ToString();
    This shows the correct UserId that I want to insert. After the Login the UserID is saved in a Session and I put the UserID from the Session into a Variable and this value into the DetailsView.

    Problem 2:
    The Field RowInserted is a field which shows a TimeStamp where a user has created something. There is a Form where a User can tell what he have done in his life.

    Like this: Beginn: 1/1/99 End: 1/1/05 Description: Job in Supermarket
    Than the User can save this entry. The field RowInserted is not on this very form. This field is an "getDate()" Field in Database. This TimeStamp must generated from the Database. I think that the form sends an empty value to the Database and overwrites the default entry. But where can I find it?

    I hope that it is now better to understand.

    Tuesday, September 9, 2008 4:05 AM
  • User-330204900 posted

    I think I was doing something like you are here but took a different approch and put the code into business logic (Linq to SQL) and here'smy code: 

    public partial class NorthwindDataContext : System.Data.Linq.DataContext
    {
    	partial void InsertCustomer(Customer instance)
    	{
    		var user = HttpContext.Current.User;
    		instance.CreatedBy = user.Identity.Name;
    		instance.CreatedOn = DateTime.Now;
    		instance.UpdatedBy = user.Identity.Name;
    		instance.UpdatedOn = DateTime.Now;
    
    		// finally send this to the DB
    		this.ExecuteDynamicInsert(instance);
    	}
    
    	partial void UpdateCustomer(Customer instance)
    	{
    		var user = HttpContext.Current.User;
    		instance.UpdatedBy = user.Identity.Name;
    		instance.UpdatedOn = DateTime.Now;
    
    		// finally send this to the DB
    		this.ExecuteDynamicUpdate(instance);
    	}
    }
    Here when a new record is created I add the relavent data to mark who created the record and when and when Updating a record I do the same but only to the UpdateXX fields thus having a record of who created and who last updated any given record. Note I marked all the columns I used here with a [ScaffoldColumn(false)] attribute so the would not show.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 9, 2008 4:42 AM
  • User116907111 posted

    Hey sjnaughton,

    that sounds interesting. I not understand how you can add a new InsertCustomer(Costumer instance) in that logic. In my code is the warning message, that I should not modifie this code, because an rebulid makes it undo. I have no Insert Event.

    I have only these:

    #region Extensibility Method Definitions
    partial void OnLoaded();
    partial void OnValidate(System.Data.Linq.ChangeAction action);
    partial void OnCreated();
    partial void OnCustomerIDChanging(string value);
    partial void OnCustomerIDChanged();
    partial void OnCompanyNameChanging(string value);
    partial void OnCompanyNameChanged();
    partial void OnContactNameChanging(string value);
    partial void OnContactNameChanged();
    partial void OnContactTitleChanging(string value);
    partial void OnContactTitleChanged();
    partial void OnAddressChanging(string value);
    partial void OnAddressChanged();
    partial void OnCityChanging(string value);
    partial void OnCityChanged();
    partial void OnRegionChanging(string value);
    partial void OnRegionChanged();
    partial void OnPostalCodeChanging(string value);
    partial void OnPostalCodeChanged();
    partial void OnCountryChanging(string value);
    partial void OnCountryChanged();
    partial void OnPhoneChanging(string value);
    partial void OnPhoneChanged();
    partial void OnFaxChanging(string value);
    partial void OnFaxChanged();
    #endregion

    Tuesday, September 9, 2008 5:58 AM
  • User-330204900 posted

    You create a sperate class file (Partial Classes) with the code I showed in and then implement the C# 3.0 partial methods.

    Hope this helps [:D]

    Tuesday, September 9, 2008 6:18 AM
  • User116907111 posted

    Maybe it helps. I have made a Class named "Partials" and if I create your code it is:

    [MetadataType(typeof(Job_MD))]
    public partial class Job
    {
    partial void InsertBerufserfahrung(Job instance)
    {
        instance._IKKey = IK_Key;
        instance._LetzteAenderung =
    DateTime.Now;
        instance._RowInserted=
    DateTime.Now;
        // finally send this to the DB

        this.ExecuteDynamicInsert(instance);

    }

    private void ExecuteDynamicInsert(Job instance)
    {
          throw new NotImplementedException();
    }

     

    What must I do with the new ExecuteDynamicInsert Event? I that the correct way?

     

    Tuesday, September 9, 2008 8:57 AM
  • User-330204900 posted

    You don't implement ExecuteDynamicInsert it is handled by Linq to SQL.

    Tuesday, September 9, 2008 9:09 AM
  • User116907111 posted

    If I don't implement it, I get an Error-Message. (unhandeld code)

    I make the following steps:

    1. Create new Class called "Partials.cs"
    2. Add the code that I showed

    Can't compile that. The Method ExecuteDynamicInsert is unknown.

    Could you please give me an guid to make it work, plz?

    Tuesday, September 9, 2008 9:35 AM
  • User-330204900 posted

    OK What are you using EntityFramwork or Linq to SQL?

    Tuesday, September 9, 2008 9:48 AM
  • User116907111 posted

    I make a DynamicData Page like the Tutorial of David Ebbo (http://sessions.visitmix.com/?selectedSearch=T24)

    That is LinqToSQL

    Tuesday, September 9, 2008 10:20 AM
  • User188291263 posted

     Maybe you getting error because u didnt implement MetadataType?
    try to remove that attribute and remove your private ExecuteDynamicInsert

    Tuesday, September 9, 2008 11:44 AM
  • User116907111 posted

     Could you please explain in detail what you mean?
    If I remove my private ExecuteDynamicInsert than Visual Studio offers me to generate it for me.

    I have made the mistake, that I forgot the firste code line. (see maked as answer post) THX

    Tuesday, September 9, 2008 2:22 PM
  • User-330204900 posted

    Are you running the RTM of Visual Studio 2008 SP1? if not you need to get it.

    I say this because erros you are getting don't make sense the ExecuteDynamicInsert(); method should just workits not a partial method...

    Wednesday, September 10, 2008 1:40 PM