I want to save images in the database in the following way controller:
[HttpPost]
public ActionResult AddMovie(Movie movie, HttpPostedFileBase Image)
{
if (Image != null)
{
using (Stream inputStream = Image.InputStream)
{
MemoryStream memoryStream = inputStream as MemoryStream;
if (memoryStream == null)
{
memoryStream = new MemoryStream();
inputStream.CopyTo(memoryStream);
}
movie.Image = memoryStream.ToArray();
}
}
else
movie.Image = null;
if (ModelState.IsValid)
{
obllmovie.AddMovie(movie);
return RedirectToAction("Index");
}
return View();
}
View:
@using (Html.BeginForm("AddMovie", "Movie", FormMethod.Post, new { enctype = "multipart/form-data" }))
<div class="row">
@Html.LabelFor(m => m.Image, new { @class = "col-md-2 col-form-label" })
<div class="col-md-10">
<input type="file" name="Image">
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-outline-dark" />
</div>
</div>
If in the view I don't put any parameter the Image is empty, but if I write the parameters the app throws the following error:
Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded.
I want to save images in the database in the following way controller:
[HttpPost]
public ActionResult AddMovie(Movie movie, HttpPostedFileBase Image)
{
if (Image != null)
{
using (Stream inputStream = Image.InputStream)
{
MemoryStream memoryStream = inputStream as MemoryStream;
if (memoryStream == null)
{
memoryStream = new MemoryStream();
inputStream.CopyTo(memoryStream);
}
movie.Image = memoryStream.ToArray();
}
}
else
movie.Image = null;
if (ModelState.IsValid)
{
obllmovie.AddMovie(movie);
return RedirectToAction("Index");
}
return View();
}
View:
@using (Html.BeginForm("AddMovie", "Movie", FormMethod.Post, new { enctype = "multipart/form-data" }))
<div class="row">
@Html.LabelFor(m => m.Image, new { @class = "col-md-2 col-form-label" })
<div class="col-md-10">
<input type="file" name="Image">
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-outline-dark" />
</div>
</div>
If in the view I don't put any parameter the Image is empty, but if I write the parameters the app throws the following error:
Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded.
Share Improve this question edited Jan 19 at 13:35 marc_s 754k184 gold badges1.4k silver badges1.5k bronze badges asked Jan 19 at 13:30 StefanIulianStefanIulian 12 bronze badges1 Answer
Reset to default 0In database make sure image property have maximum size and try this code.
[HttpPost]
public ActionResult AddMovie(Movie movie, HttpPostedFileBase Image)
{
try
{
if (Image != null && Image.ContentLength > 0)
{
// Convert image into byte array
using (var memoryStream = new MemoryStream())
{
Image.InputStream.CopyTo(memoryStream);
movie.Image = memoryStream.ToArray();
}
}
if (ModelState.IsValid)
{
// Save to database
return RedirectToAction("Index");
}
// Model fail return
return View(movie);
}
catch (Exception ex)
{
// Handle any errors
return View(movie);
}
}
//Make a movie model which will have Image property:
public class Movie
{
// XYZ properties...
public byte[] Image { get; set; }
}
You can visit here for more details