public ActionResult UploadCsvFile(string filePath) { // Checking no of files injected in Request object if (Request.Files.Count > 0) { try { string VideoList = ""; string Title = ""; string Description = ""; string Genre = ""; string RentalPrice = ""; //string Date; DateTime dateTime = DateTime.UtcNow.Date; // Get all files from Request object HttpFileCollectionBase files = Request.Files; for (int i = 0; i < files.Count; i++) { HttpPostedFileBase file = files[i]; string fName; // Checking for Internet Explorer if (Request.Browser.Browser.ToUpper() == "IE" || Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER") { string[] testfiles = file.FileName.Split(new char[] { '\\' }); fName = testfiles[testfiles.Length - 1]; } else { fName = file.FileName; } // Get the complete folder path and store the file inside it. XmlDocument configxml = new XmlDocument(); configxml.Load(AppDomain.CurrentDomain.BaseDirectory + "/Config.xml"); fName = configxml.GetElementsByTagName("UploadLocation").Item(0).InnerXml + fName; fName = fName.Replace(@"\\", @"\"); file.SaveAs(fName); DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[8] { new DataColumn("Title", typeof(string)), new DataColumn("Description", typeof(string)), new DataColumn("Genre", typeof(string)), new DataColumn("RentalPrice", typeof(string)), new DataColumn("Status", typeof(string)), new DataColumn("UserAdded", typeof(string)), new DataColumn("DateAdded", typeof(string)), new DataColumn("RentalStatus", typeof(string)) }); string csvData = System.IO.File.ReadAllText(fName); List <AddVideo> lists = new List <AddVideo>(); AddVideo videoList = new AddVideo(); //Excute a loop over the rows int RowCount = 0; foreach (string row in csvData.Split('\n').Skip(1)) { if (!string.IsNullOrEmpty(row)) { dt.Rows.Add(); int a = 0; //Execute a loop the columns foreach (string cellValue in row.Split(';')) { string cell = cellValue.Replace("\r\n", "").Replace("\r", "").Replace("\n", ""); if (RowCount == 0) { if (a == 0) { Title = cell; } else if (a == 1) { Description = cell; } else if (a == 2) { Genre = cell; } else if (a == 3) { RentalPrice = cell; } } dt.Rows[dt.Rows.Count - 1][a] = cell; a++; } RowCount++; videoList.Title = Title; videoList.Description = Description; videoList.Genre = Genre; videoList.RentalPrice = RentalPrice; } lists.Add(videoList); } Constants constants = new Constants(); using (SqlConnection con = new SqlConnection(constants.connectionString)) { foreach (var item in lists) { CoreVideoListAdd add = new CoreVideoListAdd(); add.InsertVideo(item.Title, item.Description, item.Genre, item.RentalPrice); } con.Close(); //using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con)) //{ // // Set the datatbase table name // //sqlBulkCopy.DestinationTableName = "[dbo].[VideoAdministration]"; // //sqlBulkCopy.ColumnMappings.Add("Title","Title"); // //sqlBulkCopy.ColumnMappings.Add("Description", "Description"); // //sqlBulkCopy.ColumnMappings.Add("Genre", "Genre"); // //sqlBulkCopy.ColumnMappings.Add("RentalPrice", "RentalPrice"); // ////sqlBulkCopy.ColumnMappings.Add("Status", "1"); // ////sqlBulkCopy.ColumnMappings.Add("UserAdded", "null"); // ////sqlBulkCopy.ColumnMappings.Add("DateAdded", dateTime.ToString()); // ////sqlBulkCopy.ColumnMappings.Add("RentalStatus", "Available"); // //con.Open(); // //sqlBulkCopy.WriteToServer(dt); // con.Close(); //} } } return(Json("File Uploaded Successfully!")); } catch (Exception ex) { return(Json("Error occurred. Error details: " + ex.Message)); } } return(Json("No files selected.")); }
public ActionResult AddNewVideo(string Title, string Description, string Genre, string RentalPrice) { CoreVideoListAdd core = new CoreVideoListAdd(); return(Json(core.InsertVideo(Title, Description, Genre, RentalPrice), JsonRequestBehavior.AllowGet)); }