Using AccessDataSource and ListView when I hit del in browser following error is shown
OleDbException (0x80004005)
: The record cannot be deleted or changed because table 'tblOrders' includes related records.
I am trying to solve the problem through a couple of ways which are as follows
DeleteCommand="DELETE FROM [tblCustomers] , [tblOrders] WHERE [pkeyCustomerID] = ? "
The Default command generated by ASP is
DeleteCommand="DELETE FROM [tblCustomers] WHERE (([pkeyCustomerID] = ?) OR ([pkeyCustomerID] IS NULL AND ? IS NULL)) "
Or
I removed <asp:Parameter Name="pkeyCustomerID" Type="String" /> from <DeleteParameters>
and replace it with the parameters of selected table
so the foreign key issue isnt affected
tag now there are no errors but the record isnt deleted either
How do i get around this?
Your first query:
DELETE FROM [tblCustomers] , [tblOrders] WHERE [pkeyCustomerID] = ?
Is not valid syntax, you cannot delete from two tables simulatenously, you would need something like
DELETE FROM [tblOrders] WHERE [pkeyCustomerID] = ?;
DELETE FROM [tblCustomers] WHERE [pkeyCustomerID] = ?;
However I don't think Access supports multiple statements. The best solution would be to edit this relationship to use the cascade delete referential action trigger.
To do this go into the relationships in Access:
Then double click on the relationship between tblOrders and tblCustomers which will bring up the properties. Then either uncheck "Enforce Referential Integrity", or ensure that the cascade referential action options are checked (ignore the field names, it was the first relation I came across in a test database I have):
This will ensure that when you delete a customer, you delete all related orders too.
Related
I have a database with a 1..many relationship between two tables, call them Color and Car. A Color is associated 1..many with Cars. In my case, it's critical that Colors can be deleted any time. No cascade delete, so if a Color is deleted, the Car's Color_ID field points to something that doesn't exist. This is OK. They are related via a FK named Color_ID.
The problem comes in when I do this:
var query = context.Cars.Include(x => x.Colors);
This only returns Cars that have an associated Color record that exists. What I really want is ALL the Cars, even if their color doesn't exist, so I can do model binding with a GridView, i.e.
<asp:Label runat="server" Text='<%# Item.Colors == null ? "Color Deleted!" : Item.Colors %>' />
All of this works fine if I remove the .Include() and resort to lazy loading. Then Item.Car.Color is null. Perfect. However I'm seriously concerned about doing way too many database queries for a massive result set, which is certainly possible.
One solution to avoid excessive db queries is to return an anonymous type from the datasource query with all the specific related bits of info that I need for the grid and convert all my "Item" style bindings to good 'ol Eval(). But then I lose the strong typing, and the simplicity that Value Provider attributes bring. I'd hate to re-write all that.
Am I right, do I have to choose one or the other? How can I shape my query to return all the Car records, even if there is no Color record? I think I'm screwed if I try to eager load with .Include(). I need like a .IncludeWithNulls() or something.
UPDATE: Just thought of this. I don't know how ugly this is as far as query cost, but it works. Is there a better way??
var query = context.Cars.Include(x => x.Colors);
var query2 = context.Cars.Where(x => !context.Colors.Any(y => y.Color_ID == x.Color_ID);
return query.Union(query2);
The problem was an incorrect end multiplicity. What I really needed was not 1..many but 0..many. That way, Entity Framework generates a left outer join instead of an inner join from the .Include(). Which makes sense, there may be zero actual Color records in the example above. The thing that confused me was that in the SQL database, I never set those foreign key fields to nullable because at the time of creation, they always required a valid foreign key. So I set them to nullable and fixed up my .edmx table and everything is working. I did have to add a few more null checks here and there such as the one in my question above, that weren't strictly necessary before, since the .Include is now pulling in records that reference missing related entities, but no big deal.
So I lose out on the non-null checking at the db level, but I gain some consistent logic in my LINQ queries for how those tables actually relate and what I expect to get back.
Ok I don't know if my question makes sense but I'll try to describe this as best as I can.
I am doing a school project and I have a database which stores "Projects", fields include ProjectID, Name, Publications, Status and so on.
I also have another table called Publications, which stores ID, PublicationType, ProjectID, Description and so on.
On the project page, I have the option to add a new project, and it also shows a grid view of the Current projects, and if you click on their name, it takes you to a page where you can add Publications for that project. I pass the Value of ProjectID through a HyperLinkField with the value Publications.aspx?ProjectID={0}
The ProjectID appears in the URL when I'm redirected to the Publications page, however, when I try to insert My publications, Name, Type and ProjectID the ProjectID field is left empty in my database.
I'm doing this in design view but here are my Insert and Select statements for the Publications page.
SELECT ProjectID, PubType, PubDescription, PubDetail, PubLink FROM Publications WHERE (ProjectID = ProjectID)
INSERT INTO Publications(ProjectID, PubType, PubDescription, PubDetail, PubLink) VALUES (ProjectID, ?, ?, ?, ?)
I'm 100% sure this is a logical error, but I'm not sure how to fix it. I would appreciate some help, preferably through the design interface, but I don't mind doing some of the coding if it's required.
Thanks !~
Try something like this
SelectCommand="ProjectID, PubType, PubDescription, PubDetail, PubLink FROM Publications WHERE ProjectID = #prodID"
And the add a select parameter
<SelectParameters>
<asp:QueryStringParameter Name="prodID" QueryStringField="ProjectID " />
</SelectParameters>
I have a page with GridView pulling some data from a SQL Server database via Linq-to-SQL.
I made use of the automatically-generated buttons for deleting. However, in order for the delete command to work properly, I need to somehow make sure that one table in relation with those records I want to delete, is also modified (the related record in it is also looked up and deleted).
Whats the easiest way to do this?
Thanks,
Ondrej
Define a foreign-key constraint with cascade delete.
Delete Rule
Specify what happens if a user tries to delete a row with data that is involved in a foreign key relationship:
No Action An error message tells the user that the deletion is not allowed and the DELETE is rolled back.
Cascade Deletes all rows containing data involved in the foreign key relationship.
Set Null Sets the value to null if all foreign key columns for the table can accept null values.
Part of an ASP.Net 2 datasource:
SelectCommand="SELECT BU.P_GEAC_CORP_CD AS Corp_Code,
BU.Business_unit as Abbreviation,
CC.DEPTID AS Cost_Center,
CC.DESCR AS Description
FROM fstst.PS_P_CATR_BUDPT_VW CC,
fstst.ps_p_bus_unit_cnv BU
WHERE BU.Business_unit = CC.Business_unit">
This feeds a GridView which works. The display shows that
CC.DESCR AS Description
is text (non-numeric).
I want to use a textbox as a "contains" filter, i.e., if I put "Recovery" in the box,
I want the datasource to add
AND CC.DESCR Like '%Recovery%'
to the SQL. If I hard-code that line, it works.
But if I add
<SelectParameters>
<asp:ControlParameter ControlID="Dept_Name"
Name="DName"
PropertyName="Text"
Type="string" />
</SelectParameters>
without changing the SQL, I get no rows returned. Then if I put
AND CC.DESCR Like '%' + :DName + '%'
into the SQL, I get no results when the textbox is blank, and ORA-01722: invalid number as soon as I put characters in it.
Thanks for the attempt, "birdlips."
Unfortunately, the Oracle server had only minimal logging turned on.
On top of that, while I was visiting the DBA, stackoverflow somehow ended up on our company's list of forbidden sites. So I could not share the answer.
This must be a little known fact about Oracle, as our two DBAs didn't catch it either: Oracle thinks plus is for numbers no matter what the context.
It worked as soon as I changed + to ||
you need to use single quotes around the text when using a like statement with a string.
It needs to look like
AND CC_DESCR LIKE '%VALUE%'
This thing is driving me crazy, and the error is quite meaningless to me:
Unable to update the EntitySet 'TableB' because it has a DefiningQuery and no element exists in the element to support the current operation.
My tables are put like this:
TableA
int idA (identity, primary key)
...
TableB
int idA (FK for TableA.idA)
int val
TableB has no defined primary key in the SQL server. The Entity Framework has imported the table and the association and set both fields as key. But it will output that error when I try to do an insert into the table!
What's wrong??
Edit:
As suggested by Alex, the solution was this:
Right click on the edmx file, select Open with, XML editor
Locate the entity in the edmx:StorageModels element
Remove the DefiningQuery entirely
Rename the store:Schema="dbo" to Schema="dbo" (otherwise, the code will generate an error saying the name is invalid)
Remove the store:Name property
I left the key as it was, since it was OK to me that both the columns are part of the key.
Well when a table is encountered without a PrimaryKey it is treated as a View.
And views show up in the EDMX file (open in an XML editor to see) in the StorageModel\EntitySet[n]\DefiningQuery element.
When you have a DefiningQuery the Entity becomes readonly unless you add modification functions. You need 3 modifications functions (aka Stored Procedures) one for each of Insert, Update and Delete.
But you have two options:
Change the key definion:
And convince the EF that what it thinks is a view is really a table
Or add the appropriate modification functions
In your case I recommend (1).
Just Add a primary key to the table. That's it. Problem solved.
ALTER TABLE <TABLE_NAME>
ADD CONSTRAINT <CONSTRAINT_NAME> PRIMARY KEY(<COLUMN_NAME>)
I was missing a primary key on my table and got this error message. One thing I noted was after I added the key to the table, I needed to clear the table from the edmx using the designer, save the edmx, then update it again to add the table back in. It wasn't picking up the key since it was already assigned as a view. This didn't require editing the edmx manually.
Add primary key to table, delete the model from the edmx model, then select update from database, build and run...... works
#Palantir. Verify that both of you tables have Primary Keys set, and be careful with multiple primary keys set in a table.
You need to manually open the .EDMX file in notepad or notepad++ or
in any text editor of your choice.
Locate the entry in edmx:StorageModels in file opened in step1.
Find the DefiningQuery element and remove this tag entirely.
Find the store:Schema="dbo" to Schema="dbo" (if you skip this step
it will generate error of the name is invalid).
Save and close the file.
Hope it will solve the problem.