0

I have a problem with Entity Framework in Asp.net. I want to get the Id value whenever I add a record to the SQL database table. How can I do this? Here is my code:

StoredProcedure USE [Sapphiresworld] GO /****** Object: StoredProcedure [dbo].[addProduct] Script Date: 7/4/2018 10:25:41 AM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER Proc [dbo].[addProduct] @title nvarchar(250), @price nvarchar(50), @unitID int, @date nvarchar(50), @subCategoryID int, @brandID int, @vat int, @discount int, @picture nvarchar(50), @stock int as declare @MyVal int=0 INSERT into [PRODUCT] values (@title, @price , @unitID, @date, @subCategoryID , @brandID , @vat , @discount , @picture , @stock) Set @MyVal = @@IDENTITY Select @MyVal as Val 

Here is ClsBrand

 public string AddBrand(string brandName) { var result = new SapphiresworldEntities().AddBrand(brandName).ToString(); return result; } 

Code against Save Button

 protected void Btnsave_Click(object sender, EventArgs e) { clsBrand nb = new clsBrand(); string nwBran = nb.AddBrand(relig.Text); } 
3
  • I see no Entity Framworkz code in your example Commented Jul 4, 2018 at 6:21
  • Yes! there is no EF code. Plz describe more. Commented Jul 4, 2018 at 6:24
  • @TheGeneral what should i add more ? Commented Jul 4, 2018 at 6:25

4 Answers 4

2

Complete Solution

public class Student { public int Id { get; set; } public string Name { get; set; } } public async Task<IActionResult> Create([Bind("Id,Name")] Student student) { if (ModelState.IsValid) { _context.Add(student); await _context.SaveChangesAsync(); //Here your Id Value after insert record int StudentId = student.Id; return RedirectToAction(nameof(Index)); } return View(student); } 

Hope you understand and it will be helpful for you.

Sign up to request clarification or add additional context in comments.

Comments

2

After inserting a record, the Id value will automatically populate. You just take the value like from objectname.Id.

2 Comments

I need more information. where and What should I add in this code. I want to return the ID of the newly inserted column and save it in nwBran
Please check my another answer in your question. There is an example of Entity Framework.
1

Once you call the save changes methods in Entity framework, ID will automatically be populated(Assuming that you have ID as identity column in you table)

2 Comments

I need more information. where and What should I add in this code. I want to return the ID of the newly inserted column and save it in nwBran .
You do not need to select @@IDENTITY in you insert stored procedure. Just make the unique column of your table as identity column(or add on ID column in table and make it identity) and insert a row in table. for example: var cust = new Customer(); context.Customers.Add(cust); context.SaveChanges(); // generates Id for customer string nwBran = cust.Id; // now you can get the Id(identity) of inserted object
0

As per the above answers the id value will automatically populate after record will insert.

Here is the example.

your DataModel class ex.

public class MyDataModel { [Key] public int Id {get; set;} public string TestData {get; set;} } 

and when you are going to insert your data using entity framework use this

public bool SaveMethodInDataBase(MyDataModel myDataModel) { MyDataModel result = _dbContext.MyDataModel.Add(myDataModel); _dbContext.SaveChanges(); } 

then this result will return data with Id. in here _dbContext is your EntityFrameWork SPContext if you created.

Try This.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.