1

Got a SQL Server column

   datetime, Not-Null, defaults to Getdate()

in the Linq-to-SQL (.dbml) model.

For the property in the table

    Autogenerated Value is true
    and auto-sync: OnInsert()

On insert in C#, the default value (current date) is automatically inserted in the table(C# no values are passed)

While updating, if I update the value to current date it doesn't update.

However if I change the

    Autogenerated Value is false
    and auto-sync: never

Update works (but i need a pass a value through c#)but insert fails with SQL Server overflow error because no values are passed from clientside c# to sql server

Is there any easy way to update the column value with default value?

The only solution I know is to keep the property

    Autogenerated Value is false
    and auto-sync: never

and to pass the current date to SQL Server on Insert? I'm currently using so many default values in SQL Server. I don't want to pass default values on insert from server side.

What I want to achieve is send no values on inserting, so that sql does all the job(default value updating) and if I pass different value in update via c# it should update the database(which is not working at the moment)? If you didn't understand the question, please let me know?

Please check the comments if you didn't understand the question?

Binny
  • 328
  • 5
  • 21
  • I think you want a timestamp column: https://technet.microsoft.com/en-us/library/ms182776%28v=sql.110%29.aspx – DrewJordan Apr 24 '15 at 16:51
  • 1
    The best solution is to use the *default* value for inserts (you don't provide a value - the default gets set), and then either set the new value for an update in your client-side C# code, or if you don't want to do this, you'll need to use a `AFTER UPDATE` trigger on that table to set the DateTime column to the current date&time again$ – marc_s Apr 24 '15 at 17:18
  • @marc_s I gave a example here, in my current I'm using so many default values in my sql column. Insert works fine in c#( with `Autogenerated Value is true and and auto-sync: Always)` , update doesn't work at all. To update date, I'm using trigger only but let say it I use a default value for datatype Boolean or integer (update doesn't work at all).It updates with default value only. I'll give u example if u wanted to – Binny Apr 29 '15 at 08:39
  • @DrewJordan date datatype was an example, I'm using other datatypes with default values like `integer,Boolean etc`. I would like to have linq to insert default values on insert automatically and also update the value which I provide during update, hope you understand? – Binny Apr 29 '15 at 08:54
  • 2
    Gotcha. It looks like L2S doesn't support this; you can implement an `Insert` method for the table in question according to this: http://stackoverflow.com/questions/2555206/can-linq-to-sql-omit-unspecified-columns-on-insert-so-a-database-default-value-i – DrewJordan Apr 29 '15 at 12:42
  • @DrewJordan thanks for the link, so there is no easy fix. the solution which he gave was 5year old. Anyway thanks mate – Binny Apr 29 '15 at 13:58
  • 1
    yeah... It's probably too late for this project, but Entity Framework can handle this scenario with a default value that doesn't have to always be auto-generated, if you get the opportunity to use it. – DrewJordan Apr 29 '15 at 14:01
  • 1
    @DrewJordan we dont have the time to use entity framework :( Thanks for your suggestion mate. If you find a fix anytime , please update. – Binny Apr 29 '15 at 14:09

0 Answers0