1

I created a stored procedure in SQL Server 2012

ALTER proc [dbo].[select_alltypes]
    @cdin_startunstufdate1 DateTime = null,
    @cdin_startunstufdate2 DateTime = null
AS
BEGIN
    SET NOCOUNT ON;

    SELECT
        0 as shipid, 
        i.cdin_cdindexid, p.pinv_PerformaInvID,
        coalesce(i.cdin_serial, 0) as depno,
        coalesce(convert(datetime, left(convert(nvarchar, i.cdin_startunstufdate, 120), 10), 120),'-') as deidate,
        coalesce(i.cdin_goodsDesc, '-') as gooddesc,
        coalesce(i.cdin_Customdeclar, '-') as custdec,
        coalesce(i.cdin_NoofPackages, 0) as pkg,
        coalesce(i.cdin_WT, 0) as wt, 
        coalesce(i.cdin_volumewt, 0) as vwt,
        coalesce(i.cdin_MortgageAmount, 0) as lcamt,
        coalesce(p.pinv_name, '-') as invno,
        coalesce(p.pinv_TotalAmount, 0) as invamt,
        p.pinv_Status, p.pinv_InvoiceProperty as prop,
        coalesce(c.comp_name, '-') as custname,
        coalesce(Comp_CompanyId, '-') as custid, 
        coalesce(c.comp_idcust, '-') as accpacno,
        coalesce(t.Terr_Caption, '-') as Terr,
        convert(nvarchar, '01', 2) as type     
    from 
        cdindex i 
    inner join   
        company c on i.cdin_CompanyId = c.Comp_CompanyId  
    inner join 
        Territories t on i.cdin_Secterr = t.Terr_TerritoryID 
    left outer join 
        PerformaInv p on i.cdin_cdindexid = p.pinv_CDIndexId 
    where
        (cdin_deleted Is null And c.comp_deleted Is null 
         And t.Terr_Deleted Is null And p.pinv_deleted Is null)
        and cdin_startunstufdate between @cdin_startunstufdate1 and @cdin_startunstufdate2
        and (p.pinv_status in ('Draft', 'Posted') or pinv_status is null) 
        and (p.pinv_InvoiceProperty = '01' or p.pinv_InvoiceProperty is null )
end

I want to call this proc using EF like this:

var sp = db.select_alltypes(new DateTime (2016-07-01), new DateTime (2016-07-28)).ToList();

Every time I call it with passing parameters, the debugger shows a runtime error.

But if I call it without parameters, with altering the stored procedure to be without parameters and give it specified date:

var sp = db.select_alltypes().ToList();

It returns the expected parameter.

The error is:

Additional information: An error occurred while executing the command definition. See the inner exception for details.

Error converting data type datetime2 to datetime

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Fares Ayyad
  • 383
  • 1
  • 3
  • 18

2 Answers2

2

EF doesn't treat all datetimes as DATETIME2, it treats all out of bounds or uninitialized dates as datetime2. From what I understand using Nullable<DateTime> to represent uninitialized datetimes instead of DateTime should solve your problem.

gilmishal
  • 1,884
  • 1
  • 22
  • 37
0

This is the OP's Feedback.

No need to do any datatype change on the SQL table and Just 2 steps as below.

step 1 :

Change the datatype of the parameter on the SP to be like this:

@cdin_startunstufdate1 DateTime2=null, @cdin_startunstufdate2 DateTime2=null

Step 2 :

Sp call should be like this.

var sp = db.select_alltypes(DateTime.Parse("2016-07-01"),DateTime.Par‌​se("2016-
07-28")).To‌​List();

Original Answer :

Entity framework handles all the dates as a Datetime2,

Problem :

If your fields in the database are Datetime,then that can be a problem.

Solution :

You have to change the cdindex table's cdin_startunstufdate column date field datatype to datetime2.

How to change the datetime to dDatetime2

Just go to your SQL table's datetime column and change it as shown below.

enter image description here

Update :

Sp call should be like this.

var sp = db.select_alltypes(DateTime.Parse("2016-07-01"),DateTime.Par‌​se("2016-
07-28")).To‌​List();
Sampath
  • 63,341
  • 64
  • 307
  • 441
  • Is there another solution, – Fares Ayyad Aug 28 '16 at 07:28
  • it gives me error while executing it on sql `Msg 5074, Level 16, State 1, Line 1 The index '_dta_index_Activity_7_2021582240__K11_9_10' is dependent on column 'acty_Login'. Msg 4922, Level 16, State 9, Line 1 ALTER TABLE ALTER COLUMN acty_Login failed because one or more objects access this column. ` – Fares Ayyad Aug 28 '16 at 07:43
  • I don't know what is the conversion to DateTime2 but i'm at work so what if someone wants to work on database using SQL server is there any issue to CRUD the Datetime column in any table ? – Fares Ayyad Aug 28 '16 at 07:49
  • Sorry,I didn't get your point ? – Sampath Aug 28 '16 at 07:50
  • Also After closing visual studio the same error occur. – Fares Ayyad Aug 28 '16 at 07:53
  • That script won't work hence FK references. I have removed that.You have to do it manually. – Sampath Aug 28 '16 at 08:09
  • how to change datetime to datetime2 manually. – Fares Ayyad Aug 28 '16 at 08:13
  • i know that i asked you to help me many times, but really there's too much tables to change it's columns to DateTime2 so i noticed that there is another solution which is to convert it to nullable, do you know how to do this, i want a solution without modifying database. – Fares Ayyad Aug 28 '16 at 08:34
  • You don't need to change all the tables.Just `cdindex` table's `cdin_startunstufdate` column.That is it. – Sampath Aug 28 '16 at 08:59
  • when i try to edit the datatype it gives me the following error: `Saving Changes isn't permitted` – Fares Ayyad Aug 28 '16 at 09:11
  • you need to run the management studio as `Administrator` permission. – Sampath Aug 28 '16 at 09:14
  • i already run with administrator, if you understand how to solve it using nullable method? i want a solution without modifying database. – Fares Ayyad Aug 28 '16 at 09:19
  • Ok,then see this :https://support.microsoft.com/en-us/kb/956176 – Sampath Aug 28 '16 at 09:21
  • you cannot use other solution hence database first. – Sampath Aug 28 '16 at 09:23
  • Ok, sir be patient i convert the datatype to Datetime2 now i want to pass the parameters, how to do this ? – Fares Ayyad Aug 28 '16 at 09:42
  • what happened when you run your SP again ? Any errors again ? – Sampath Aug 28 '16 at 09:52
  • Error converting data type datetime2 to datetime. now the error is that the datetime2 can't be converted to datetime!? – Fares Ayyad Aug 28 '16 at 09:54
  • please do this `@cdin_startunstufdate1 datetime2 = null, @cdin_startunstufdate2 datetime2 = null` on your SP. – Sampath Aug 28 '16 at 09:56
  • it gives me no error at run time but no output appeared. – Fares Ayyad Aug 28 '16 at 10:10
  • it looks like the parameter isn't taken or something – Fares Ayyad Aug 28 '16 at 10:12
  • can you run your SP directly on the SQL and see what is the output ? – Sampath Aug 28 '16 at 10:16
  • it gives me the expected result which is a list of data from the joined tables , note that i used the following format to pass the parameters: `exec select_alltypes '2016-07-01','2016-07-28'` , in VS i used to call the proc in this way: `var sp = db.select_alltypes(new DateTime(2016-07-01),new DateTime(2016-07-28)).ToList();` – Fares Ayyad Aug 28 '16 at 10:21
  • can you try like this `var sp = db.select_alltypes("2016-07-01","2016-07-28").ToList();` – Sampath Aug 28 '16 at 10:45
  • sorry for delay, it gives me the error : cannot convert string to DateTime? – Fares Ayyad Aug 28 '16 at 11:21
  • I found it : ` var sp = db.select_alltypes(DateTime.Parse("2016-07-01"),DateTime.Parse("2016-07-28")).ToList();` Thank you very much for help. @Sampath – Fares Ayyad Aug 28 '16 at 11:34
  • Well done.I'll update it :) – Sampath Aug 28 '16 at 11:36
  • :) thxxxxxxxxxxxx – Fares Ayyad Aug 28 '16 at 11:45
  • Oh, notice that it doesn't matter to change the datatype in Table=>right click=>Design. but you must change the datatype for parameter in Stored Procedure. – Fares Ayyad Aug 28 '16 at 11:45
  • Sorry..Can you explain it bit.may be a code snippet ...Then I can update my answer.Hope it'll help someone else in the future. – Sampath Aug 28 '16 at 11:52
  • Ok, in our attempts to solve the problem you mentioned that i should change the datatype of the Column: **cdin_startunstufdate** from table name(which is cdindex) Design, to be DateTime2. but the following step only needed to solve the problem even if you don't change the datatype of the column in a table. **This is the Basic Step** change the datatype of the parameter procedure to be like this: `@cdin_startunstufdate1 DateTime2=null, @cdin_startunstufdate2 DateTime2=null`. and the second step: `DateTime.Parse("2016-07-01"),DateTime.Parse("2016-07-28")` – Fares Ayyad Aug 28 '16 at 12:12
  • Aha..Is that mean no need to change the table column type ? did you revert it to `datetime` again and tested ? but above your `sp` says `DateTime2=null`.Then why do we need that ? – Sampath Aug 28 '16 at 12:15
  • i don't know how to explain it clearly but if there's any question please mention. i will explain again: to solve the problem there's two steps to be considered: **firstly** change the datatype of the parameter procedure to be like this: `@cdin_startunstufdate1 DateTime2=null, @cdin_startunstufdate2 DateTime2=null` **Second** `DateTime.Parse("2016-07-01"),DateTime.Parse("2016-07-28")` – Fares Ayyad Aug 28 '16 at 12:17
  • yes that what i'm trying to say: once you changed the datatype of the parameter in the proc no need to change it in the table column. you only need to change the datatype to `DateTiome2=null` in the proc – Fares Ayyad Aug 28 '16 at 12:17
  • One question. Did you revert the SQL table column name back to `datetime` and tested again ? – Sampath Aug 28 '16 at 12:19
  • And i revert the column type to DateTime then i test it the result is Worked perfectly. – Fares Ayyad Aug 28 '16 at 12:20
  • Aha..OK. I'll put your feedback also as 2nd option.Thanks for the feedback. – Sampath Aug 28 '16 at 12:21
  • Yes i revert the sql table, notice that for verifying purpose in my model.edmx only retrieve the stored procedures not the views or the Tables. – Fares Ayyad Aug 28 '16 at 12:22
  • 1
    ur welcome, thanks for you. – Fares Ayyad Aug 28 '16 at 12:23