I'm trying to pull some data from a SQL table in my dataset using C#.
In this case I do not need all the columns just a few specific ones, however as I am not pulling back a column with a mandatory NOT NULL, the copy of the table is throwing the exception
"Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints."
I'm sure I can work around this by returning the unNullable column to my table however I want to avoid returning unnecessary data.
The query I am using which throws this exception is
SELECT DeviceSerialNumber, BuildID, LEMSCredentialsID, LEMSSoftwareID, OwnerID, RestagedDate
FROM tblDevice
WHERE (DeviceSerialNumber = @SerialNumber)
This excludes the mandatory column "tblLocationID". In reality though, this column is only mandatory when considering the database as a whole, not when I just need build and software detail for use in my form.
I am trying to use this query in the following manner.
private DataTable dtDevice;
dtDevice = taDevice.GetDataByDeviceSN_ForRestage(txtDeviceSerial.Text);
I notice when browsing the preview data, Visual Studio draws columns that are not specified in my SQL including the column tblLocationID it does not however populate these columns with data.
Is there anyway I can use this data in a temporary table without importing the non-nullable aspect of the column? preferably by not pulling through the non-selected columns at all?
For completeness, here's the definition (- minus foreign key definitions) of the source table:
CREATE TABLE [dbo].[tblDevice](
[DeviceSerialNumber] [nvarchar](50) NOT NULL,
[Model] [nvarchar](50) NULL,
[ManufactureDate] [smalldatetime] NULL,
[CleanBootDate] [smalldatetime] NULL,
[BuildID] [int] NULL,
[Notes] [nvarchar](3000) NULL,
[AuditID] [int] NULL,
[LocationID] [int] NOT NULL,
[SimID] [int] NULL,
[LEMSCredentialsID] [int] NULL,
[LEMSSoftwareID] [int] NULL,
[OwnerID] [int] NULL,
[RestagedDate] [smalldatetime] NULL,
[Boxed] [bit] NULL,
CONSTRAINT [PK_tblDevice_1] PRIMARY KEY CLUSTERED
([DeviceSerialNumber] ASC) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]