0

I have a Table with GUID date type field as Primery Key. when I run the query in DataSet window of ASP.net, the result is OK, but when I use it in ASP.Net Page it will return an error page like below :

Failed to enable constraints. One or more rows contain values violating non-null,unique, or foreign-key constraints. 

The query has not any type of join, and a simple SUM query that add up daily work amounts in a filed based on date range which pass as a parameter to the query and Group by activities that exist in another filed.

This is the table :


CREATE TABLE [dbo].[DailyReport] (
    [ReportDate]       DATETIME         NULL,
    [ReportId]         UNIQUEIDENTIFIER NOT NULL,
    [ConstructionType] NVARCHAR (50)    NULL,
    [Zone]             NVARCHAR (50)    NULL,
    [BuildingName]     NVARCHAR (50)    NULL,
    [ActivityId]       INT              NULL,
    [TodayWork]        REAL             NULL,
    [Decription]       NTEXT            NULL,
    PRIMARY KEY CLUSTERED ([ReportId] ASC)
);

And this is the query :
 

SELECT SUM(TodayWork) AS SumWork, ActivityId, COUNT(ReportId) AS RecordCount
FROM   DailyReport
WHERE  (ConstructionType = N'1') AND (ReportDate >= @DateStart) AND 
       (ReportDate <= @DateFinish)
GROUP BY ActivityId

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • Possible duplicate of http://stackoverflow.com/questions/7026566/failed-to-enable-constraints-one-or-more-rows-contain-values-violating-non-null#7029713 – Rob Johnston May 29 '15 at 11:46
  • 1
    @RobJohnston if that's indeed a duplicate, it means the OP is trying to load data into a DataTable with incompatible constraints. We *really* need to see the code, especially if there are any non-nullable DataColumn fields in the datatable, or if there are FK constraints – Panagiotis Kanavos May 29 '15 at 11:51
  • I think, the problem is related to GUID Filed... my problems starts after I use GUID instead of int primery key... – Mohammad Khandordi May 29 '15 at 12:03

1 Answers1

-2

Normally in c# int does not allow null values but that can do in sqlserver, that mean null values can be allowed by sql server but not in c#.

so it can be solved in 2 ways 1) either use allow null to false in database 2) or use nullable int type in c# code by using like "int? a=null"

Midhun
  • 23
  • 7
  • The OP is reading from the database, not writing to it. A @RobJohnston commented, the OP most likely is trying to load data into a DataTable with incompatible constraints – Panagiotis Kanavos May 29 '15 at 11:52