Hi akhter,
Check this example. Now please take its reference and correct your code.
For display purpost i have used DataTable and inserted each record in DataTable and diplay in GridView. You need to remove the code for inserting in DataTable and GridView.
Note : According to you Database column DataType change the DataType.
HTML
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button Text="Import" runat="server" OnClick="ReadDat" />
<asp:GridView runat="server" ID="gvEmployees" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="EmployeeCode" HeaderText="Employee Code" />
<asp:BoundField DataField="Date" HeaderText="Date" DataFormatString="{0:yyyyMMdd}" />
<asp:BoundField DataField="Time" HeaderText="Time" />
<asp:BoundField DataField="Status" HeaderText="Status" />
<asp:BoundField DataField="MId" HeaderText="MID" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Code
C#
protected void ReadDat(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("EmployeeCode",typeof(int)),
new DataColumn("Date",typeof(DateTime)),
new DataColumn("Time",typeof(TimeSpan)),
new DataColumn("Status",typeof(string)),
new DataColumn("MId",typeof(int)) });
string[] lines = File.ReadAllLines(FileUpload1.PostedFile.FileName);
for (int i = 0; i < lines.Length; i++)
{
for (int j = 0; j < lines[i].Split(' ').Length; j++)
{
string dateValue = lines[i].Split(' ')[j].Substring(0, 8);
DateTime Dates = new DateTime(Convert.ToInt32(dateValue.Substring(0, 4)), Convert.ToInt32(dateValue.Substring(4, 2)), Convert.ToInt32(dateValue.Substring(6, 2)));
string time = lines[i].Split(' ')[j].Substring(8, 4);
TimeSpan Times = new TimeSpan(Convert.ToInt32(time.Substring(0, 2)), Convert.ToInt32(time.Substring(2, 2)), 0);
string INOUT = lines[i].Split(' ')[j].Substring(12, 4);
Int32 empcode = Convert.ToInt32(lines[i].Split(' ')[j].Substring(16, 7));
int MID = Convert.ToInt32(lines[i].Split(' ')[j].Substring(23, 2));
dt.Rows.Add(empcode, Dates, Times, INOUT, MID);
//Insert(empcode, Dates, Times, INOUT, MID);
}
}
gvEmployees.DataSource = dt;
gvEmployees.DataBind();
}
VB.Net
Protected Sub ReadDat(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("EmployeeCode", GetType(Integer)), New DataColumn("Date", GetType(DateTime)), New DataColumn("Time", GetType(TimeSpan)), New DataColumn("Status", GetType(String)), New DataColumn("MId", GetType(Integer))})
Dim lines As String() = File.ReadAllLines(FileUpload1.PostedFile.FileName)
For i As Integer = 0 To lines.Length - 1
For j As Integer = 0 To lines(i).Split(" "c).Length - 1
Dim dateValue As String = lines(i).Split(" "c)(j).Substring(0, 8)
Dim Dates As DateTime = New DateTime(Convert.ToInt32(dateValue.Substring(0, 4)), Convert.ToInt32(dateValue.Substring(4, 2)), Convert.ToInt32(dateValue.Substring(6, 2)))
Dim time As String = lines(i).Split(" "c)(j).Substring(8, 4)
Dim Times As TimeSpan = New TimeSpan(Convert.ToInt32(time.Substring(0, 2)), Convert.ToInt32(time.Substring(2, 2)), 0)
Dim INOUT As String = lines(i).Split(" "c)(j).Substring(12, 4)
Dim empcode As Int32 = Convert.ToInt32(lines(i).Split(" "c)(j).Substring(16, 7))
Dim MID As Integer = Convert.ToInt32(lines(i).Split(" "c)(j).Substring(23, 2))
dt.Rows.Add(empcode, Dates, Times, INOUT, MID)
'Insert(empcode, Dates, Times, INOUT, MID);
Next
Next
gvEmployees.DataSource = dt
gvEmployees.DataBind()
End Sub
Screenshot
Employee.txt
2018052604440002200009131 2018052604440002200009231 2018052604440002200009431