Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. updating a database

updating a database

Scheduled Pinned Locked Moved C#
helpdatabasesecurityannouncementlearning
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    steve_rm
    wrote on last edited by
    #1

    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. 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

    H C 2 Replies Last reply
    0
    • S steve_rm

      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. 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

      H Offline
      H Offline
      Hugo Hallman
      wrote on last edited by
      #2

      I think that the problem is that you've entered the id of a student that doesn't exist in the Database. Either enter a corresponding student into the database or remove the constraint that there must be a student actually responsible for the loan. ;)

      1 Reply Last reply
      0
      • S steve_rm

        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. 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

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        This should really be posted in the ADO.NET forum[^], but I'll try an answer it anyway. Without knowing more about your data model, I'm guessing that you have no student whose Id is an empty (zero length) string (This is not the same as NULL). And when you update the table the row is trying to relate itself to a student with an empty string ID. If my guess is correct: You should set StudentID in the BOOK table to be nullable. If the student is to be cleared you should set the value of the column to be DBNull.Value.


        Do you want to know more?


        Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Please remember that the force of gravity can go up as well as down.

        S 1 Reply Last reply
        0
        • C Colin Angus Mackay

          This should really be posted in the ADO.NET forum[^], but I'll try an answer it anyway. Without knowing more about your data model, I'm guessing that you have no student whose Id is an empty (zero length) string (This is not the same as NULL). And when you update the table the row is trying to relate itself to a student with an empty string ID. If my guess is correct: You should set StudentID in the BOOK table to be nullable. If the student is to be cleared you should set the value of the column to be DBNull.Value.


          Do you want to know more?


          Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Please remember that the force of gravity can go up as well as down.

          S Offline
          S Offline
          steve_rm
          wrote on last edited by
          #4

          Hello Thanks for your help. I did what you asked, but l am still having the same problem. As i am using MS Access i can't set the StudentID in the book table to be Nullable. dtReturnBook.Rows[0]["StudentID"] = DBNull.Value; //Gave a compile error dtReturnBook.Rows[0]["StudentID"] = DBNull.Value.ToString(); //Gave the same error as the first problem. The problem remains, l need to clear the StudentID in the book table, as the student no longer has that book. Any other ways to solve this problem. Thanks in advance, Steve

          C 1 Reply Last reply
          0
          • S steve_rm

            Hello Thanks for your help. I did what you asked, but l am still having the same problem. As i am using MS Access i can't set the StudentID in the book table to be Nullable. dtReturnBook.Rows[0]["StudentID"] = DBNull.Value; //Gave a compile error dtReturnBook.Rows[0]["StudentID"] = DBNull.Value.ToString(); //Gave the same error as the first problem. The problem remains, l need to clear the StudentID in the book table, as the student no longer has that book. Any other ways to solve this problem. Thanks in advance, Steve

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            steve_rm wrote: As i am using MS Access i can't set the StudentID in the book table to be Nullable. I've not used Access in a few years, so I may be wrong, but you can set Required to No - which is the same as saying Nullable. steve_rm wrote: DBNull.Value; //Gave a compile error Not sure why that would be...


            Do you want to know more?


            Vogon Building and Loan advise that your planet is at risk if you do not keep up repayments on any mortgage secured upon it. Please remember that the force of gravity can go up as well as down.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups