Insert DAT File data in DataBase Table using C# and VB.Net in ASP.Net

Last Reply 9 months ago By pandeyism

Posted 9 months ago


Kindly give me the solution for how to upload a DAT file using file uploader in c# and insert to sql database.



Posted 9 months ago Modified on 9 months ago

Hi vail,

Refer below sample code.


<asp:FileUpload ID="fuRead" runat="server" />
<br />
<asp:Button Text="Upload" runat="server" OnClick="ReadDat" />



using System.IO;
using System.Configuration;
using System.Data.SqlClient;


Imports System.IO
Imports System.Configuration
Imports System.Data.SqlClient



protected void ReadDat(object sender, EventArgs e)
    string fileName = fuRead.PostedFile.FileName;
    fuRead.SaveAs(Server.MapPath("~/Files/") + Path.GetFileName(fileName));
    string filePath = Server.MapPath("~/Files/") + Path.GetFileName(fileName);
    string[] lines = File.ReadAllLines(filePath);
    for (int i = 1; i < lines.Length; i++)
        Int32 id = Convert.ToInt32(lines[i].Split('\t')[0]);
        string name = lines[i].Split('\t')[1];
        DateTime date = Convert.ToDateTime(lines[i].Split('\t')[2].ToString());
        string time = lines[i].Split('\t')[3];
        int deviceNo = Convert.ToInt32(lines[i].Split('\t')[4]);
        int InOut = Convert.ToInt32(lines[i].Split('\t')[5]);
        Insert(id, name, date, time, deviceNo, InOut);

public void Insert(int id, string name, DateTime date, string time, int deviceNo, int InOut)
    string str = ConfigurationManager.ConnectionStrings[1].ConnectionString;
    using (SqlConnection con = new SqlConnection(str))
        string query = "INSERT INTO TableName(id,name,date,time,deviceNo,InOut) VALUES(@id,@name,@date,@time,@deviceNo,@InOut)";
        using (SqlCommand cmd = new SqlCommand(query, con))
            cmd.Parameters.AddWithValue("@id", id);
            cmd.Parameters.AddWithValue("@name", name);
            cmd.Parameters.AddWithValue("@date", date);
            cmd.Parameters.AddWithValue("@time", time);
            cmd.Parameters.AddWithValue("@deviceNo", deviceNo);
            cmd.Parameters.AddWithValue("InOut", InOut);


Protected Sub ReadDat(ByVal sender As Object, ByVal e As EventArgs)
        Dim fileName As String = fuRead.PostedFile.FileName
        fuRead.SaveAs(Server.MapPath("~/Files/") + Path.GetFileName(fileName))
        Dim filePath As String = Server.MapPath("~/Files/") + Path.GetFileName(fileName)
        Dim lines As String() = File.ReadAllLines(filePath)

        For i As Integer = 1 To lines.Length - 1
            Dim id As Int32 = Convert.ToInt32(lines(i).Split(vbTab)(0))
            Dim name As String = lines(i).Split(vbTab)(1)
            Dim date As DateTime = Convert.ToDateTime(lines(i).Split(vbTab)(2).ToString())
            Dim time As String = lines(i).Split(vbTab)(3)
            Dim deviceNo As Integer = Convert.ToInt32(lines(i).Split(vbTab)(4))
            Dim InOut As Integer = Convert.ToInt32(lines(i).Split(vbTab)(5))
            Insert(id, name, date, time, deviceNo, InOut)
    End Sub

    Public Sub Insert(ByVal id As Integer, ByVal name As String, ByVal date As DateTime, ByVal time As String, ByVal deviceNo As Integer, ByVal InOut As Integer)
        Dim str As String = ConfigurationManager.ConnectionStrings(1).ConnectionString

        Using con As SqlConnection = New SqlConnection(str)
            Dim query As String = "INSERT INTO TableName(id,name,date,time,deviceNo,InOut) VALUES(@id,@name,@date,@time,@deviceNo,@InOut)"

            Using cmd As SqlCommand = New SqlCommand(query, con)
                cmd.Parameters.AddWithValue("@id", id)
                cmd.Parameters.AddWithValue("@name", name)
                cmd.Parameters.AddWithValue("@date", date)
                cmd.Parameters.AddWithValue("@time", time)
                cmd.Parameters.AddWithValue("@deviceNo", deviceNo)
                cmd.Parameters.AddWithValue("InOut", InOut)
            End Using
        End Using
End Sub