Results 1 to 6 of 6

Thread: Linq To Sql Issues :(

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Linq To Sql Issues :(

    Ok, I'm using SQL Server Express 2008 and .Net 3.5 (c#)

    I have a couple of datetime fields in the database and am trying to make an edit to a row (Using Linq-to-Sql) I receive the error "Row not found or changed."

    I have spent some time getting the generated SQL and it seems that the issue is caused by the milliseconds attached to the datetime.

    Generated SQL that does not work,
    Code:
     @p5: Input DateTime (Size = 0; Prec = 0; Scale = 0) [20/10/2009 16:04:45]
     @p6: Input DateTime (Size = 0; Prec = 0; Scale = 0) [23/10/2009 10:15:36]
     @p7: Input DateTime (Size = 0; Prec = 0; Scale = 0) [23/10/2009 09:27:27]
    
    AND ([SignUpDate] = @p5) 
    AND ([LastActivityDate] = @p6) 
    AND ([LastLoginDate] = @p7)
    if i modify it myself like so it works,

    Code:
     @p5: Input DateTime (Size = 0; Prec = 0; Scale = 0) [20/10/2009 16:04:45.390]
     @p6: Input DateTime (Size = 0; Prec = 0; Scale = 0) [23/10/2009 10:15:36.733]
     @p7: Input DateTime (Size = 0; Prec = 0; Scale = 0) [23/10/2009 09:27:27.747]
    
    AND ([SignUpDate] = @p5) 
    AND ([LastActivityDate] = @p6) 
    AND ([LastLoginDate] = @p7)
    What are my options in ways to get arround this?

    Just to add this is my edit code,

    Code:
    var UserToEdit = this.GetUser(UserId);
    
    UserToEdit.Forename = Fields["Forename"];
    UserToEdit.Surname = Fields["Surname"];
    UserToEdit.DateOfBirth = Convert.ToDateTime(Fields["DateOfBirth"]);
    UserToEdit.DisplayName = Fields["DisplayName"];
    UserToEdit.TelephoneNumber = Fields["TelephoneNumber"];
    
    _db.SubmitChanges();

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Linq To Sql Issues :(

    Hey,

    What is the Data Type of that column in the Database?

    Gary

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Linq To Sql Issues :(

    DateTime

    See this link,

    http://stackoverflow.com/questions/8...und-or-changed

    > # High precision datetime fields are used. The solution is to set
    > UpdateCheck to never for that column
    > your DBML file

    This has resolved my issue but feel a bit like a hack.

    I'm leaving this open to see what others think.

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Linq To Sql Issues :(

    [20/10/2009 16:04:45] and [20/10/2009 16:04:45.390] are not equal dates. They differ by milliseconds. So when you do a compare ([SignUpDate] = @p5), you end up with no rows returned. So while comparing, you would either have to truncate the milliseconds part, or add the milliseconds part to the other so that they compare equal. I think the truncate approach would be easier than the add approach.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Linq To Sql Issues :(

    Quote Originally Posted by Pradeep1210 View Post
    [20/10/2009 16:04:45] and [20/10/2009 16:04:45.390] are not equal dates. They differ by milliseconds. So when you do a compare ([SignUpDate] = @p5), you end up with no rows returned. So while comparing, you would either have to truncate the milliseconds part, or add the milliseconds part to the other so that they compare equal. I think the truncate approach would be easier than the add approach.
    Yes exactly. However I cant edit the Generated SQL as i'm using the Generated Object and methods. I can only see the SQL because I have used the DatabseContexts Log property.

    Here is the actual code as in the first post,

    Code:
    var UserToEdit = this.GetUser(UserId);
    
    UserToEdit.Forename = Fields["Forename"];
    UserToEdit.Surname = Fields["Surname"];
    UserToEdit.DateOfBirth = Convert.ToDateTime(Fields["DateOfBirth"]);
    UserToEdit.DisplayName = Fields["DisplayName"];
    UserToEdit.TelephoneNumber = Fields["TelephoneNumber"];
    
    _db.SubmitChanges();
    Pino.

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Linq To Sql Issues :(

    Hey,

    Agreed, it seems like a bit of a hack, but I am not aware of anything else to get round it.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width