Scroll To Top

Create New Text file with Data Entry

By Amit Sanandiya Oct 04, 2012
Create New Text file With Data Entry In
Create New Text file With Data Entry In

First We Create New Table With Some Data..

Table Name Is db
Field 1 ->iddb
Field 2 ->name
Field 3 ->salary


#Region "Namespaces"
Imports DAL
Imports System.Linq
Imports System.Threading
Imports System.Data.SqlClient
Imports System.IO
Imports System.Text
#End Region 

Dim i As String = String.Empty
Dim id As String = String.Empty
Dim con As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=D:\amit\VBDemo\VBDemo\Database1.mdf;Integrated Security=True;User Instance=True")

//This Butten Key Use For A New File Creation And Data Edit
Private Sub CreateFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreateFile.Click
    'Write Data In Text File
        id = TXTDBID.Text
        If i = "Y" Then
            Dim dt As DataTable = New DataTable
            Dim cmd As SqlCommand = New SqlCommand
            cmd.Connection = con
            cmd.CommandText = "select * from db where iddb ='" & id & "'"
            cmd.CommandType = CommandType.Text
            Dim da As SqlDataAdapter = New SqlDataAdapter
            da.SelectCommand = cmd
            If dt.Rows.Count > 0 Then
                Dim TextFile As New StreamWriter("D:\" + id + ".txt")
                TextFile.WriteLine("Id : " + dt.Rows(0)(0).ToString()
                TextFile.WriteLine("Name :" + dt.Rows(0)(1).ToString())
                TextFile.WriteLine("Salary :" + dt.Rows(0)(2).ToString())
                MessageBox.Show(id + ".txt Text File created")
            End If
        End If
    Catch ex As IOException
    End Try
End Sub 

//This Function Use For A Creation New Text File
Private Sub CreateFile()
    If File.Exists("d:\" + id + ".txt") The
        MessageBox.Show("File Already Exist")
        i = "N"
        Exit Sub
        Dim file_stream As New FileStream("d:\" + id + ".txt", FileMode.Append)
        i = "Y"
    End If
End Sub