I am connected with the ado.net entity model. I can retrieve information from the database using the Dropdownlist, but I am not able to commit changes (Insert) into the database. It previously works fine two days ago, but just stop working today. I have tried every trick in similar topics but no changes.
My Controlller:
public class RimySaleController : Controller
{
// GET: RimySale
// dropdownlist
public ActionResult RimSaleIndex()
{
newLayanDBEntities17 db = new newLayanDBEntities17();
List<Rim> list = db.Rims.ToList();
ViewBag.RimName = new SelectList(list, "rim_id", "rim_name");
List<Employee> lists = db.Employees.ToList();
ViewBag.EmpName = new SelectList(lists, "emp_id", "emp_name");
List<Customer> listp = db.Customers.ToList();
ViewBag.CustName = new SelectList(listp, "cust_id", "cust_name");
return View();
}
public ActionResult RimSaleSave(RimSale model)
{
try
{
newLayanDBEntities17 db = new newLayanDBEntities17();
Rim_Sale addRim = new Rim_Sale();
addRim.Date = model.Date;
addRim.cust_int = model.Cust_int;
addRim.emp_id = model.Emp_id;
addRim.rim_id = model.Rim_id;
addRim.rim_sold_quantity = model.Rim_sold_quantity;
addRim.rim_sold_unit_price = model.Rim_sold_unit_price;
//making the changes
db.Rim_Sale.Add(addRim);
db.SaveChanges();
}
catch (Exception ex)
{
throw ex;
}
return RedirectToAction("RimSaleIndex");
}
}
}
My View:
@using (Html.BeginForm("SaveBattSale", "BatterySale", FormMethod.Post))
{
<br />
@Html.TextBoxFor(model => model.Sold_date, new { @type = "Date", @class = "form-control" })
<br />
@Html.DropDownListFor(model => model.Bat_id, ViewBag.BattName as SelectList, "--Select Battery--", new { @class = "form-control" })
<br />
@Html.TextBoxFor(model => model.Batt_sold_quantity, new { @type = "number", @placeholder = "Type Quantity Sold", @class = "form-control " })
<br />
@Html.TextBoxFor(model => model.Batt_unit_price, new { @placeholder = "Price Paid for One Battery", @type = "number", @class = "form-control" })
<br />
@Html.DropDownListFor(model => model.Cust_id, ViewBag.CustName as SelectList, "--Select Customer--", new { @class = "form-control" })
<br />
@Html.DropDownListFor(model => model.Emp_id, ViewBag.EmpName as SelectList, "--Select Employee--", new { @class = "form-control" })
<br />
<input type="Submit" value=" Submit" /> <input type="reset" value=" Reset" />
<br />
}
Your code looks ok. Try another one approach to insert data:
using (var db = new YourEntities())
{
Rim_Sale addRim = new Rim_Sale();
addRim.Date = model.Date;
addRim.cust_int = model.Cust_int;
addRim.emp_id = model.Emp_id;
addRim.rim_id = model.Rim_id;
addRim.rim_sold_quantity = model.Rim_sold_quantity;
addRim.rim_sold_unit_price = model.Rim_sold_unit_price;
db.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Rim_Sale] ON");
//making the changes
db.Rim_Sale.Add(addRim);
db.SaveChanges()
db.Entry(addRim).State = EntityState.Added;
db.SaveChanges();
db.Database.ExecuteSqlCommand("SET IDENTITY_INSERT [dbo].[Rim_Sale] OFF");
}
If data is not inserted, then there is a chance that you are inserting data to another database. If you use localDb, then make sure that a property of .mdf file in your solution as Copy to output Directory: "Copy only if newer", otherwise your db file will overwrite every time it runs.
UPDATE:
It is not a good way, but give a try:
db.Database.ExecuteSqlCommand(
"Insert into Rim_Sale Values(@Date, @cust_int, @emp_id, @rim_id,
@rim_sold_quantity , @rim_sold_unit_price )",
new SqlParameter("Date", Date),
new SqlParameter("cust_int", cust_int),
new SqlParameter("emp_id", emp_id),
new SqlParameter("rim_id", rim_id),
new SqlParameter("rim_sold_quantity ", rim_sold_quantity ),
new SqlParameter("rim_sold_unit_price ", rim_sold_unit_price ),
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With