asked on
VIEW:
public partial class MainWindowView : Form , IDepartmentView
{
private IDepartmentPresenter departmentPresenter;
public event AddViewDepartmentDataHandler AddViewDepartmentData;
public event DeleteViewDepartmentDataHandler DeleteViewDepartmentData;
public event DisposeContextHandler DisposeContext;
public MainWindowView()
{
InitializeComponent();
departmentPresenter = new DepartmentPresenter(this);
}
public void DataToBindingSource(Object queryDepartment)
{
dEPARTMENTBindingSource.DataSource = queryDepartment;
}
private void addDepartmentButton_Click_1(object sender, EventArgs e)
{
if (AddViewDepartmentData != null)
AddViewDepartmentData(departmentTextBox.Text);
}
private void deleteDepartmentButton_Click(object sender, EventArgs e)
{
if (DeleteViewDepartmentData != null)
{
DEPARTMENT department = (DEPARTMENT)departmentListBox.SelectedItem;
if (department == null)
{
MessageBox.Show("Sie müssen ein existierendes Department wählen", "Fehler");
return;
}
DeleteViewDepartmentData(department);
}
}
}
PRESENTER:
class DepartmentPresenter : IDepartmentPresenter
{
private companyEntities mycontext = new companyEntities();
private IDepartmentView view;
private ObjectQuery<DEPARTMENT> queryDepartment;
public DepartmentPresenter(IDepartmentView view)
{
this.view = view;
LoadDepartments();
view.AddViewDepartmentData += View_AddDepartment;
view.DeleteViewDepartmentData += View_DeleteDepartment;
}
private void LoadDepartments()
{
try
{
queryDepartment = mycontext.DEPARTMENT;
this.view.DataToBindingSource(queryDepartment);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.StackTrace);
}
}
private void View_AddDepartment(String departmentName)
{
var department = new DEPARTMENT();
try
{
department.department_name = departmentName;
mycontext.AddToDEPARTMENT(department);
mycontext.SaveChanges();
this.view.DataToBindingSource(queryDepartment.Execute(MergeOption.AppendOnly));
}
catch (UpdateException ex)
{
Console.WriteLine("Error: " + ex.StackTrace);
MessageBox.Show("The department: " + departmentName + " already exists", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void View_DeleteDepartment(DEPARTMENT department)
{
try
{
mycontext.DeleteObject(department);
mycontext.SaveChanges();
}
catch (UpdateException ex)
{
Console.WriteLine("Error: " + ex.StackTrace);
}
}
}
ASKER
ASKER
ASKER
ASKER
catch (UpdateException ex)
{
Console.WriteLine("Error: " + ex.StackTrace);
MessageBox.Show("The department: " + departmentName + " already exists", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
mycontext.DeleteObject(department);
mycontext.SaveChanges();
}
catch (EntityException ex)
{
Console.WriteLine("Error: " + ex.StackTrace);
MessageBox.Show("The department: " + departmentName + " already exists", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
mycontext.DeleteObject(department);
mycontext.SaveChanges();
}
ASKER
ASKER
The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.
TRUSTED BY
ASKER
I tried your code snippet on my SQLite database and it worked fine!
I tried your code snippet on my SQL Compact Edition 3.5 database and it did not work ?
Any explanation for that before I give an A rating?