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. Database & SysAdmin
  3. Database
  4. Cannot insert the value NULL into column

Cannot insert the value NULL into column

Scheduled Pinned Locked Moved Database
helpquestionlearning
4 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.
  • U Offline
    U Offline
    User 9875787
    wrote on last edited by
    #1

    Hi all. I have run into a problem with inserting values into table. I have 2 tables: PUBLISHER with fields: PUBLISHER_CODE (Primary Key) and PUBLISHER_NAME. BOOK table fields: PUBLISHER_CODE (Primary Key and Foreign key to PUBLISHER table) and TITLE. The user enters bookCode and publisherName into 2 textboxes Here is the code for inserting: "INSERT INTO BOOK (TITLE) VALUES(@bookCode)"; "INSERT INTO PUBLISHER (PUBLISHER_NAME) VALUES(@publisherName)"; When running programme, the PUBLISHER table generates a new PUBLISHER_CODE (Primary Key) and publisherName inserted. Now problem is for the BOOK table the following error appears: "Cannot insert the value NULL into column 'PUBLISHER_CODE'", table BOOK :sigh: Why is this happening? Doesnt BOOK generate same/new PUBLISHER_CODE from PUBLISHER table since its a foreign key?

    M B 2 Replies Last reply
    0
    • U User 9875787

      Hi all. I have run into a problem with inserting values into table. I have 2 tables: PUBLISHER with fields: PUBLISHER_CODE (Primary Key) and PUBLISHER_NAME. BOOK table fields: PUBLISHER_CODE (Primary Key and Foreign key to PUBLISHER table) and TITLE. The user enters bookCode and publisherName into 2 textboxes Here is the code for inserting: "INSERT INTO BOOK (TITLE) VALUES(@bookCode)"; "INSERT INTO PUBLISHER (PUBLISHER_NAME) VALUES(@publisherName)"; When running programme, the PUBLISHER table generates a new PUBLISHER_CODE (Primary Key) and publisherName inserted. Now problem is for the BOOK table the following error appears: "Cannot insert the value NULL into column 'PUBLISHER_CODE'", table BOOK :sigh: Why is this happening? Doesnt BOOK generate same/new PUBLISHER_CODE from PUBLISHER table since its a foreign key?

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Member 9912091 wrote:

      Doesnt BOOK generate same/new PUBLISHER_CODE from PUBLISHER table since its a foreign key?

      No - you have to retrieve it from the inserted publisher record. I use a stored proc for this and return the new record after it is inserted, then you can use that ID to insert the book record. Or Wrap the whole thing in a SQL Transaction in a stored proc and do the insert to both table at once.

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • U User 9875787

        Hi all. I have run into a problem with inserting values into table. I have 2 tables: PUBLISHER with fields: PUBLISHER_CODE (Primary Key) and PUBLISHER_NAME. BOOK table fields: PUBLISHER_CODE (Primary Key and Foreign key to PUBLISHER table) and TITLE. The user enters bookCode and publisherName into 2 textboxes Here is the code for inserting: "INSERT INTO BOOK (TITLE) VALUES(@bookCode)"; "INSERT INTO PUBLISHER (PUBLISHER_NAME) VALUES(@publisherName)"; When running programme, the PUBLISHER table generates a new PUBLISHER_CODE (Primary Key) and publisherName inserted. Now problem is for the BOOK table the following error appears: "Cannot insert the value NULL into column 'PUBLISHER_CODE'", table BOOK :sigh: Why is this happening? Doesnt BOOK generate same/new PUBLISHER_CODE from PUBLISHER table since its a foreign key?

        B Offline
        B Offline
        Bikash Prakash Dash
        wrote on last edited by
        #3

        Primary key value cant be null. You have to provide any value for that.

        M 1 Reply Last reply
        0
        • B Bikash Prakash Dash

          Primary key value cant be null. You have to provide any value for that.

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          Bikash Prakash Dash wrote:

          You have to provide any value for that

          No you have to provide the correct key, any key will corrupt your data.

          Never underestimate the power of human stupidity RAH

          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