Create New Text file With Data Entry In Vb.net
First We Create New Table With Some Data..
Table Name Is db
Field 1 ->iddb
Field 2 ->name
Field 3 ->salary
//Code
#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
Try
con.Open()
id = TXTDBID.Text
CreateFile()
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
da.Fill(dt)
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())
TextFile.Close()
MessageBox.Show(id
+ ".txt Text File created")
End
If
End If
con.Close()
Catch ex As IOException
MsgBox(ex.ToString)
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
Else
Dim
file_stream As New
FileStream("d:\"
+ id + ".txt", FileMode.Append)
file_stream.Close()
i = "Y"
End If
End Sub