Blurryface213
Blurryface213

Reputation: 93

"Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index

I currently studying web development using asp.net mvc5, now working on my school project. I would like to store and display image from database. I encountered this error.

here is the screenshot

Here is my code this is the controller:

public ActionResult AddItems()
{
    return View();
}

[HttpPost]
public ActionResult AddItems(FormCollection form)
{
    StoreItems i = new StoreItems();
    //i.ID = int.Parse(form["AlbumID"]);
    i.AlbumName = form["AlbumName"];
    i.Artist = form["AlbumArtist"];
    i.Genre = form["AlbumGenre"];
    i.DateReleased = DateTime.Parse(form["AlbumDateReleased"]);
    i.Price = int.Parse(form["AlbumPrice"]);
    i.Downloads = int.Parse(form["AlbumDownloads"]);
    i.Listens = int.Parse(form["AlbumListens"]);
    i.RecordLabel = form["RecordLabel"];
    HttpPostedFileBase file = Request.Files[0];
    i.PicturePath = file.FileName.ToString();

    DAL.AddItems(i);
    return RedirectToAction("ItemLists");
}

And here is the model:

public static void AddItems(StoreItems i)
{

    byte[] bytes;
    if (string.IsNullOrEmpty(i.PicturePath))
    {
        string filename = System.Web.HttpContext.Current.Server.MapPath("~/Content/Images/default-artwork.png");
        bytes = System.IO.File.ReadAllBytes(filename);
    }

    else
    {
        string filename = i.PicturePath;
        bytes = System.IO.File.ReadAllBytes(filename);


    }


    SqlConnection con = new SqlConnection(DAL.cs);
    con.Open();
    SqlCommand com = new SqlCommand(
        "INSERT INTO AlbumsTb ( AlbumName, Artist, Genre, DateReleased, Price, Downloads, Listens, RecordLabel, DateAdded, AlbumArt) VALUES( @AlbumName, @Artist, @Genre, @DateReleased, @Price, @Downloads, @Listens, @RecordLabel, @DateAdded, @AlbumArt)", con);
    //com.Parameters.AddWithValue("@ID", i.ID);
    com.Parameters.AddWithValue("@AlbumName", SqlDbType.VarChar).Value = i.AlbumName;
    com.Parameters.AddWithValue("@Artist", SqlDbType.VarChar).Value = i.Artist;
    com.Parameters.AddWithValue("@Genre", SqlDbType.VarChar).Value = i.Genre;
    com.Parameters.AddWithValue("@DateReleased", SqlDbType.Date).Value = i.DateReleased;
    com.Parameters.AddWithValue("@Price",i.Price);
    com.Parameters.AddWithValue("@Downloads", i.Downloads);
    com.Parameters.AddWithValue("@Listens", i.Listens);
    com.Parameters.AddWithValue("@RecordLabel", SqlDbType.VarChar).Value = i.RecordLabel;
    com.Parameters.AddWithValue(@"DateAdded", DateTime.Now.ToString());

    com.Parameters.AddWithValue("@AlbumArt", SqlDbType.VarBinary).Value = bytes;

    com.ExecuteNonQuery();
    con.Close();

}

Upvotes: 2

Views: 4573

Answers (1)

Nkosi
Nkosi

Reputation: 247068

There is no file posted with the request. Which means the array is empty. Hence the index out of range error. You should also practice defensive coding and check to make sure the array is populated. If the file is mandatory then you can gracefully error out and return relevant error message (BadRequest..etc)

[HttpPost]
public ActionResult AddItems(FormCollection form)
{
    StoreItems i = new StoreItems();
    //i.ID = int.Parse(form["AlbumID"]);
    i.AlbumName = form["AlbumName"];
    i.Artist = form["AlbumArtist"];
    i.Genre = form["AlbumGenre"];
    i.DateReleased = DateTime.Parse(form["AlbumDateReleased"]);
    i.Price = int.Parse(form["AlbumPrice"]);
    i.Downloads = int.Parse(form["AlbumDownloads"]);
    i.Listens = int.Parse(form["AlbumListens"]);
    i.RecordLabel = form["RecordLabel"];
    var files = Request.Files;
    if(files.Count > 0) {
        var file = files[0];
        i.PicturePath = file.FileName.ToString();
    } else {
        //...return some error code or validation message.
    }

    DAL.AddItems(i);
    return RedirectToAction("ItemLists");

}

Upvotes: 2

Related Questions