Please help me on this to resolve
My code as below
View
@model TESTInternetMVC.ADONET_DAL.Read
@{
ViewBag.Title = "Edit";
}
Edit
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
}
@Html.ActionLink("Back to List", "Index")
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Model
public class Read
{
[Required]
public string productname;
[Required, DataType(DataType.Currency)]
public decimal price;
[Required]
public string description;
public int id;
Model also has this method for other operations
public IEnumerable read()
{
SqlConnection cn = DataAccess.sqlcn();
string q = "select id,productname,price,description from testinternetmvc..products";
//SqlCommand cmd = new SqlCommand(q, cn);
cn.Open();
SqlDataAdapter sda = new SqlDataAdapter(q, cn);
//DataSet ds = new DataSet();
DataTable dt=new DataTable();
//ds.Clear();
//sda.Fill(ds.Tables[0]);
sda.Fill(dt);
//ds = cmd.ExecuteReader();
//SqlDataReader d = cmd.ExecuteReader();
cn.Close();
IEnumerable dtlist = from r in dt.AsEnumerable()
select new Read { productname=r["productname"].ToString(),
price=Convert.ToDecimal(r["price"]),
description=r["description"].ToString(),
id=Convert.ToInt32(r["id"])
};
//List dtlist = dt.AsEnumerable().Select(row => new Read
//{
// productname = row.Field("productname"),
// price = row.Field("price"),
// description = row.Field("description")
//}).ToList();
return dtlist;
}
Controller
[HttpGet]
public ActionResult Edit(int id)
{
Read rd = new Read();
Read data = rd.read().SingleOrDefault(d => d.id == id);
return View(data);
}
//
// POST: /ADONET/Edit/5
[HttpPost]
public ActionResult Edit(Read editeddata) //int id, FormCollection collection)
{
Response.Write(editeddata.id);
Response.Write(editeddata.description);
Response.Write(editeddata.price);
Response.Write(editeddata.productname);
try
{
if (ModelState.IsValid)
{
// TODO: Add update logic here
SPUpdate spu = new SPUpdate();
spu.Update(editeddata);
return RedirectToAction("Index");
}
else
return RedirectToAction("Index");
}
catch
{
return View();
}
}
Update method(Intended ADO.NET operations on posted data)
public class SPUpdate
{
//OPTION 1 - DIRECT UPDATE QUERY
public void Update(Read editeddata)
{
SqlConnection cn = DataAccess.sqlcn();
string q = "Update testinternetmvc..products set productname=@productname ,price=@price, description=@description where id=@oid";
SqlDataAdapter sda = new SqlDataAdapter(q,cn);
//sda.UpdateCommand.CommandText=q;
sda.UpdateCommand.Parameters.AddWithValue("@oid",editeddata.id);
sda.UpdateCommand.Parameters.AddWithValue("@productname",editeddata.productname);
sda.UpdateCommand.Parameters.AddWithValue("@price",editeddata.price);
sda.UpdateCommand.Parameters.AddWithValue("@description",editeddata.description);
//DataTable dt=new DataTable();
//sda.Fill(dt);
cn.Open();
sda.UpdateCommand.ExecuteNonQuery();
cn.Close();
}
}
Vincent Maverick DuranoPosted May 5, 2015, 3:54 PM