I am wondering what is the best way to edit a database that has multiple tables.
(Section 1) I have a Administration, Teacher and Student table that is linked up with the Person table which has a Primary Key.
The Person table consists of the general information of the person.
The Student table consists of information about the student; student ID and qualification code.
The Teacher table consists of information about the teacher; teacher ID, Reg No and password.
The Administration table consists of information about the admin; admin ID, Role and password.
As the Primary Key in the Person table is the ID, I have linked up with each of the other's table with their appropriate ID.
(Section 2) I have a Course and Qualification table that is linked up with the Student and Teacher table as well as each other.
The course table constists of the Course ID, Course Name ... and Teacher ID.
The Qualifications table consists of Qualification Code, Qualification Name and Duration.
There is a section where I have to create a view which shows just the Student ID, Course ID and has the Student's Marks in it.
I have got a combobox which then links up with the dgv(datagridview).
I have got insert and delete methods for both sections. Here is an example of the insert method into the Admin/Person table.
try
{
personTableAdapter.Insert(aFirstName.Text, aSurname.Text, Convert.ToDateTime(aDoB.Text), aPhone.Text, aAdd1.Text, aAdd2.Text, aSuburb.Text, aState.Text, aPostcode.Text, AdminType.Text);
administrationTableAdapter.Insert(Convert.ToInt32(aAID1.Text), aRole.Text, aPassword.Text);
MessageBox.Show(aFirstName.Text + " " + aSurname.Text + " has been added. Your ID is " + aAID1.Text);
this.personTableAdapter.Fill(this._30002195DataSet.Person);
this.administrationTableAdapter.Fill(this._30002195DataSet.Administration);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Here is an example of the delete method in the Admin/Person table.
try
{
personTableAdapter.Delete(Convert.ToInt32(aID.Text), aFirstName.Text, aSurname.Text, Convert.ToDateTime(aDoB.Text), aPhone.Text, aAdd1.Text, aAdd2.Text, aSuburb.Text, aState.Text, aPostcode.Text, AdminType.Text);
administrationTableAdapter.Delete(Convert.ToInt32(aAID.Text), aRole.Text, aPassword.Text);
MessageBox.Show("Person Deleted");
this.personTableAdapter.Fill(this._30002195DataSet.Person);
this.administrationTableAdapter.Fill(this._30002195DataSet.Administration);
}
catch (Exception)
{
MessageBox.Show("Cannot delete Person");
}
Those methods above are working fine, what I'm having problems with, is with the editing/updating part.
I have tried a few things and haven't worked.
Here's an example of an edit, while trying to use textboxes.
personTableAdapter.Update(aFirstName.Text, aSurname.Text, Convert.ToDateTime(aDoB.Text), aPhone.Text, aAdd1.Text, aAdd2.Text, aSuburb.Text, aState.Text, aPostcode.Text, AdminType.Text, Convert.ToInt32(aID.Text), aFirstName.Text, aSurname.Text, Convert.ToDateTime(aDoB.Text), aPhone.Text, aAdd1.Text, aAdd2.Text, aSuburb.Text, aState.Text, aPostcode.Text, AdminType.Text);
administrationTableAdapter.Update(aRole.Text, aPassword.Text, Convert.ToInt32(aAID.Text), aRole.Text, aPassword.Text);
personBindingSource.EndEdit();
administrationBindingSource.EndEdit();
administrationTableAdapter.Update(_30002195DataSet.Administration);
personTableAdapter.Update(_30002195DataSet.Person);
MessageBox.Show("Person Updated");
this.personTableAdapter.Fill(this._30002195DataSet.Person);
this.administrationTableAdapter.Fill(this._30002195DataSet.Administration);
Here I tried to do the new values/original values, while trying to use textboxes
personTableAdapter.Update(aFirstName.Text, aSurname.Text, Convert.ToDateTime(aDoB.Text), aPhone.Text, aAdd1.Text, aAdd2.Text, aSuburb.Text, aState.Text, aPostcode.Text, AdminType.Text, Convert.ToInt32(aID.Text), aFirstName.Text, aSurname.Text, Convert.ToDateTime(aDoB.Text), aPhone.Text, aAdd1.Text, aAdd2.Text, aSuburb.Text, aState.Text, aPostcode.Text, AdminType.Text);
administrationTableAdapter.Update(aRole.Text, aPassword.Text, Convert.ToInt32(aAID.Text), aRole.Text, aPassword.Text);
Trying to use the example through the mdsn, this is trying to use the datagridview.
this.Validate();
personBindingSource.EndEdit();
teacherBindingSource.EndEdit();
_30002195DataSet.PersonDataTable deletedPerson = (_30002195DataSet.PersonDataTable)
_30002195DataSet.Person.GetChanges(DataRowState.Deleted);
_30002195DataSet.PersonDataTable newPerson = (_30002195DataSet.PersonDataTable)
_30002195DataSet.Person.GetChanges(DataRowState.Added);
_30002195DataSet.PersonDataTable modifiedPerson = (_30002195DataSet.PersonDataTable)
_30002195DataSet.Person.GetChanges(DataRowState.Modified);
try
{
if (deletedPerson != null)
{
personTableAdapter.Update(deletedPerson);
}
teacherTableAdapter.Update(_30002195DataSet.Teacher);
if (newPerson != null)
{
personTableAdapter.Update(newPerson);
}
if (modifiedPerson != null)
{
personTableAdapter.Update(modifiedPerson);
}
_30002195DataSet.AcceptChanges();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (deletedPerson != null)
{
deletedPerson.Dispose();
}
if (newPerson != null)
{
newPerson.Dispose();
}
if (modifiedPerson != null)
{
modifiedPerson.Dispose();
}
MessageBox.Show("Updated");
this.personTableAdapter.Fill(this._30002195DataSet.Person);
this.teacherTableAdapter.Fill(this._30002195DataSet.Teacher);
}
Now because I am trying to edit a certain user which requires the Person table along with the other table, it just doesn't seem to work at all.
With the datagridview, I made a new view in the sql and it has both tables combined and shows when I bring it out from the datasource, but where I go into the dataset builder and try to create an update method, all I get is the "Update" not what I would get when I created the update method from just the person's table by itself.
Can someone provide me with an example or help me out someway because I am struggling with this, I can't seem to find much information at all.
Thanks.
|