Note that there are some explanatory texts on larger screens.

plurals
  1. POPass a NULL value to DateTime Field in LINQ
    primarykey
    data
    text
    <p>My database table is like this</p> <pre><code>CREATE TABLE MYBUDGET.tbl_CurrentProperty ( [PropID] INT NOT NULL IDENTITY(1,1), [UpdatedOn] DATETIME NOT NULL, [Amount] MONEY NOT NULL, [Remarks] VARCHAR(100) NOT NULL, ) ALTER TABLE MYBUDGET.tbl_CurrentProperty ADD CONSTRAINT PK_CurrentProperty_PropID PRIMARY KEY ([PropID]) ALTER TABLE MYBUDGET.tbl_CurrentProperty ADD CONSTRAINT DF_CurrentProperty_UpdatedOn DEFAULT (DATEADD(MINUTE,30,DATEADD(HOUR, 5, GETUTCDATE()))) FOR [UpdatedOn] ALTER TABLE MYBUDGET.tbl_CurrentProperty ADD CONSTRAINT CK_CurrentProperty_Amount CHECK([Amount] &gt; -1) GO </code></pre> <p>I'm using LINQ to SQL. In C# I need to pass only [Amount] and [Remarks] fields and other fields must be used with their default values ([PropID] and [UpdatedOn]).</p> <p>In C# I create tbl_CurrentProperties object like below,</p> <pre><code>tbl_CurrentProperties currentProperties = new tbl_CurrentProperties(); currentProperties.Amount = 50.00M; currentProperties.Remarks = "remarks"; </code></pre> <p>and then submit the object to the data context. But here, Linq assigned <code>'1/1/0001 12:00:00 AM'</code> for UpdatedOn field. But this violate the SQL datetime range <code>1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM</code> and Occurring an exception. Also I can't assign a NULL value manually for a <code>DateTime</code> field since its a not nullable type. Anyhow I need to make this to use its DEFAULT Constraint. How do I do this?</p> <hr> <p>PS: I want to use it like this because, My database is Online and Users are in different locations. So If I used DateTime.Now, the time in the user machine may be wrong, and It insert a wrong value into DB. I need to use SQL server time always.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload