1

I have FormView in Category_New.aspx from where new item is inserted. It is inserted in following method.

 public void myForm_InsertItem()
    {            
        var item = new A.Models.Category();
        CategoryContext db = new CategoryContext();            
        TryUpdateModel(item);
        if (ModelState.IsValid)
        {
            // Save changes here
            db.Category.Add(item);
            db.SaveChanges();
            //item.CategoryID is present from this point
        }
    }

I would like to redirect a user to the page that is for editing that Item. That page is Category_Edit.aspx.

How to get ID of inserted item in method myForm_ItemInserted so that the following code would work?

protected void myForm_ItemInserted(object sender, FormViewInsertedEventArgs e)
{
     Response.RedirectToRoute(GetRouteUrl("CategoryEdit", new {CategoryID = /* how to get ID of inserted item??? */}));            
}

How to know the ID of inserted item?

2 Answers2

1

What you probably can do is to have a global int type variable to hold the value of new category Id, then you can pass it in in your redirect method.

private int newCategoryId;
public void myForm_InsertItem()
    {            
        var item = new A.Models.Category();
        CategoryContext db = new CategoryContext();            
        TryUpdateModel(item);
        if (ModelState.IsValid)
        {
            // Save changes here
            db.Category.Add(item);
            db.SaveChanges();
            //item.CategoryID is present from this point
            newCategoryId = item.CategoryId; // presumably it's called categoryId
        }
    }

protected void myForm_ItemInserted(object sender, FormViewInsertedEventArgs e)
{
     Response.RedirectToRoute(GetRouteUrl("CategoryEdit", new {CategoryID = newCategoryId}));            
}

The key here is that when inserting, the generated ID is saved into the instance of the object being saved.

woodykiddy
  • 6,074
  • 16
  • 59
  • 100
  • I was thinking to put categoryid to session. Then in method myForm_ItemInserted retrieve categoryid from session, redirect, clear the categoryid from session. I am just wondering what is the convenient way in webforms to redirect to edit after insert. – Developer Marius Žilėnas Apr 15 '16 at 16:42
  • @Willmore If you don't like to use Session, then you can consider ViewState, which allows you to persist values through postback. That way you can pass this value in your redirect method. Alternatively, using HiddenField control can be another option. – woodykiddy Apr 18 '16 at 08:56
1

If you are using entity framework, It will persist save object auto generated id at the time data inserted to the database.

Normally entity framework execute SELECT SCOPE_IDENTITY() to get when auto-generated Ids.

Therefore you should be able to get inserted id as following.

int Id= item.ID; 
Telan Niranga
  • 437
  • 3
  • 10
  • ID is available :). Just don't know how to access item variable in method myForm_ItemInserted. How do you do it? – Developer Marius Žilėnas Apr 17 '16 at 09:52
  • 1
    Define a variable in class level like follow. Then use that variable for the ItemInserted method. Int categoryId=0; public void myForm_InsertItem() { //Data insert code. } protected void myForm_ItemInserted(object sender, FormViewInsertedEventArgs e) { Response.RedirectToRoute(GetRouteUrl("CategoryEdit", new {CategoryID = categoryId })); } – Telan Niranga Apr 18 '16 at 13:44