ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Insert DAT File data in DataBase Table using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="vail"> vail</a>,</p> <p>Refer below sample code.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:FileUpload ID=&#34;fuRead&#34; runat=&#34;server&#34; /&gt; &lt;br /&gt; &lt;asp:Button Text=&#34;Upload&#34; runat=&#34;server&#34; OnClick=&#34;ReadDat&#34; /&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.IO; using System.Configuration; using System.Data.SqlClient;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.IO Imports System.Configuration Imports System.Data.SqlClient</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void ReadDat(object sender, EventArgs e) { &nbsp;&nbsp;&nbsp; string fileName = fuRead.PostedFile.FileName; &nbsp;&nbsp;&nbsp; fuRead.SaveAs(Server.MapPath(&#34;~/Files/&#34;) + Path.GetFileName(fileName)); &nbsp;&nbsp;&nbsp; string filePath = Server.MapPath(&#34;~/Files/&#34;) + Path.GetFileName(fileName); &nbsp;&nbsp;&nbsp; string[] lines = File.ReadAllLines(filePath); &nbsp;&nbsp;&nbsp; for (int i = 1; i &lt; lines.Length; i++) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Int32 id = Convert.ToInt32(lines[i].Split(&#39;\t&#39;)[0]); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string name = lines[i].Split(&#39;\t&#39;)[1]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DateTime date = Convert.ToDateTime(lines[i].Split(&#39;\t&#39;)[2].ToString()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string time = lines[i].Split(&#39;\t&#39;)[3]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int deviceNo = Convert.ToInt32(lines[i].Split(&#39;\t&#39;)[4]); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int InOut = Convert.ToInt32(lines[i].Split(&#39;\t&#39;)[5]); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Insert(id, name, date, time, deviceNo, InOut); &nbsp;&nbsp;&nbsp; } } public void Insert(int id, string name, DateTime date, string time, int deviceNo, int InOut) { &nbsp;&nbsp;&nbsp; string str = ConfigurationManager.ConnectionStrings[1].ConnectionString; &nbsp;&nbsp;&nbsp; using (SqlConnection con = new SqlConnection(str)) &nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string query = &#34;INSERT INTO TableName(id,name,date,time,deviceNo,InOut) VALUES(@id,@name,@date,@time,@deviceNo,@InOut)&#34;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; using (SqlCommand cmd = new SqlCommand(query, con)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@id&#34;, id); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@name&#34;, name); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@date&#34;, date); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@time&#34;, time); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;@deviceNo&#34;, deviceNo); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.Parameters.AddWithValue(&#34;InOut&#34;, InOut); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; con.Open(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cmd.ExecuteNonQuery(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; con.Close(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } &nbsp;&nbsp;&nbsp; } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub ReadDat(ByVal sender As Object, ByVal e As EventArgs) Dim fileName As String = fuRead.PostedFile.FileName fuRead.SaveAs(Server.MapPath(&#34;~/Files/&#34;) + Path.GetFileName(fileName)) Dim filePath As String = Server.MapPath(&#34;~/Files/&#34;) + 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) Next 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 = &#34;INSERT INTO TableName(id,name,date,time,deviceNo,InOut) VALUES(@id,@name,@date,@time,@deviceNo,@InOut)&#34; Using cmd As SqlCommand = New SqlCommand(query, con) cmd.Parameters.AddWithValue(&#34;@id&#34;, id) cmd.Parameters.AddWithValue(&#34;@name&#34;, name) cmd.Parameters.AddWithValue(&#34;@date&#34;, date) cmd.Parameters.AddWithValue(&#34;@time&#34;, time) cmd.Parameters.AddWithValue(&#34;@deviceNo&#34;, deviceNo) cmd.Parameters.AddWithValue(&#34;InOut&#34;, InOut) con.Open() cmd.ExecuteNonQuery() con.Close() End Using End Using End Sub</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/658883/Insert-DAT-File-data-in-DataBase-Table-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/658883/Insert-DAT-File-data-in-DataBase-Table-using-C-and-VBNet-in-ASPNet/Wed, 04 Sep 2019 07:44:07 GMT