数据被移动而不是添加

时间:2017-05-20 13:28:16

标签: c# asp.net entities

我正在尝试创建将数据从一个订单复制到另一个订单的功能。以下是复制数据的代码:

 protected void copySeminarIDButton_Click(object sender, EventArgs e)
    {
        try
        {
            //show some message incase validation failed and return
            String seminarID = this.copySeminarIDText.Text;
            Int32 id;
            try
            {
                id = Convert.ToInt32(seminarID);
            } catch (Exception e2)
            {
                return;
            }

            //copy over all the registration types now for the entered id
            using (SeminarGroupEntities db = new SeminarGroupEntities())
            {
                var seminars = db.seminars.Where(x => x.id == id);
                if (seminars.Any())
                {
                    var s = seminars.First();

                    //Load RegTypes
                    try
                    {
                        var regList = s.seminar_registration_type
                            .OrderBy(x => x.sort)
                            .ToList()
                            .Select(x => new seminarRegistrationTypeListing
                            {
                                id = x.id,
                                amount = x.amount,
                                description = x.description,
                                feeTypeId = x.feeTypeId,
                                method = x.method,
                                promoCodes = PromoCodesToString(x.xref_reg_type_promo.Select(z => z.promoId).ToList()),
                                title = x.title,
                                isPreorder = x.isPreorder,
                                sort = x.sort
                            });

                        this.dlTypes.DataSource = regList;
                        this.dlTypes.DataBind();
                    }
                    catch (Exception ex)
                    {
                        Response.Write("RegTypes: " + ex.Message);
                    }
                } else
                {
                    return;
                }
            }
        }
        catch (Exception m)
        {

        }
    }

用户将输入要从中复制的发票的ID。上述代码副本从发票中提取正确的数据并将其加载到页面上。当我单击保存时,正在保存数据。

正在发生的问题是我复制数据的发票将不再拥有数据。因此,基本上不是复制数据,而是实际将数据移动到新发票。我想复制。

这是我的Save代码。

        private void SaveRegTypes(int seminarId)
    {
        //Reg Types
        using (SeminarGroupEntities db = new SeminarGroupEntities())
        {
            try
            {
                var s = db.seminars.Where(x => x.id == seminarId).First();
                var msg = "alert('Saving:" + seminarId + "');";
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "Data Copied...", msg, true);
                this.copySeminarIDText.Text = msg;

                //s.seminar_registration_type.Clear();
                foreach (DataListItem dli in this.dlTypes.Items)
                {
                    String sId = ((HiddenField)dli.FindControl("hdnId")).Value;
                    var reg = new seminar_registration_type();
                    if ((sId != "") && (sId != "0"))
                    {
                        Int32 id = Convert.ToInt32(sId);
                        reg = db.seminar_registration_type.Where(x => x.id == id).Single();
                        ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "Data Copied...", "alert('hidded field id is empty.');", true);
                    }
                    else
                    {
                        db.seminar_registration_type.Add(reg);
                    }

                    reg.feeTypeId = Convert.ToInt32(((DropDownList)dli.FindControl("ddlRegType")).SelectedItem.Value);
                    reg.amount = Convert.ToDecimal(((TextBox)dli.FindControl("txtPrice")).Text);
                    reg.title = ((DropDownList)dli.FindControl("ddlRegType")).SelectedItem.Text;
                    reg.description = ((TextBox)dli.FindControl("txtRegDesc")).Text;
                    reg.method = Convert.ToInt32(((DropDownList)dli.FindControl("ddlDeliveryMethod")).Text);
                    reg.promocode = null;
                    reg.isPreorder = Convert.ToBoolean(((CheckBox)dli.FindControl("chkPreorder")).Checked);
                    reg.sort = 1;
                    reg.seminarId = seminarId;

                    string sort = ((TextBox)dli.FindControl("txtRTSort")).Text;
                    try
                    {
                        reg.sort = Convert.ToInt32(sort);
                    }
                    catch (Exception ex) { }

                    //Do Promo Codes


                    Repeater rptPromocodes = (Repeater)dli.FindControl("rptPromoCodesList");
                    reg.xref_reg_type_promo.Clear();
                    foreach (RepeaterItem ri in rptPromocodes.Items)
                    {
                        try
                        {
                            Int32 id = Convert.ToInt32(((HiddenField)ri.FindControl("hdnCodeId")).Value);
                            DateTime? expires = null;

                            try
                            {
                                HiddenField exp = (HiddenField)ri.FindControl("hdnExpirationDate");
                                if (!String.IsNullOrWhiteSpace(exp.Value))
                                    expires = Convert.ToDateTime(((HiddenField)ri.FindControl("hdnExpirationDate")).Value);
                            }
                            catch (Exception ex) { }

                            var code = db.promo_code.Where(x => x.id == id).First();
                            reg.xref_reg_type_promo.Add(new xref_reg_type_promo
                            {
                                expiration = expires,
                                promoId = code.id
                            });
                        }
                        catch (Exception ex) { }
                    }

                    db.SaveChanges();
                    ((HiddenField)dli.FindControl("hdnId")).Value = reg.id.ToString();

                }

            }
            catch (Exception ex) 
            {
                String err = ex.Message;
            }
        } 

    }

0 个答案:

没有答案