C# - Checking Where All Foreign Key Is Being Used?

Jan 8, 2011

Lets say there's a table Table1 with Primary key as "MainID" and this MainID is being used as a Foreign key in many tables.

Now when I try and delete a record from Table1 who's MainID as foreign key is being used in some child tables, then it throws this exception that "Foreign key conflict n stuff"..

Now I want to design my Delete Strored procedure in such a way that first of all checks ALL the child t ables that have that foreign key column "MainID" and if MainID from Table1 is being used in any of the child tables it should return 0 . Based on this 0 value I want an alert box to show that will say "Still in use or watever" and wont let the user delete that record unless ALL the records in child tables have been deleted that were using that mainID from table1.

I can't use ON DELETE CASCADE. I want the user to delete child table records manually one by one. Only after child table records using the MainID have been deleted that the user can delete record from Table1

View 1 Replies


Similar Messages:

MVC 2 Relation Without Foreign Key With Entity Framework 4 Or With Foreign Key?

Sep 13, 2010

1 - Is it necessary to have foreign key to obtain a Relation in Entity Framework between each entity.?
2 - I have a Language Table and many many table with a foreign key related to the language table. Is it right to add this foreign key or I should do something else ?
Ex:
Language
LangID
LangName

TableTextA
TblAID
TextInfo
LangID

TableTextB
TblBID
TextInfo
LangID

TableTextC
TblCID
TextInfo
LangID
etc ...

View 1 Replies

C# - Get Images By Foreign Key?

Mar 22, 2011

I had table had data about product photos and this table had a relation to product table and product may be has more than Image. I did my code well to get all images for product by productID query string but only first image retrieved. Note that ProductID in handler page is a foreign key into the Photobank images table, also I note that productID had value of ID table didnot the productID:

stored proc:

ALTER proc [dbo].[SP_GetPhotoBank]
(
@ProductID Int
)
as

[Code].....

View 5 Replies

Identity_insert Error With Foreign Key

Jun 22, 2010

I've got 2 tables where each has a primary key which is also the identifier (Both fields called "id" in each table) In the "picture" table as one of the fields I've got the photoalbumid (The id of the "photoalbum" table). This field is just an int field. I'm getting the following error: "Cannot insert explicit value for identity column in table 'Picture' when IDENTITY_INSERT is set to OFF" Can anyone give me some advise? It's such a basic thing I would hope that there's just something small in the creation of the database that I've missed out. Let me know if you need more info.

View 9 Replies

ADO.NET :: Want To Insert Data With Foreign Key?

Oct 5, 2010

I've two table

a). Country

1.CountryId -PK

2. Country Name

b)City

1.CityId---PK

2.CityName

3.CountryId --FK to Country table

and my code is FWUDataContextDataContext CityContext = new FWUDataContextDataContext();

View 3 Replies

MVC :: How To Update A Changed Foreign Key In 3.5

Jul 24, 2010

I am trying to build the MVC Music Store from ASP.Net, my only problem is that the tutorial is made for asp.net 4.0 C# and mvc2 an i am creating it with asp.net 3.5 VB mvc 1. For now i found out a lot of thing wich will work much better in the new frameworks, but my webhost still doesnt support it, because they have some problemms with PLESK.

Now i have a problem i cant figure out, i cant update data from a dropdown list, in the database using the updatemodel, that are foreign keys. Just all other data wich are not foreign keys and primary keys are updateable. These are automaticly created in the sql entity model.

Here is my code.

[code]....

View 3 Replies

ADO.NET :: Foreign Key Not Available For Insert In Entity

Feb 17, 2011

I have the following code

Customer currentCustomer = new Customer();

when i do currentCustomer. In the drop down I can see all the columns listed except one column which is a foriegn key from order table , how do i insert into intorder_fk

View 1 Replies

SQL Server :: Foreign Key - Cannot Use Primary Key

Sep 10, 2010

In my databse i have a databse tables

UserRole -UserID,UserName,RoleName.
Compaign - CompaignId,Region,CompaignName,LOB.
Employee -EmpNo,EmpName,CompignId
Timesheet -TimeId,EmpNo,Date.

I dont have primary key for Compaign Table CompaignId is Identity column. Now i want to use CompaignId in Employee Table because based on compaign user will retrieve employee data in Front end. Again Employee table doesn't have primary key. I am uploading excel file data to databse but in excel file there is no empNo so i cannot use primaryKey i have EmpName in excel file. How to solve this issue. How to use master table if possible create table structure for my requirements.

View 5 Replies

ADO.NET :: Foreign Key Not Mapping In .edmx?

Nov 16, 2010

So I am working with a database and am trying to get my model code working.I have a situation where I have a table called accounts that for simplicity has 3 columns

accountid, accounttype, parentid

basically its a parent child relationship. if parentid is null its a parent, its not its a child of the accountid.Im running into problems getting the .edmx file mapping. parentid is a foriegn key on the table.

how can access the parentid column? i can see it in account navigation account1 but Im not sure how to get access to it in my linq query

View 3 Replies

MVC :: Replace A ID Value (a Foreign Key) In A View By Its Value?

Jun 17, 2010

As i start to work with MVC simple things are not so simple. I have a view that renders correctly a single table of my Database (using Entitry framework created automatically by visual from my SQL database).The table show everything including the foreign key and i i seeking for replacing the foreign key by a friendly name of my foreign table.In the controleur i have the following code :

public
ActionResult Index()"Equipement"]
= new
SelectList(_entities.T_E_EQUIPEMENTS_RESEAU_EQP.ToList(),
"EQP_ID",
"EQP_NOM");
return View(_entities.T_E_CONSOMMATEURS_CSO.ToList());

both table are joined by the foreign key CSO_EQP_ID and the primary key EQP_IDI have no clue of what to define in the view ?Any exemple of this simple need will be welcome.

View 3 Replies

MVC :: ModelBinding With Foreign Keys?

Feb 22, 2011

I have an MVC3 with EF4 project. I have a screen for order .Order has 2 id's inside it for Billilng and Shipping AddressID .When I go and create a new order these fields render as single textboxes as Ids'Question is how can I render full address textboxes (addressline1,city,state,zip etc) do I have drop in the html markup for them? In that case while capturing the data back I willl have to go with classic way of retrieving name/value pair and then do insert into address bring back that identity and update the order record...?

Create(int customerID,FormCollection fc){}

So the main question is how can I do ModelBinding with foreign keys?

View 8 Replies

SQL Server :: Assigning A Foreign Key?

Mar 10, 2011

I have two tables, which is table 'A' and table 'B'. Table B contains a foreign key that is referenced to table 'A'. The question now is, could I used an option in my project in visual studio an 'Others specify'? I believed that it's not possible, isn't it?If I have a selection that is bound to a datasource then is it possible to make an option 'Others specify' without adding the specified value to the database, which in turn would not have a unique ID? Table B contains the table A ID PK field, and all values from table A to table B should contain a unique ID.

View 2 Replies

MVC :: ADO.NET Framework And Error With Foreign Key?

Apr 21, 2010

I have a database with the structure

Client
--------------
ClientID -> PK
FistName
LastName
AddressID -> FK to Address.AddressID

and

Address
--------------
AdressID -> PK
City
CityCode

I created an initial set of address so

0 - London - 1000
1 - Paris - 2000
2 - Madrid - 3000
3 - Lisbon - 4000

I'm using Visual Studio 2008 with MVC.
Framwork to use ADO.NET to create the model

My view is:

[Code]....

My model is just: ... Private _dataModel As New just2testDBEntities
[Code]....

When post it retun erro

{"Can not insert the value NULL into column 'City', table 'dbo.Address'; column does not allow nulls. INSERT fails. ... statement terminated."}What I was intended to do is to insert a client and not a client and an address.

View 5 Replies

Web Forms :: How To Retrieve The Username For The Foreign Key

Mar 1, 2010

I have 2 tables: Users and Accounts. The Accounts table has CreatedBy, UpdatedBy fields for audit purposes.

On my UI I need to display: ABC Inc. was last modified by "John Smith" - where John Smith is the UpdatedBy ID from the user's table.

In my DataContext file I have removed all relationships. Can anyone tell me how to retrieve the username for the foreign key.

View 6 Replies

How To Delete Values In Case Of Foreign Key

Sep 7, 2010

How to delete values in case of foreign key?

[Code]....

View 8 Replies

ADO.NET :: How To Insert Foreign Key In Entity Framework In MVC

Oct 18, 2010

I m try to insert record to table, its working well without foreignkey using Entity model. But i need to insert foreign key. I used the following query, its not working well via Entity model.

objFacility.FacilityCategoryReference.EntityKey =
new System.Data.EntityKey("testEntities.test",
"id", 10);

View 1 Replies

Access :: Getting List Of Foreign Keys?

Feb 28, 2011

I am using Access 2003 Database and I need to retrieve list of a table's foreign keys and tables they are related to in C#. I used GetSchema() function of OleDbConnection as well as OdbcConnection classes. But It just returns the name of key among few other details.

OdbcConnection con = new OdbcConnection(conStr);

View 6 Replies

SQL Server :: Creating Foreign Key And Primary Key?

Dec 7, 2010

I am new to sql. I got this task to design a table in sql. It has for columns user_ID(PK, FK1) , WebSite_ID (PK, FK2), Create_Dt, Update_Dt How do I create PK and Fk1 and PK FK2 in sql server 2008 R2

View 3 Replies

SQL Server :: When To Use Primary And Foreign Key In Our Project

Oct 20, 2010

I need ms sql video, in which i can learn how to set primary and foreign key.. why it useful and how it works...

View 3 Replies

ADO.NET :: Primary And Foreign Key Auto Populate?

Aug 7, 2010

In LINQ to SQL, can primary and foreign keys be autopopulated from an associated key?I'm using the asp.net userID assigned by the create user wizard as the primary key/foreign key in several tables. Right now, when a new user is created, their userID shows up in the asp.net tables but not the ones I created. I thought it would be easier if the keys could fill themselves in, like cascade, so as I have the user fill out forms, they would already have a row in the table created and it would be an of an update instead of an insert row.

View 2 Replies

MVC :: Creating Foreign Key In Model Classes?

May 31, 2010

I am trying to create a relation between 2 of my model classes. I have separated my Model classes to be in their own project "Domain Model" and the application exists in the "WebUI" project.

I have 2 models.... SupportIssues and Users....

SupportIssues has a column UserID which is also a primary key in the Users table.

I want to create a relationship where one user can have many support issues.

I just can figure out how to do this?

Here is a snippet of my supportIssue.cs class:

[Code]....

How can i link UserID to my User.cs class?

Is it anything to do with the [Associate] tag?

View 2 Replies

Updating A Foreign Key To Null Value Using Nhibernate?

Jan 31, 2011

I have two table BuildGroup and table DocumentTemplate. DocumentTemplate table has BuildGroupId as foreign key which is nullable. In a certain senario I update BuildGroupId in DocumentTemplate table.

[Code].....

In another senario I need to set BuildGroupId in DocumentTemplate table again to dbnull.value. I tried with different cases as in else block. It giving the error : Cannot implicitly convert type 'System.DBNull' to 'int'. How can I update a foreign key value with NULL?

View 2 Replies

C# - Handle Foreign Key For Optional Field?

Mar 23, 2010

What is the best way to handle following situation? A dropdown(for master table) is optional in a particular form. But, In database table the field is constrained with foreign key. If user don't select from dropdown then It creates problem because of foreign key. One solution is to create default option in master table and use it in case of blank selection. but in dropdown, we need to handle this to show on top. Is it perfect solution?

View 1 Replies

MVC :: Saving Data To A Table With Foreign Key?

Jan 27, 2011

I have a app that records some jobs and those jobs can have notes attached therefore i connected the table with a foreign key, my notes to a Job.

in the details view of the job i allow the user to view the jobs by sending the id and fulling all the notes that match the id in the index view for the notes table. in the list of notes i have a action link to create a new note, here i sent also the id of the job to the create action of the control and try to insert that intot he note JobId to make the reference. I get this error and the note is not created. the note creats from code but not via the note controller creat action.

i get this - An error occurred while updating the entries. See the inner exception for details. below is the create action in the note controller

public ActionResult Create(int id)

{
// var newnote = db.Jobs.Include("StatusNotes").Single(j => j.JobId == id);[code]...

View 4 Replies

How To Make Composite Key And Foreign Keys

Mar 22, 2011

I am developing asp.net MVC3 application and I have following entity

This entity has composite key (CreditRegistryId and Accoubnt No are primary keys). CreditRegistryId is a foreign key as well. How can I make composite key and foreign key. I am using DbContext API from EF 4.1. I am not using edmx ( ORM designer)

[code]...

View 1 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved