private void AddressesChangesUpdate(cUser Demography) { List <cAddress> addresses = Session["dem_Addresses"] as List <cAddress>; int userId = (int)Session["UserID"]; //For each element of the original list perform one of the following: //If update, update information //If delete, mark record for delete //If new, mark record for adding cAddress a1 = null; if (Demography.UserAddresses != null) { foreach (cAddress a in Demography.UserAddresses) //state in database { a1 = addresses.FirstOrDefault(x => x.AddressID == a.AddressID); //If record not found in memory that means it was deleted if (a1 == null) { a.SaveUpdate(userId, true); } else { a1.SaveUpdate(userId); } } } a1 = addresses.FirstOrDefault(x => x.AddressID <= 0); if (a1 != null && a1.IsValid()) // If new and valid, let push it to the database { a1.SaveUpdate(userId); } }
protected void gv_Address_RowUpdating(object sender, GridViewUpdateEventArgs e) { List <cAddress> addresses = null; int gindex = e.RowIndex; cAddress address = new cAddress(); GridView gv = (GridView)sender; addresses = Session["dem_Addresses"] as List <Classes.cAddress>; if (gindex < addresses.Count()) { address.StrAddress1 = ((gv.Rows[gindex].FindControl("gv_txtAddress1") as TextBox).Text + string.Empty).Trim(); address.StrAddress2 = ((gv.Rows[gindex].FindControl("gv_txtAddress2") as TextBox).Text + string.Empty).Trim(); address.StrCity = ((gv.Rows[gindex].FindControl("gv_txtCity") as TextBox).Text + string.Empty).Trim(); address.StrStateID = ((gv.Rows[gindex].FindControl("gv_txtState") as TextBox).Text + string.Empty).Trim(); address.StrPostalCode = ((gv.Rows[gindex].FindControl("gv_txtZipCode") as TextBox).Text + string.Empty).Trim(); address.StrCountry = ((gv.Rows[gindex].FindControl("gv_txtCountry") as TextBox).Text + string.Empty).Trim(); int iRetVal = 0; int.TryParse((gv.Rows[gindex].FindControl("ddAddressType") as DropDownList).SelectedValue, out iRetVal); //only native types can be returned so temp variable address.IntAddressTypeID = iRetVal; address.IsPrimary = (gv.Rows[gindex].FindControl("rbtnPrimary") as RadioButton).Checked; if (address.IsValid()) { addresses[gindex].StrAddress1 = address.StrAddress1; addresses[gindex].StrAddress2 = address.StrAddress2; addresses[gindex].StrCity = address.StrCity; addresses[gindex].StrStateID = address.StrStateID; addresses[gindex].StrPostalCode = address.StrPostalCode; addresses[gindex].StrCountry = address.StrCountry; addresses[gindex].IntAddressTypeID = address.IntAddressTypeID; addresses[gindex].IsPrimary = address.IsPrimary; if (addresses[gindex].IntAddressID < 1)//Always make sure that there is an extra records to edit, since there is not option to add records { addresses.Add(new cAddress()); } //if the primary record is checked then all other record must not be ckeched if (addresses[gindex].IsPrimary) { addresses.ForAll(x => x.IsPrimary = false); addresses[gindex].IsPrimary = true; } Session["dem_Addresses"] = addresses; gv.Rows[gindex].RowState = DataControlRowState.Normal; gv_Address.EditIndex = -1; } else { lblMessage.Text = address.strErrorDescription; e.Cancel = true; } } BindAllGrids(); }
protected void gvAddresses_RowUpdating(object sender, GridViewUpdateEventArgs e) { List <cAddress> clAddresses = Session["dem_Addresses"] as List <cAddress>; if (e.RowIndex < clAddresses.Count) { cAddress UpdatedAddress = clAddresses[e.RowIndex]; TextBox tbAddress1 = (TextBox)gvAddresses.Rows[e.RowIndex].FindControl("tbAddress1"); if (tbAddress1 != null) { UpdatedAddress.Address1 = tbAddress1.Text; } TextBox tbCity = (TextBox)gvAddresses.Rows[e.RowIndex].FindControl("tbCity"); if (tbCity != null) { UpdatedAddress.City = tbCity.Text; } TextBox tbPostalCode = (TextBox)gvAddresses.Rows[e.RowIndex].FindControl("tbPostalCode"); if (tbPostalCode != null) { UpdatedAddress.PostalCode = tbPostalCode.Text; } if (!UpdatedAddress.IsValid()) { lblMessage.Text = UpdatedAddress.strErrorDescription; e.Cancel = true; } else { TextBox tbAddress2 = (TextBox)gvAddresses.Rows[e.RowIndex].FindControl("tbAddress2"); if (tbAddress2 != null) { UpdatedAddress.Address2 = tbAddress2.Text; } TextBox tbState = (TextBox)gvAddresses.Rows[e.RowIndex].FindControl("tbState"); if (tbState != null) { UpdatedAddress.StateID = tbState.Text; } TextBox tbCountry = (TextBox)gvAddresses.Rows[e.RowIndex].FindControl("tbCountry"); if (tbCountry != null) { UpdatedAddress.Country = tbCountry.Text; } DropDownList ddlAddressType = (DropDownList)gvAddresses.Rows[e.RowIndex].FindControl("ddlAddressType"); if (ddlAddressType != null) { UpdatedAddress.AddressTypeID = ddlAddressType.SelectedValue.ToInt32(); } RadioButton rbPrimary = (RadioButton)gvAddresses.Rows[e.RowIndex].FindControl("rbPrimary"); if (rbPrimary != null) { if (rbPrimary.Checked) { clAddresses.ForAll(x => x.IsPrimary = false); UpdatedAddress.IsPrimary = true; } } Session["dem_Addresses"] = clAddresses; gvAddresses.EditIndex = -1; } BindAddresses(); } else { e.Cancel = true; } }