Click here to Skip to main content
15,905,875 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
hi, there I am following this article to upload csv file into sql server database but this tutorial in aspx mvc2.. so I am not able to make it work.. also it is giving me several exceptions.

http://arranmaclean.wordpress.com/2010/07/20/net-mvc-upload-a-csv-file-to-database-with-bulk-upload/[^]

HomeController Code


C#
[HttpPost]
       public ActionResult Index(HttpPostedFileBase FileUpload)
       {

           DataTable dt = new DataTable();


           if (FileUpload.ContentLength > 0)
           {

               string fileName = Path.GetFileName(FileUpload.FileName);
               string path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);


               try
               {
                   FileUpload.SaveAs(path);

                   dt = ProcessCSV(path);


                   ViewData["Feedback"] = ProcessBulkCopy(dt);
               }
               catch (Exception ex)
               {

                   ViewData["Feedback"] = ex.Message;
               }
           }
           else
           {

               ViewData["Feedback"] = "Please select a file";
           }


           dt.Dispose();

           return View("Index", ViewData["Feedback"]);
       }

       private static DataTable ProcessCSV(string fileName)
       {

           string Feedback = string.Empty;
           string line = string.Empty;
           string[] strArray;
           DataTable dt = new DataTable();
           DataRow row;


           Regex r = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");


           StreamReader sr = new StreamReader(fileName);


           line = sr.ReadLine();
           strArray = r.Split(line);


           Array.ForEach(strArray, s => dt.Columns.Add(new DataColumn()));



           while ((line = sr.ReadLine()) != null)
           {
               row = dt.NewRow();


               row.ItemArray = r.Split(line);
               dt.Rows.Add(row);
           }


           sr.Dispose();


           return dt;


       }


       private static String ProcessBulkCopy(DataTable dt)
       {
           string Feedback = string.Empty;
           string connString = ConfigurationManager.ConnectionStrings["myConnection"].ConnectionString;


           using (SqlConnection conn = new SqlConnection(connString))
           {

               using (var copy = new SqlBulkCopy(conn))
               {


                   conn.Open();


                   copy.DestinationTableName = "BulkImportDetails";
                   copy.BatchSize = dt.Rows.Count;
                   try
                   {

                       copy.WriteToServer(dt);
                       Feedback = "Upload complete";
                   }
                   catch (Exception ex)
                   {
                       Feedback = ex.Message;
                   }
               }
           }

           return Feedback;
       }



Index Razor View

XML
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@{
    ViewBag.Title = "Home Page";
}
<h2>CSV Bulk Upload</h2>
@{ using (Html.BeginForm("", "", FormMethod.Post, new { enctype = "multipart/form-data" }))
 {
    <input type="file" name="FileUpload" />
    <input type="submit" name="Submit" id="Submit" value="Upload" />
 }
}

<p>@{ Html.Encode(ViewData["Feedback"]);} </p>


Currently, I am getting "no file selected exception" via Feedback.
Posted
Updated 11-Jul-14 3:58am
v2
Comments
[no name] 11-Jul-14 9:08am    
Okay.... and what is it that you would expect us to do about it?
JBobby 11-Jul-14 9:13am    
I never expected you guys to do the work for me.. help me anyway. provide me some tips or let me know how to use mvc razor for uploading csv file into sql database.
[no name] 11-Jul-14 9:18am    
And you would want help for what? To get any kind of help you would have to have some sort of a problem. "not able to make it work" and "giving me several exceptions" means absolutely nothing to us. We can't see your code, your project or read your mind.
JBobby 11-Jul-14 9:59am    
I updated the question. Now you can check it out.

http://arranmaclean.wordpress.com/2010/07/20/net-mvc-upload-a-csv-file-to-database-with-bulk-upload/[^][^]

This article is awesome . I just converted aspx to chtml. codes.. you can see the code in my question. :) silly me. I forgot to update the database, now everything is working fine. :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900