Suppose, there is form of registration or a form of add data in database and you want to add multiple records at one attempt
then this post will help you to accomplish the task.
Step 1 :
Take a new Asp .Net MVC 4 Project and download the CsvHelper.dll , you can download this dll by the following way
Click on Tools --> Library Package Manager --> Package Manager Console
Now write Install-Package CsvHelper on Package Manager Console
like PM > Install-Package CsvHelper and enter
by this you will get the latest version of CsvHelper.
Step 2 :
Make a table for saving the record and add ADO .Net Entity Data Model by clicking on Add --> New Item
Step 3 :
In Model, write the following code for configure the CsvHelper and make a class for User Property
[DebuggerDisplay("Index = {Index}, Name = {Name}, Ignore = {Ignore}, ReferenceKey = {ReferenceKey}")] [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)] public class CsvFieldAttribute : Attribute { public virtual object Default { get; set; } public virtual string Format { get; set; } public virtual bool Ignore { get; set; } public virtual int Index { get; set; } public virtual string Name { get; set; } public virtual string[] Names { get; set; } public virtual string ReferenceKey { get; set; } } public class UserForCsv { [CsvField(Index = 0)] public string FirstName { get; set; } [CsvField(Index=1)] public string MiddleName { get; set; } [CsvField(Index = 2)] public string LastName { get; set; } [CsvField(Index = 3)] public string MobileNumber { get; set; } [CsvField(Index = 4)] public string EmailAddress { get; set; } [CsvField(Index = 5)] public string DOB { get; set; } }
Step 4 :
Now, In Controller write an HttpPost ActionResult with the following code for saving multiple record
[HttpPost] public ActionResult Index(HttpPostedFileBase userDetailCsvfile) { try { ICsvParser csvParser = new CsvParser(new StreamReader(userDetailCsvfile.InputStream)); var csvReader = new CsvReader(csvParser); var userData = csvReader.GetRecords<UserForCsv>().ToList(); foreach (var item in userData) { var user = new Models.User(); user.FirstName = item.FirstName; user.MiddleName = item.MiddleName; user.LastName = item.LastName; user.EmailAddress = item.EmailAddress; if(item.DOB!=string.Empty) user.DOB = Convert.ToDateTime(item.DOB); user.MobileNumber = item.MobileNumber; using (var context=new Models.UploadImageEntities()) { context.AddToUsers(user); context.SaveChanges(); } } ViewBag.Result =Alert.Success ; } catch (Exception) { ViewBag.Result = Alert.Error; } return View(); }
Step 5 :
In view i.e. Index.cshtml write the following code
@{ ViewBag.Title = "Index"; }<h2>Index</h2> @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) {<input type="file" name="userDetailCsvfile" id="userDetailCsvfile"/><br/><br/><input type="submit" value="Save"/><br/> @ViewBag.Result }
Step 6 :
Prepare the csvfile by which you will add record
Now, build the application and run.
You can download the solution from the following link :(attach bak file or make new table and find the csv file in the attached project)
AddMultipleRecordByCsv.zip (6.39 mb)