Upload Text File .txt and .log

In this article, I am going to show you how to upload a delimited file into the database. 

ASPX

We have just added one button and upload file tool. code is below.

 File Uploader

<asp:FileUpload ID="FileUpload1" runat="server" BorderColor="Black" BorderStyle="Solid" Width="303px" BackColor="white" CssClass="form-control" />

Button

<asp:Button ID="btnUploadFile" runat="server" Text="Upload"  OnClick="btnUploadFile_Click" CssClass="btn btn-success button-62" />

Now, we will write the code behind the file upload function.

Code Behind

On button click we will call the file reader function.

string[] lines = File.ReadAllLines(file_Path + file_Name); 

This reads all lines function will read text files from top to bottom.

Now, from here, we have to write the logic to split the file column into cells. suppose your file has 3 columns.

for (int i = 0; i <= lines.Length - 1; i++)
{
    // Note this is the separation we have file with separation [:]  
    string[] lines_data = lines[i].ToString().Split(':');

    strsql = "INSERT INTO tableName(col1,col2,col3)";
    strsql += $"VALUES('{lines_data[0].ToString().Trim()}', '{lines_data[1].ToString().Trim()}', '{lines_data[2].ToString().Trim()}', '{Source_File_Name.ToString().ToUpper()}')";
    Flag = obj.GetNonScalar(strsql, obj._ConnIND).ToString();
}

All done, it will read the file successfully.

Thanks for watching. please feel free to ask any questions.

This will not have any output screen to share. it will directly link to database tables.