ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Import data from .dbf file to SQL Server using AngularJS in ASP.Net MVC<p>Hi <a class="username" rel="mahesh213"> mahesh213</a>,</p> <p>Check this example. Now please take its reference and correct your code.</p> <p>First you need to save the uploaded file in project folder.</p> <p>Then read the dbf file using&nbsp;OleDb and fill the DataTable using OleDbDataAdapter.</p> <p>Then loop through the DataTable rows insert the record in database.</p> <p>After finishing the process delete saved dbf file from folder.</p> <p><strong><span style="text-decoration: underline;">Controller</span></strong></p> <pre class="brush: csharp">public class HomeController : Controller { &nbsp;&nbsp;&nbsp; // GET: /Home/ &nbsp;&nbsp;&nbsp; public ActionResult Index() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return View(); &nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; public JsonResult AddState() &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string fileName = &#34;&#34;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (Request.Files.Count &gt; 0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HttpPostedFileBase postedFile = Request.Files[0]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fileName = postedFile.FileName; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Save dbf file to Files folder. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; postedFile.SaveAs(Path.Combine(Server.MapPath(&#34;~/Files/&#34;), fileName)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataTable dt = new DataTable(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OleDbConnection con = new OleDbConnection(@&#34;Provider=VFPOLEDB.1;Data Source=&#34; + Path.Combine(Server.MapPath(&#34;~/Files/&#34;), fileName)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; con.Open(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (con.State == ConnectionState.Open) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string query = &#34;SELECT * FROM Customers&#34;;&nbsp; // dbf table name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OleDbCommand cmd = new OleDbCommand(query, con); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OleDbDataAdapter oda = new OleDbDataAdapter(cmd); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; oda.Fill(dt); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; con.Close(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (dt.Rows.Count &gt; 0) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Insert records to database table. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CustomersEntities entities = new CustomersEntities(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach (DataRow row in dt.Rows) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; entities.Customers.Add(new Customer &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Name = row[&#34;Name&#34;].ToString(), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Country = row[&#34;Country&#34;].ToString() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; entities.SaveChanges(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Delete saved dbf file. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.IO.File.Delete(Path.Combine(Server.MapPath(&#34;~/Files/&#34;), fileName)); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return Json(&#34;Import dbf file Success!&#34;, JsonRequestBehavior.AllowGet); &nbsp;&nbsp;&nbsp; } }</pre> <p><strong><span style="text-decoration: underline;">View</span></strong></p> <pre class="brush: html">&lt;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt; &lt;head&gt; &lt;title&gt;Index&lt;/title&gt; &lt;link rel=&#34;stylesheet&#34; href=&#34;https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css&#34; /&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.8/angular.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34; src=&#34;https://cdnjs.cloudflare.com/ajax/libs/danialfarid-angular-file-upload/12.2.13/ng-file-upload.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; var app = angular.module(&#34;myApp&#34;, [&#39;ngFileUpload&#39;]); app.controller(&#34;myCntrl&#34;, [&#39;$scope&#39;, &#39;$http&#39;, &#39;myService&#39;, function ($scope, $http, myService) { $scope.UploadFiles = function (files) { $scope.SelectedFiles = files; }; $scope.AddUpdateEmployee = function () { var formData = new FormData(); var files = $scope.SelectedFiles; if (files != undefined) { for (var i = 0; i &lt; files.length; i++) { formData.append(files[i].name, files[i]); } } var details = {}; details.Name = $scope.Name; formData.append(&#34;details&#34;, JSON.stringify(details)); var getData = myService.AddSt(formData); getData.then(function (tc) { alert(tc); }); } } ]); app.service(&#34;myService&#34;, function ($http) { this.AddSt = function (employee) { var response = $.ajax({ url: &#39;/Home/AddState&#39;, type: &#34;POST&#34;, contentType: false, processData: false, data: employee }); return response; } }); &lt;/script&gt; &lt;/head&gt; &lt;body ng-app=&#34;myApp&#34; ng-controller=&#34;myCntrl&#34;&gt; &lt;div class=&#34;container&#34;&gt; &lt;div&gt; &lt;div id=&#34;wrapper&#34; class=&#34;clearfix&#34;&gt; &lt;form name=&#34;userForm&#34;&gt; &lt;div class=&#34;form-horizontal&#34;&gt; &lt;div class=&#34;form-row&#34;&gt; &lt;div class=&#34;col-md-4&#34;&gt; &lt;label for=&#34;COI_Name&#34;&gt; Upload a file &lt;/label&gt; &lt;input type=&#34;file&#34; ngf-select=&#34;UploadFiles($files)&#34; /&gt; &lt;/div&gt; &lt;/div&gt; &lt;div&gt; &lt;/div&gt; &lt;div class=&#34;form-group&#34;&gt; &lt;/div&gt; &lt;div class=&#34;form-group&#34; style=&#34;width: 120%; text-align: center; padding: 10px;&#34;&gt; &lt;div class=&#34;col-md-offset-2 col-md-5&#34;&gt; &lt;p&gt; &lt;button ng-model=&#34;IsVisible&#34; ng-click=&#34;AddUpdateEmployee()&#34;&gt; &lt;span class=&#34;glyphicon glyphicon-ok&#34;&gt;&lt;/span&gt;Submitt &lt;/button&gt; &lt;/p&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/form&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/167569/Import-data-from-dbf-file-to-SQL-Server-using-AngularJS-in-ASPNet-MVC/https://www.aspforums.net:443/Threads/167569/Import-data-from-dbf-file-to-SQL-Server-using-AngularJS-in-ASPNet-MVC/Mon, 26 Aug 2019 06:09:27 GMT