Results 1 to 1 of 1

Thread: updating a database [* Resolved *]

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    updating a database [* Resolved *]

    Hello,

    I am developing a library system. I have 2 tables, book and student. The relationship is 1 student can have many books. I am having updating the book table when the student returns the book, the studentID (foreign key) will be cleared from the book table. I think this is the problem l am having. My code is listed below with the error description.

    Code:
    try
    			{
    				cnnReturnBook.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\IBS Library System\LibrarySystem.mdb;Persist Security Info=False";
    			
    				//Find the book that have to be returned
    				OleDbCommand cmdReturnBook = cnnReturnBook.CreateCommand();
    				cmdReturnBook.CommandText = @"SELECT * FROM BOOK
    											  WHERE RefNumber = '" + txtBookRef.Text + "' ";
    
    				OleDbDataAdapter daReturnBook = new OleDbDataAdapter(cmdReturnBook);
    				OleDbCommandBuilder cbReturnBook = new OleDbCommandBuilder(daReturnBook);
    
    				if ( daReturnBook.Fill(dtReturnBook) == 1 )//Book has been found, there should be only one 1 record in the datatable
    				{
    					//Clear the text boxes
    					txtDateOut.Text = "";
    					txtDateDue.Text = "";
    					txtStatus.Text = "Available"; //Show that the book is now available
    					txtIDNumber.Text = ""; //ID number of the student who has this book.
    					txtName.Text = "";
    					txtSurname.Text = "";
    
    					//Insert into the datatable row the new values
    					dtReturnBook.Rows[0]["Status"] = txtStatus.Text;
    					dtReturnBook.Rows[0]["DateOut"] = txtDateOut.Text;
    					dtReturnBook.Rows[0]["Remarks"] = txtRemarks.Text;
    					dtReturnBook.Rows[0]["StudentID"] = txtIDNumber.Text;
    					dtReturnBook.Rows[0]["DateDue"] = txtDateDue.Text;
    
    					daReturnBook.Update(dtReturnBook);//Error here
    				}
    			}
    			catch ( OleDbException ex )
    			{
    				MessageBox.Show(ex.ToString(),"Exeception has occurred",MessageBoxButtons.OK,MessageBoxIcon.Warning);
    			}
    			catch ( Exception ex )
    			{
    				MessageBox.Show(ex.ToString(),"Exeception has occurred",MessageBoxButtons.OK, MessageBoxIcon.Warning);
    			}
    			finally 
    			{
    				cnnReturnBook.Dispose();
    			}
    		}
    Error description
    System.Data.OleDb.OleDbException: You cannot add or change a record because a related record is required in table 'Student'.
    At System.Data.Common.DbDataAdapter.Update(DataRow[] dataRows, DataTableMapping tableMapping)
    At System.Data.Common.DbDataAdapter.Update(DataTable dataTable)

    Thanks in advance,

    Steve
    Last edited by steve_rm; Oct 22nd, 2004 at 11:26 AM.
    steve

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width