views:

336

answers:

1

Hello guys, in my database there are 3 tables

CustomerType
CusID

EventType
EventTypeID

CustomerEventType
CusID
EventTypeID

alt text


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  A: 

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.
Obalix
Thx for the reply Obalix! The result that I want to have is I want to insert the new EventTypeID 1 into EventType table with the CustomerType checkboxlists that contains 2 IDs in one FORM .So, after I Click on Insert button the data in EventType table and CustomerEventType table should be:EventTypeEventTypeID1CustomerEventTypeEventTypeID = 1, 1 CusID = 1, 2
Vicheanak
http://social.msdn.microsoft.com/Forums/en-US/linqtosql/thread/242a3235-1d62-4c5d-822a-575be7f5f141You can check this post for the clear description. Thx.
Vicheanak
The button will be clicked for 1 time only. I'm just trying to make the database and the code easy to read only, actually the EventType table, there are EventTypeID and EventName that the EventTypeID is the Primary Key auto increment by 1 so that It's always unique. The EventTypeID TextBox actually suppose to be the EventName Textbox. The CustomerType CheckboxList is bound with 2 IDs from CustomerTypes 1 and 2. Thx again.
Vicheanak
@Vicheanak: See edit. Sorry for the C# but I don't trust my VB without Visual Studio.
Obalix