lundi 30 mars 2015

.net can't get datatable to update DB for a checkbox matrix

I am using MS Visual Studio 2013 to create an Employee Training Program for a client but cannot seem to figure one thing out...


The problem I have is writing any changes from a checkbox matrix back to the relevant table (VehicleTrainingMatrix)


Matrix summary: A vehicle manufacture has a range of vehicles and 3 departments of the build, Parts, Construction & Finishing. before the Matrix is displayed an Employee has been chosen in the program. Bascially each vehicle has a row showing the vehicle name then 3 checkboxes (1 per department) and if that Employee has been trained for a particular department for a particular vehicle you enter a Tick in the relevant place. Simple so I thought!?!?


The datatable was dragged from Data Sources onto the form.


It is based on the following SQL ...



SELECT V.Vehicle, VTM.PART AS Parts, VTM.CONST as Construction, VTM.FIN AS Finishing
FROM VehicleTrainingMatrix AS VTM RIGHT OUTER JOIN
Vehicles AS V ON VTM.VehicleID = V.ID AND VTM.EmployeeID = @IDparam
ORDER BY V.Vehicle


In my program I have the following line to load data:



Me.VehicleTrainingMatrixTableAdapter.FillByID(TrainingDBDataSet.VehicleTrainingMatrix, SelectedEmployeeID)


You should see the IDparam in the query is satisfied by the supplied SelectedEmployeeID variable.


VehicleTrainingMatrix table definition:



CREATE TABLE [dbo].[VehicleTrainingMatrix](
[ID] [int] IDENTITY(1,1) NOT NULL,
[EmployeeID] [int] NOT NULL,
[VehicleID] [int] NOT NULL,
[PART] [bit] NULL,
[CONST] [bit] NULL,
[FIN] [bit] NULL,
PRIMARY KEY CLUSTERED
([ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]


Basically any row with a Tick in the matrix relates to one row in the table. The row will have the relevant Employees ID and Vehicle ID stored with a "1" (tick) in the relevant field PART, CONST or FIN (or a combination, 1 row could have all 3 of these ticked).


Loading of data works. The problem I have is writing back to the DB any Vehicle Training Matrix changes to the VTM table.


I have tried ticking "Generate Insert, Update and Delete Statements" in Advanced Options of the Table Adapter definition, but this does not happen. When you go back into the definition they are greyed out again. I have looked into this and I believe (correct me if I am wrong) it is because I have more than 1 table listed in my select query. Another point is I do not really think I need the VehicleTrainingMatrix.ID field, I only added it when trying to get around this cannot write back to DB issue (I have been trying to sort this for a couple of days now) where one post seemed to indicate I needed a Primary Key to do it.


I have manually added the following into SQL



INSERT INTO [dbo].[VehicleTrainingMatrix]
([EmployeeID]
,[VehicleID]
,[PART]
,[CONST]
,[FIN])
VALUES
(621, 2, 1, NULL ,NULL)


621 = Employee ID,


2 = The Vehicles ID value


1 = That employee is trained in the Parts Department for Vehicle with ID 2


NULL = That employee is NOT trained in the Construction Department for Vehicle with ID 2


NULL = That employee is NOT trained in the Construction Department for Vehicle with ID 2


Hopefully the above has all the details required but to summarize The Datatable reads from the DB and populates the Matrix perfectly. My problem is how can I get any changes to the Matrix (new ticks only I will be disabling the ability to untick, not quite sure how yet though). A change could involve having to create a new row in Table VehicleTrainingMatrix if an unticked row has a tick put in it or just updating an existing row.


So you know my level I am just getting back into programming (after quite a few years just doing support) and have basically taught myself .net (please dont respond with C examples) and VS 2014. I have had a huge learning curve but was doing really well up to this problem. Certain terminology may be lost on me so I would appreciate explantions rather than possible a short to the point answer.


Many thanks in advance to anyone who takes the time to help.


Paul.


Aucun commentaire:

Enregistrer un commentaire