As shown in the previous article, there are two ways to delete, update, and insert data. One is to set the corresponding method in the data source editor, the other is to write the code by yourself.
The updated code is provided below:
Test_dbDataSet.courseRow cro;
Test_dbDataSet.facultyRow fro;
If (course. text = "") | (facultyid. text = "") | (this. name. text = "") | (this. num. text = ""))
MessageBox. Show ("one or more items are not filled in. Please enter them before submitting them! ");
Else
{
/// In the first method, the primary key of the table is not updated here. The best way to update the primary key is to find the record, delete it, and insert a new record.
If (cbupdate. Text = "tableAdater. update ")
{
Int f = facultyTableAdapter. Updatefaculty (this. name. Text, this. num. Text, this. facultyid. Text );
Int c = courseTableAdapter. Updatecourse (this. course. Text, this. facultyid. Text );
If (f> 0) & (c> 0 ))
MessageBox. Show ("Update successful! ");
Else
MessageBox. Show ("update failed! ");
}
/// Method 2
Else
{
String cid;
Fro = test_dbDataSet.faculty.FindByfaculty_id (facultyid. Text );
Fro = updatefacultydata (ref fro );
This. Validate ();
FacultyBindingSource1.EndEdit ();
Int f = facultyTableAdapter. Update (test_dbDataSet.faculty );
Cid = courseTableAdapter. getcourseid_fid (facultyid. Text );
Cro = test_dbDataSet.course.FindBycourse_id (cid );
Cro = updatecoursedata (ref cro );
/// This. Validate ();
/// FacultyBindingSource1.EndEdit ();
/// CourseBindingSource1.EndEdit ();
Int c = courseTableAdapter. Update (test_dbDataSet.course );
If (f> 0) & (c> 0 ))
MessageBox. Show ("the second method is updated successfully! ");
Else
MessageBox. Show ("the second method failed to update! ");
}
Deleted code:
Test_dbDataSet.courseRow crow;
Test_dbDataSet.facultyRow frow;
/// A dialog box is displayed to check whether data is to be deleted.
If (MessageBox. Show ("are you sure you want to delete the data? "," Delete ", MessageBoxButtons. YesNo) = System. Windows. Forms. DialogResult. Yes)
{
/// Method 1
If (cbway. Text = "tableAdater. delete ")
{
String str;
Str = facultyTableAdapter. getid (cbdelete. Text );
Int k = facultyTableAdapter. Deletefaculty (str );
Int j = courseTableAdapter. Deletecourse (str );
If (k> 0) & (j> 0 ))
MessageBox. Show ("deleted successfully! ");
Else
MessageBox. Show ("deletion failed! ");
}
/// Method 2:
Else
{
String id = facultyTableAdapter. getid (cbdelete. Text );
Frow = test_dbDataSet.faculty.FindByfaculty_id (id );
Frow. Delete ();
Int f = facultyTableAdapter. Update (test_dbDataSet.faculty); // The number of deleted records returned by this function
String cid = courseTableAdapter. getcourseid_fid (id );
Crow = test_dbDataSet.course.FindBycourse_id (cid );
Crow. Delete ();
Int c = courseTableAdapter. Update (test_dbDataSet.course );
If (c> 0) & (f> 0 ))
MessageBox. Show ("the second method is deleted successfully! ");
Else
MessageBox. Show ("the second method failed to be deleted! ");
}