hello every body.
I defined a class with insert method for insert data to sqlserver database.
I have 2 table that one of them is child of another.
There is a relation between them.
i want to insert an entity(record) to parent table and one or some record to it's child table that child table's records have one forenkey of parent table's record.
I define two class:
1. First class named ‘StudentBLL’ ,for insert,update,delete students.
2. Second class named ‘TeacherBLL’ for insert,update,delete teachers.
See below :
class TeacherBLL
{
DataBaseDataContext _db;
public TeacherBLL(DataBaseDataContext db)
{
_db = db;
}
public bool Insert(Teacher _teacher, Students[] _students)
{
using(TransactionScope ts=new TransactionScope())
{
_db.tbl_plaques.InsertOnSubmit(_teacher);
try
{
_db.SubmitChanges();
if (new StudentBLL(_db).Insert(_students))
{
ts.Complete();
_db.Dispose();
return true;
}
else
{
return false;
}
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
return false;
}
}
}
}
class StudentBLL
{
DataBaseDataContext _db;
public StudentBLL(DataBaseDataContext db)
{
_db = db;
}
public bool Insert(Students[] _students)
{
_db.tbl_plaques.InsertAllOnSubmit(_teacher);
try
{
_db.SubmitChanges();
System.Windows.Forms.MessageBox.Show("Data Inserted Successfully...");
return true;
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
return false;
}
}
}
public partial class Form1 : Form
{
DataBase.DataClasses1DataContext _db;
public Form1()
{
InitializeComponent();
_db = new DataBase.DataClasses1DataContext();
}
private void ButtonSave_Click(object sender, EventArgs e)
{
Teacher _Teacher=new Teacher(){
ID=txt_ID.Text,
Gender=txt_Gender.Text,
Name=txt_Name.Text};
Student[] _Students=new Student[10];
.
.
.
.
.
if (new TeacherBLL(_db).Insert(_Teacher,_Students))
{
this.Close();
}
}
}
When user click save button, I create an object of Teacher BLL class and call insert method,
In insert method ,I insert teacher entity and submit my data context’s changes and create one object of StudentBLL and call insert method for each student item.( for some reasons )
I create one transaction before insert teacher and commit transaction after students inserted successfully or rollback if one of students insert has error ( Even save not Teacher and other students).
Notice that my data context object created in form and is passed to teacherbll( for some reasons) in it’s constructor and to studentbll too. So my data context object not disposed if save action has error and posts to TeacherBLL and StudentBLL in future.
If user try to save data after first save without close form, occur below error :
Cannot add an entity that already exists. on line 1 of method insert in TeacherBLL :
_db.tbl_plaques.InsertOnSubmit(_teacher);
before i try to save data in second,i check database tables and no items in previous save action were saved,so in second save action i need to remove teacher item that in first save action was saved to avoid Duplicated Data error.
please help me :
how can i solve this problem?
how can i remove that record to solve that error.
thanks alot.... !