Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

i have into my database, 4 Tables, all are connected to each other.
In my Situation i just Need to explain you 2 of them.

1.) - Users Table (PK = UserNumber)
2.) - Projekt Table (PK = ProjektNumber)

If an user exists and a Projekt is signed to this user, like this example:

user: 1001 has projekt number: 0001;

So everything works fine, BUT, to add new users i created a new form, wich is working to add new users.

But if i now want to delete this user, it aint work. It is working i mean, but
it did it wrong.

I got an referenze exeption, which was Logical, cause the user had Projekt numbers added. So i discovered that if you want to delete an user, you have to check if this user have projekt numbers, and if he has, it should delete first the projektnumbers and afterwards the user.

My Programm works now like, if an user has Projekt numbers it wount delete the user, it just tells you about the References.

- I got this code succesfully implemented

To get that stuff into my form, i used Databindings, i just drag and dropped the
datatable into my form and i got some menue Points automatically, like skip Forward/backwards, add new and delete..etc.

But now, it doesnt matter if an user has or hasnt projekt numbers, it deletes the user.

here is the button code:

C#
private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
		{
			bool delete = true;
			
                        Data.lehrlingDataSet.NV_AUFKDataTable zeilen = 
                        AUFKAdapter.GetDataByKUNR(AktualKu);

			if (zeilen != null)
			{
			
			var zeile = zeilen.FirstOrDefault(a => a.KU_NR == AktualKu);
				if (zeile != null)
				{
					MessageBox.Show("This user has projekt NR");
					delete = false;
				}
			}
			if (delete == true)
			{
				this.Validate();
				this.nS_KUBindingSource.EndEdit();
			this.tableAdapterManager.UpdateAll(this.lehrlingDataSet);
			}
		}



Thank you

greets niko
Posted

1 solution

Hi Niko,

Here for deleting the records you can implement one more column to your table that shows the record is been deleted or not.

For that you can add the column as "IsDeleted" of type "bool" and having default value=0.
The record you want to delete can have the column value set to 1 otherwise it will be 0.

This is the best way to delete a record having dependencies with more then one table.

Or

There is another solution to this. You can set the property of the table as "cascade delete = true". This will help you deleting the record. But this is not at all recommended. As this will delete the record from the child tables as well as parent tables too.

So go for my 1st solution that is mostly adapted.

Thanks
 
Share this answer
 
v2
Comments
[no name] 19-Dec-12 6:35am    
All the best...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900