none
How to Insert one to many relationship using checkboxlist in VB.NET, LINQ to SQL? RRS feed

  • Question

  • I have 3 tables:

    CustomerType
    CusID

    EventType
    EventTypeID

    CustomerEventType
    CusID
    EventTypeID

    How to Insert one to many relationship using checkboxlist in VB.NET, LINQ to SQL?

    I want to insert the new EventTypeID 1 into EventType table with the CustomerType checkboxlists that contains 2 IDs in one FORM .


    Basically it will allow to automatically insert data into CustomerEventType table based on the EvenTypeID which has value 1 and the checks of the CusID that is using CheckBoxList1 bounded from CustomerType (Assume there are 2 IDs checked. )

    alt text


    So,  after I Click on Insert button the data in EventType table and CustomerEventType table should be:

    EventType
    EventTypeID
    1

    CustomerEventType
    EventTypeID.....CusID
    1........................1
    1........................2


    The only question that I have is, what should be the code behind the insert button using VB.NET LINQ-to-SQL to get this result? Please help me out of this problem. Thanks in advance.

    Sunday, March 7, 2010 5:21 AM

Answers

  • Ahaha I found the solution now. Very easy just change

        Dim
     newCustomerEventType = New
     CustomerEventType
    to place inside the For Loop. Work pretty sweet!
    Tuesday, March 9, 2010 1:13 PM

All replies

  • I just come up with these lines of codes:
                Dim db = new mydbDataContext
                Dim newEvent = new EventType
                newEvent.EventTypeID = txtEventID.text
                db.EventType.InsertOnSubmit(newEvent)
                db.SubmitChanges()
    
                'To select the last ID of event
                Dim lastEventID = (from e in db.EventType Select e.EventTypeID Order By EventTypeID Descending).first()
    
                Dim chkbx As CheckBoxList = CType(form1.FindControl("CheckBoxList1"), CheckBoxList)
                Dim newCustomerEventType = New CustomerEventType
                Dim i As Integer
                For i = 0 To chkbx.Items.Count - 1 Step i + 1
                    If (chkbx.Items(i).Selected) Then
                        newCustomerEventType.INTEVENTTYPEID = lastEventID
                        newCustomerEventType.INTSTUDENTTYPEID = chkbxStudentType.Items(i).Value
                        db.CustomerEventType.InsertOnSubmit(newCustomerEventType)
                        db.SubmitChanges()
                    End If
                Next
    It works fine when I checked only 1 Single ID of CustomerEventType from CheckBoxList1. It inserts data into EventType with ID 1 and CustomerEventType ID 1. However, when I checked both of them, the error message said

    Cannot add an entity that already exists.

    Can you please check this for me? Thanks so much.
    Monday, March 8, 2010 3:10 AM
  • To all LINQ to SQL participants, is my description clear enough to understand? If not, please let me know because I'm just so curious about this problem. Any suggestions, please?
    Tuesday, March 9, 2010 7:08 AM
  • Ahaha I found the solution now. Very easy just change

        Dim
     newCustomerEventType = New
     CustomerEventType
    to place inside the For Loop. Work pretty sweet!
    Tuesday, March 9, 2010 1:13 PM
  • Ahaha I found the solution now. Very easy just change

      Dim newCustomerEventType = New CustomerEventType
    to place inside the For Loop. Work pretty sweet!

    Thanks for your answer! I've tried it and it works.
    Thursday, March 3, 2011 1:15 AM