通过组合框在数据库中添加新项

时间:2012-10-11 07:16:05

标签: asp.net entity-framework combobox

我有一个数据库,我通过实体框架使用它,我需要通过编辑文本框和网格中的其他项来更新数据,这是正常工作,下面是代码: 在aspx.cs中

protected void SaveBtn_Click(object sender, EventArgs e)
    {
        obj.Address = AppAddress.Text;
        obj.City = AppCity.Text;
        obj.Email = AppEmail.Text;
        obj.Notes = AppComments.Text;
        obj.Postal = AppPostal.Text;
        obj.AppraiserAppraiserCompanyId = ApprCompCmbx.SelectedIndex;
        obj.ProvinceState = Province.SelectedIndex;
        apprblobj.GetUpdate(obj);
        Response.Write("<script>alert('You have successfully updated the Data');</script>");
}

打电话给BL:

 public void GetUpdate(Appraiser appObj)
    {
        obj.UpdateData(appObj);

    }

并在DAL中

 public void UpdateData(Appraiser apprObj)
    {

        try
        {
            var Appsave = context.Appraisers.FirstOrDefault(App => App.AppraiserId == apprObj.AppraiserId);
            if (Appsave != null)
            {
                Appsave.AppraiserName = apprObj.AppraiserName;
                Appsave.AppraiserAppraiserCompanyId = apprObj.AppraiserAppraiserCompanyId;
                Appsave.Address = apprObj.Address;
                Appsave.City = apprObj.City;
                Appsave.ProvinceState = apprObj.ProvinceState;
                Appsave.Email = apprObj.Email;
                Appsave.Postal = apprObj.Postal;
                Appsave.Notes = apprObj.Notes;

                context.SaveChanges();
            }
        }
        catch (Exception ex)
        {

             log.Debug("Appraiser : UpdateData: " + ex.Message + " Trace : " + ex.StackTrace);
        }
    }

现在我想通过相同的组合框和按钮点击功能添加新项目,因为它正在执行添加,更新和删除等所有工作。 请提供一下提示:我知道addobject( _ _)将用于代替savechanges等。

0 个答案:

没有答案