Updating an entity

Posts   
 
    
dcarlile
User
Posts: 11
Joined: 09-Nov-2006
# Posted on: 20-Nov-2006 18:44:57   

When I load in an existing entity collection, and make changes to it... Then SaveEntity, a new ID is created. I don't want to create a NEW entity, I just want to UPDATE the entity. How do I do this? Thanks, Dan

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 20-Nov-2006 19:27:56   

please follow the message guidelines: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=7722

Also, a code snippet would be great

Frans Bouma | Lead developer LLBLGen Pro
dcarlile
User
Posts: 11
Joined: 09-Nov-2006
# Posted on: 20-Nov-2006 20:56:07   

I hope this helps.

#region PROPERTIES

private ProductionRequestEntity _request;
private ProductionRequestItemEntity _item = new ProductionRequestItemEntity();
private EntityCollection _items = new EntityCollection();
private DataSet _dsRequest;
//-------

//requestId is Id of current ProductionRequest
ProductionRequest request = new ProductionRequest(requestId);
//open form 
request.ShowDialog();
//---------

private void PopulateRequest()
{
      // This is a request that alread exists
      if (_request == null)
      {
          _request = new ProductionRequestEntity();

          //fill fields
          txtOrganizationName.Text = _dsRequest.Tables[0].Rows[0]["OrganizationName"].ToString();
          txtAddress.Text = _dsRequest.Tables[0].Rows[0]["Address"].ToString();
          txtCity.Text = _dsRequest.Tables[0].Rows[0]["City"].ToString();
          drpState.Value = _dsRequest.Tables[0].Rows[0]["City"].ToString();
          txtZipCode.Text = _dsRequest.Tables[0].Rows[0]["ZipCode"].ToString();

          //fill entity
          _request.Id = Convert.ToInt32(_dsRequest.Tables[0].Rows[0]["Id"].ToString());
          _request.OrganizationName = _dsRequest.Tables[0].Rows[0]["OrganizationName"].ToString();
          _request.Address = _dsRequest.Tables[0].Rows[0]["Address"].ToString();
          _request.City = _dsRequest.Tables[0].Rows[0]["City"].ToString();
          _request.State = _dsRequest.Tables[0].Rows[0]["State"].ToString();
          _request.ZipCode = _dsRequest.Tables[0].Rows[0]["ZipCode"].ToString();

          if (_dsRequest.Tables[1].Rows.Count > 0)
                {
                    grdRequests.DataSource = _dsRequest.Tables[1];
                    grdRequests.DisplayLayout.Bands[0].Override.CellClickAction = Infragistics.Win.UltraWinGrid.CellClickAction.RowSelect;

                    //collect entity
                    for (int i = 0; i < _dsRequest.Tables[1].Rows.Count; i++)
                    {
                        ProductionRequestItemEntity item = new ProductionRequestItemEntity();
                        item.CameraCard = _dsRequest.Tables[1].Rows[i]["CameraCard"].ToString();
                        item.EventName = _dsRequest.Tables[1].Rows[i]["EventName"].ToString();
                        item.EventTitle = _dsRequest.Tables[1].Rows[i]["EventTitle"].ToString();
                        item.Frame = Convert.ToInt16(_dsRequest.Tables[1].Rows[i]["Frame"].ToString());
                        item.Id = Convert.ToInt32(_dsRequest.Tables[1].Rows[i]["Id"].ToString());
                        item.Imagename = _dsRequest.Tables[1].Rows[i]["ImageName"].ToString();
                        item.JobId = Convert.ToInt32(_dsRequest.Tables[1].Rows[i]["JobId"].ToString());
                        item.Quantity = Convert.ToInt16(_dsRequest.Tables[1].Rows[i]["Quantity"].ToString());

                        _request.ProductionRequestItems.Add(item);
                    }
                }
          }
//-------

//Save entity
private void btnSave_Click(object sender, System.EventArgs e)
{

    _request.UpdatedBy = Common.UserInfo.Username;
                int ireqid = _request.Id;
    _request = (ProductionRequestEntity)Common.server.SaveEntity(_request, true);
    foreach(ProductionRequestItemEntity item in _request.ProductionRequestItems)
    {
        item.UpdatedBy = Common.UserInfo.Username;
        item.ProductionRequestId = _request.Id;
    }
                try
                {
                    Common.server.SaveEntityCollection(_request.ProductionRequestItems);
                    OnSaveProductionRequest();
                    base.statusForm.Text = Common.SUCCESSFUL_SAVE;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message +
                        Environment.NewLine + ex.InnerException);
                    base.statusForm.Text = Common.UNSUCCESSFUL_SAVE;
                }

}


dcarlile
User
Posts: 11
Joined: 09-Nov-2006
# Posted on: 20-Nov-2006 21:05:49   

Do I have to Delete the records from the SQL tables before I SAVE the Entity again?

dcarlile
User
Posts: 11
Joined: 09-Nov-2006
# Posted on: 20-Nov-2006 21:48:22   

version 1.0.2005.1 final

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 21-Nov-2006 02:27:46   

You indicate here that that request already exists, but then initialize a new ProductionRequestEntity.


     // This is a request that alread exists
     if (_request == null)
     {
         _request = new ProductionRequestEntity();
         ...

This new request will be marked as IsNew = true and will therefore be saved with an insert. You should set the entity's IsNew property to false before the save.