Programming Lab 2: Employee Records Input, due Thursday, 9/28
Objective: To write a program that allows the user to insert records, including foreign key records as
needed
Here are the details and rules:
- Write a program to allow the user to enter and save employee data like shown here:
- All the objects you use to execute queries, retrieve results, etc. should be created in code that
you write. Don't drag and drop to create the Connection object or such.
- The "Save" button should work as expected: Saving the data in tblEmployee, using a MsgBox to
indicate success or failure
- If the user enters a Department ID which is not in tblDepartment, the foreign key constraint will
prevent that addition. In this case, your program should ask the user to enter the name of this new
department and create a record for it in tblDepartment (the ID doesn't have to be the one the user
entered). Then after this new record has been created, the employee record can be saved as well.
- Use Transactions to ensure that, when a new department record is created, the corresponding employee
record is created as well. (Make both changes or none at all.)
- Hint: the best way to do this program is to use a SqlCommand object, on which you execute the
ExecuteNonQuery method to execute an INSERT query which you assemble in code based on the user's input.
If the command execution fails because of a foreign key violation, then you'll need to execute another
INSERT query, on tblDepartment, then try the tblEmployee insert again.
- For 5% Extra Credit: Make your program completely robust (idiot-proof). So no matter what
the user enters, the program does not crash - you handle all errors as best you can. For example, if the
user leaves the TextBoxes blank, or if the user types non-numeric characters for the salary, or if the
database is not available (maybe the server is stopped), then you catch the exception, produce an
appropriate error message or otherwise handle it, and the program can still go on.
- When turning in your homework, simply turn in the code for the form. Print this text or e-mail it to
cpersiko@ccsf.edu
Return to main CS 112D page