Akhter HUssain

Akhter HUssain

  • 684
  • 1.3k
  • 95.9k

Importing Data From txt File into sql table

Nov 27 2018 10:20 PM
My Data Format is that
 
2018052605330002200009131
2018052604460002200009231
2018052604430001200009431
 
For understanding columns in sql table
 
(Date,20180526,time:0444,Status:0002,Empcode:2000091,MID:31)
 
here is my code, when i import data then error is coming of input string wast not in a correct format on this line
 
Int32 empcode = Convert.ToInt32(lines[i].Split(' ')[0].ToString());
 
whole code is mentioned below
  1. protected void ReadDat(object sender, EventArgs e)  
  2. {  
  3. // string[] lines = File.ReadAllLines(Server.MapPath("FileUpload1.PostedFile.FileName"));  
  4. string[] lines = File.ReadAllLines(Server.MapPath(FileUpload1.PostedFile.FileName));  
  5. for (int i = 0; i < lines.Length; i++)  
  6. {  
  7. Int32 empcode = Convert.ToInt32(lines[i].Split(' ')[0].ToString());  
  8. DateTime Date = Convert.ToDateTime(lines[i].Split(' ')[1].ToString() + " " + lines[i].Split(' ')[2].ToString());  
  9. int Time = Convert.ToInt32(lines[i].Split(' ')[3]);  
  10. int INOUT = Convert.ToInt32(lines[i].Split(' ')[4]);  
  11. int MID = Convert.ToInt32(lines[i].Split(' ')[5]);  
  12. Insert (empcode,Date,Time,INOUT,MID);  
  13. }  
  14. }  
  15. public void Insert(int empcode, DateTime date, int TIme, int INOUT, int MID)  
  16. {  
  17. string str = ConfigurationManager.ConnectionStrings[1].ConnectionString;  
  18. using (SqlConnection con = new SqlConnection(str))  
  19. {  
  20. string query = "INSERT INTO HR VALUES(" + empcode + ",'" + date + "'," + TIme + "," + INOUT + "," + MID + ")";  
  21. using (SqlCommand cmd = new SqlCommand(query, con))  
  22. {  
  23. con.Open();  
  24. cmd.ExecuteNonQuery();  
  25. con.Close();  
  26. }  
  27. }  
  28. }  
  29. }  
  30. }

Answers (2)