Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 646373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:36:16+00:00 2026-05-13T21:36:16+00:00

in my database there are 3 tables CustomerType CusID EventType EventTypeID CustomerEventType CusID EventTypeID

  • 0

in my database there are 3 tables

CustomerType
CusID

EventType
EventTypeID

CustomerEventType
CusID

EventTypeID

alt text http://img706.imageshack.us/img706/8806/inserevent.jpg

Dim db = new CustomerEventDataContext
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.

Any suggestions please? Thx in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:36:17+00:00Added an answer on May 13, 2026 at 9:36 pm

    Did you change the EventID before you pressed the button again. To me it looks as you did not. This would result that the code tries to insert the event with the ID 1 into the database, although, it is already there.

    Maybe try increasing the event ID automatically or check whether the event is alreay present before trying to insert it.

    Edit:

    OK, here is what I think you want to do … if I understood it correctly (it’s in C# as I am more fluent in that language – however you should be able to easily convert the algorithm to VB – so just take it as pseudo code):

    var db = new CustomerEventDataContext();
    var newEvent = db.EventTypeSingleOrDefault(x => x.EventTypeId == txtEventID.Text);
    if (newEvent != null) {
        newEvent = new EventType();
        newEvent.EventTypeId = txtEventID.Text;
        db.EventType.InsertOnSubmit();
    }
    
    var chkbx = (CheckBoxList) form1.FindControl("CheckBoxList1");
    for (int i = 0; i < chkbx.Items.Count; i++) {
        var value = chkbxStudentType.Items(i).Value;
        if (db.CustomerEventTypes.SingleOrDefault(x => x.EventTypeId == newEvent.EventTypeId) != null) {
            // item already exists
        } else {
          var newCustomerEventType = new CustomerEventType();
          newCustomerEventType.INTEVENTTYPEID = newEvent.EventTypeId;
        newCustomerEventType.INTSTUDENTTYPEID = value;
        db.CustomerEventType.InsertOnSubmit(newCustomerEventType);
        }
    }
    
    db.SubmitChanges();
    

    Two things I noticed:

    1. You were adding the new EventType and then selecting the last event type based upon the id. This may not result in the item you just added.
    2. You do not have to call SubmitChanges after each InsertOnSubmit. The DataBaseContext implementaton holds the inserted objects for you and you can reference them. Then you do a single submit to commit all changes. Note, however, in some complex circumstances a separate SubmitChanges is necessary, but this is rarely the case.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a simple app to edit database tables. Are there any code generators
In this sample database there are two tables, products and prices. The goal is
I've got collection of geo objects in database: There are four Tables: Countries Regions
Are there any examples of database design for public transport time-tables ? Or any
Is there a best practice in getting data from multiple database tables using Zend?
Imagine 2 tables in a relational database, e.g. Person and Billing. There is a
I am working on a tag cloud application. There are 3 database tables. Content
My database contains three tables called Object_Table , Data_Table and Link_Table . The link
I've got a database with three tables: Books (with book details, PK is CopyID),
I have a MySql database with three tables I need to combine in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.