Import .csv file into oracle database.

Apr 5 2018 2:40 AM
my csv file contain a column which is having a date datatype and im getting an error which is row1 column 1 is invalid number.How to resolve this problem.The code is written in asp.net and c#
  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Collections;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9. using System.Web.UI.WebControls.WebParts;  
  10. using System.Web.UI.HtmlControls;  
  11. using System.Data.OleDb;  
  12. using System.Data.SqlTypes;  
  13. using System.Globalization;  
  14. using System.IO;  
  15. //using System.Data.OracleClient;  
  16. using Oracle.DataAccess.Client;  
  17. public partial class Exp : System.Web.UI.Page  
  18. {  
  19. public OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["conguest"].ToString());  
  20. public OracleConnection conn1 = new OracleConnection(ConfigurationManager.ConnectionStrings["conguest1"].ToString());  
  21. private void InsertCSVRecords(DataTable dt)  
  22. {  
  23. conn1.Open();  
  24. OracleBulkCopy OracleBulkCopy = new OracleBulkCopy(conn1);  
  25. OracleBulkCopy.DestinationTableName = "guest.material_master";  
  26. //Response.Write("test2");  
  27. OracleBulkCopy.ColumnMappings.Add("REQ_NO""REQ_NO");  
  28. OracleBulkCopy.ColumnMappings.Add("REQ_DT""REQ_DT");  
  29. OracleBulkCopy.ColumnMappings.Add("REQ_BY""REQ_BY");  
  30. OracleBulkCopy.ColumnMappings.Add("MAT_CD""MAT_CD");  
  31. OracleBulkCopy.ColumnMappings.Add("MAT_DESC""MAT_DESC");  
  32. OracleBulkCopy.ColumnMappings.Add("MAT_TYPE_CD""MAT_TYPE_CD");  
  33. OracleBulkCopy.ColumnMappings.Add("BASE_UOM_CD""BASE_UOM_CD");  
  34. OracleBulkCopy.ColumnMappings.Add("STOR_LOC_CD""STOR_LOC_CD");  
  35. OracleBulkCopy.ColumnMappings.Add("PUR_GRP_CD""PUR_GRP_CD");  
  36. OracleBulkCopy.ColumnMappings.Add("HSN_CD""HSN_CD");  
  37. OracleBulkCopy.ColumnMappings.Add("MAT_LONG_DESC""MAT_LONG_DESC");  
  38. OracleBulkCopy.ColumnMappings.Add("TAX_IND""TAX_IND");  
  39. OracleBulkCopy.ColumnMappings.Add("ACTIVE_IND""ACTIVE_IND");  
  40. OracleBulkCopy.ColumnMappings.Add("DEL_IND""DEL_IND");  
  41. OracleBulkCopy.WriteToServer(dt);  
  42. conn1.Close();  
  43. }  
  44. protected void Button4_Click(object sender, EventArgs e)  
  45. {  
  46. //try  
  47. //{  
  48. string csvPath = Server.MapPath("~/Uploads/") + Path.GetFileName(FileUpload1.PostedFile.FileName);  
  49. FileUpload1.SaveAs(csvPath);  
  50. if ((FileUpload1.PostedFile != null) && (FileUpload1.PostedFile.ContentLength > 0))  
  51. {  
  52. string fn = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);  
  53. string SaveLocation = Server.MapPath("~/Uploads") + "\\" + fn;  
  54. FileUpload1.PostedFile.SaveAs(SaveLocation);  
  55. Response.Write("The file has been uploaded.");  
  56. DataTable dt = new DataTable();  
  57. //DataSet ds = new DataSet();  
  58. dt.Columns.AddRange(new DataColumn[14] {  
  59. new DataColumn("REQ_NO"typeof(string)),  
  60. new DataColumn("REQ_DT"typeof(string)),  
  61. new DataColumn("REQ_BY",typeof(string)),  
  62. new DataColumn("MAT_CD"typeof(string)),  
  63. new DataColumn("MAT_DESC"typeof(string)),  
  64. new DataColumn("MAT_TYPE_CD"typeof(string)),  
  65. new DataColumn("BASE_UOM_CD"typeof(string)),  
  66. new DataColumn("STOR_LOC_CD"typeof(string)),  
  67. new DataColumn("PUR_GRP_CD"typeof(string)),  
  68. new DataColumn("HSN_CD"typeof(string)),  
  69. new DataColumn("MAT_LONG_DESC"typeof(string)),  
  70. new DataColumn("TAX_IND"typeof(string)),  
  71. new DataColumn("ACTIVE_IND"typeof(string)),  
  72. new DataColumn("DEL_IND"typeof(string))});  
  73. //DateTime dt1 = DateTime.Parse("dd-MMM-yyyy");  
  74. string csvData = File.ReadAllText(fn);  
  75. foreach (string row in csvData.Split('\r'))  
  76. {  
  77. if (!string.IsNullOrEmpty(row))  
  78. {  
  79. dt.Rows.Add();  
  80. int i = 0;  
  81. foreach (string cell in row.Split(','))  
  82. {  
  83. dt.Rows[dt.Rows.Count - 1][i] = cell;  
  84. i++;  
  85. }  
  86. }  
  87. }  
  88. GridView1.DataSource = dt;  
  89. GridView1.DataBind();  
  90. //Response.Write("Test2");  
  91. InsertCSVRecords(dt);  
  92. }  
  93. else  
  94. {  
  95. Response.Write("Please select a file to upload.");  
  96. }  
  97. //}  
  98. //catch (Exception ex)  
  99. //{  
  100. // Response.Write(ex.Message);  
  101. //}  
  102. }  
  103. }

Answers (1)