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. Please Help me :(

Please Help me :(

Scheduled Pinned Locked Moved Database
helpdatabasequestion
5 Posts 2 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.
  • D Offline
    D Offline
    Dayekh
    wrote on last edited by
    #1

    Hi, 2 tables. User table and Request Table. Request table has a foriegn key in the Request table called AssignedToID. I would like to store a Request record without specifying the AssignedToID. I am receiving the following error: INSERT statement conflicted with COLUMN FOREIGN KEY constraint 'FK_tblRequest_tblUser1'. The conflict occurred in database 'PMSystem', table 'tblUser', column 'inUserID'. The input for AssignedToID is a Dropdown List. Here's how it is bound: lst2.Text = "None" lst2.Value = 0 lst2.Selected = True sQueryDDL = "SELECT tblUser.inUserID, tblUser.vchSurname + ', ' + tblUser.vchForename AS AssignedToName FROM tblUser ORDER BY tblUser.vchSurname" sTableDDL = "tblUser" dsDDL = clsCommon.getData(sQueryDDL, sTableDDL) dsDDL.Tables(0).DefaultView.Sort = "AssignedToName" Me.ddlAssignedToID.DataSource = dsDDL.Tables(0).DefaultView Me.ddlAssignedToID.DataTextField = "AssignedToName" Me.ddlAssignedToID.DataValueField = "inUserID" Me.ddlAssignedToID.DataBind() Me.ddlAssignedToID.Items.Insert(0, "lst2") The AssignedToID field is not mandatory and if a user does not select anything, the default value to be stored is 0. The compiler is not liking this. What could be wrong? Thank you in advance.

    P 1 Reply Last reply
    0
    • D Dayekh

      Hi, 2 tables. User table and Request Table. Request table has a foriegn key in the Request table called AssignedToID. I would like to store a Request record without specifying the AssignedToID. I am receiving the following error: INSERT statement conflicted with COLUMN FOREIGN KEY constraint 'FK_tblRequest_tblUser1'. The conflict occurred in database 'PMSystem', table 'tblUser', column 'inUserID'. The input for AssignedToID is a Dropdown List. Here's how it is bound: lst2.Text = "None" lst2.Value = 0 lst2.Selected = True sQueryDDL = "SELECT tblUser.inUserID, tblUser.vchSurname + ', ' + tblUser.vchForename AS AssignedToName FROM tblUser ORDER BY tblUser.vchSurname" sTableDDL = "tblUser" dsDDL = clsCommon.getData(sQueryDDL, sTableDDL) dsDDL.Tables(0).DefaultView.Sort = "AssignedToName" Me.ddlAssignedToID.DataSource = dsDDL.Tables(0).DefaultView Me.ddlAssignedToID.DataTextField = "AssignedToName" Me.ddlAssignedToID.DataValueField = "inUserID" Me.ddlAssignedToID.DataBind() Me.ddlAssignedToID.Items.Insert(0, "lst2") The AssignedToID field is not mandatory and if a user does not select anything, the default value to be stored is 0. The compiler is not liking this. What could be wrong? Thank you in advance.

      P Offline
      P Offline
      Private_Void
      wrote on last edited by
      #2

      It looks like you are inserting a value into tblUser, but the Column inUserID needs to have the same value as what is going into tblUser.

      D 1 Reply Last reply
      0
      • P Private_Void

        It looks like you are inserting a value into tblUser, but the Column inUserID needs to have the same value as what is going into tblUser.

        D Offline
        D Offline
        Dayekh
        wrote on last edited by
        #3

        To be honest, I just want to leave the "AssignedTo" value as null, but it won't let me :(. If anyone has any suggestions, please help. Thank you for your reply mate.

        P 1 Reply Last reply
        0
        • D Dayekh

          To be honest, I just want to leave the "AssignedTo" value as null, but it won't let me :(. If anyone has any suggestions, please help. Thank you for your reply mate.

          P Offline
          P Offline
          Private_Void
          wrote on last edited by
          #4

          Columns with constraints or keys can not have null values. You will need to either eliminate the constraint on the AssignedTo column or will have to provide the same value from the insert in both places. In the db if you run sp_help [TableName] you will see the constraints for both of your tables and I am fairly sure that you will see a constraint on the AssignedTo Column.

          D 1 Reply Last reply
          0
          • P Private_Void

            Columns with constraints or keys can not have null values. You will need to either eliminate the constraint on the AssignedTo column or will have to provide the same value from the insert in both places. In the db if you run sp_help [TableName] you will see the constraints for both of your tables and I am fairly sure that you will see a constraint on the AssignedTo Column.

            D Offline
            D Offline
            Dayekh
            wrote on last edited by
            #5

            I found out what the problem was. I was leaving the AssignedTo field unassigned, but the parameter was automatically being populated with a value of 0. And ofcourse there is no User in the User table with an ID of 0, therefore it was throwing that error. So all I had to do was put an IF statement in place. If the Value was 0, make the parameter = (variable = System.DBNull.Value) Problem sorted. Thank you both for your replies! :)

            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