public List <ActorMovieConnection> GetConnectionPaths(List <string[]> actorList, List <string[]> movieList, string cultureCode) { var actorMovieConnections = new List <ActorMovieConnection>(); for (int outerIndex = 0; outerIndex < actorList.Count; outerIndex++) { var actorMovieConnection = new ActorMovieConnection { ActorMovieInfo = new List <ActorMoviePath>() }; var actorMoviePathList = new List <ActorMoviePath>(); for (int innerIndex = 0; innerIndex < actorList[outerIndex].ToList().Count; innerIndex++) { var movieIdString = movieList[outerIndex][innerIndex]; int movieId; string movieName = string.Empty; if (Int32.TryParse(movieIdString, out movieId)) { movieName = _actorMovieService.Entities.FirstOrDefault(x => x.MovieId == movieId)? .Movie?.CulturedEntities?.FirstOrDefault( x => x.Culture.CultureCode == cultureCode && x.Status == Status.Active)?.Title; } var actorIdString = actorList[outerIndex][innerIndex]; int actorId; string actorName = string.Empty; if (Int32.TryParse(actorIdString, out actorId)) { actorName = _actorMovieService.Entities.FirstOrDefault(x => x.ActorId == actorId)? .Actor?.Name; } var movieInfo = new MovieInfo(movieId, movieName); var actorInfo = new ActorInfo(actorId, actorName); var actorMoviePath = new ActorMoviePath(actorInfo, movieInfo); actorMoviePathList.Add(actorMoviePath); } actorMovieConnection.ActorMovieInfo.AddRange(actorMoviePathList); actorMovieConnections.Add(actorMovieConnection); } return(actorMovieConnections); }
//Add new Movie protected void btnSubmit_Click(object sender, EventArgs e) { //when ever we need to insert data in multiple time we need to use transaction if any error occurs data will // not be inserted in any table DataClasses1DataContext dataContext = new DataClasses1DataContext(); DbTransaction transaction = null; try { dataContext.Connection.Open(); transaction = dataContext.Connection.BeginTransaction(); dataContext.Transaction = transaction; var movieList = (from d in dataContext.Movies where d.Name.ToUpper().Trim() == txtMovieName.Text.ToUpper().Trim() select d); if (movieList.Any()) { throw new Exception("Movie already addedd"); } Movie mov = new Movie(); if (!string.IsNullOrWhiteSpace(txtMovieName.Text)) { mov.Name = txtMovieName.Text.Trim(); } else { throw new Exception("Movie name is required"); } //year of release will in format 2012 mov.YearOfRelease = txtYearOfRelease.Text.Trim(); mov.Plot = txtPlot.Text.Trim(); //Posture of Movie if (fuImage.HasFile) { Random rad = new Random(); rad.Next(0, 9999); string fileName = rad.Next() + Path.GetExtension(fuImage.PostedFile.FileName); Stream stream = fuImage.PostedFile.InputStream; System.Drawing.Image img = System.Drawing.Image.FromStream(stream); mov.Poster = fileName; fuImage.SaveAs(Server.MapPath("~/images/" + fileName)); } if (ddlProducers.SelectedValue != "0") { mov.ProducerId = Convert.ToInt32(ddlProducers.SelectedValue); } else { throw new Exception("Producer is required."); } dataContext.Movies.InsertOnSubmit(mov); dataContext.SubmitChanges(); if (ViewState["actorDetails"] != null) { List <ActorDetails> list2 = ViewState["actorDetails"] as List <ActorDetails>; //Add Actor and movie relations one by one foreach (var item in list2) { ActorMovieConnection acm = new ActorMovieConnection(); acm.ActorId = item.ActorId; acm.MovieId = mov.MovieId; acm.ConnectionStatus = (byte)ConnectionStatus.Active; dataContext.ActorMovieConnections.InsertOnSubmit(acm); dataContext.SubmitChanges(); } } transaction.Commit(); lblMsg.Text = "Movie added successfully!!"; lblMsg.ForeColor = System.Drawing.Color.Green; Clear(); List <ActorDetails> list1 = new List <ActorDetails>(); grActorList.DataSource = list1; grActorList.DataBind(); } catch (Exception ex) { lblMsg.Text = ex.Message; lblMsg.ForeColor = System.Drawing.Color.Red; } }