Hi,

I have the problem that the textfile did not appear after I saved it at my document, dekstop and other existing folder or new folder that created by user.Currently, it is only can be saved at C and D.
Below is the summary:

first:i want to call the value at database mssql server 2005
second:i want to save or export it to the textfile
third:the text file can be save at any location such as c,d,my document, new folder and any folder.

Here my code:

Private Sub savingFile()

        sd.CreatePrompt = False
        sd.FileName = lblName.Text

        sd.InitialDirectory = "C:\"
        sd.Title = "Save text Files"
        sd.Filter = "Text File|*.txt"

        If sd.ShowDialog() = DialogResult.OK Then
            Console.WriteLine(sd.FileName)
            exportProcedure(sd.FileName)
            MsgBox("The data have been saved as text file.")
        Else
            MsgBox("You are not save as text file.")
        End If
    End Sub
Private Sub exportProcedure(ByVal fileName)
           Try
            Dim bcpCommand As String

            bcpCommand = "Select Id,productcode, desc, Exclusion, 
                          BrandName, CostPrice, "& _                       
                          "from " & DbName & ".dbo.tblWare " & _
                          "Where e.Id = '" & lblId.Text & "'"

            Shell("bcp " + """" + bcpCommand + """" + " queryout " & _
               "" & fileName & " -S " & sServer & " -U sa -P Sun -c -t ^ -r \n ", 
               vbHide)

        Catch ex As Exception
            MsgBox(ex.Message())
        End Try
    End Sub

TQ.

what happens when you comment this line

sd.InitialDirectory = "C:\"

and then try to create the file by browsing the file dialog ?

See if this helps.

Private Sub exportProcedure(ByVal fileName As String)
        Try

            '// your entire code here to get value from database.

            Dim sTemp As String = "my database value goes here"
            IO.File.WriteAllText(fileName, sTemp) '// save file.
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

Try using TextmWriter object:

Dim text1 As String = "Some text in the line"
Dim text2 As String() = {"a", "b", "c"}
Dim path As String = "c:\myFile.txt"
'fill will be create (if not exists) on the c: drive
Using fs As New FileStream(path, FileMode.OpenOrCreate)
	Using tw As TextWriter = New StreamWriter(fs)
		tw.WriteLine(text1)
		For Each str As String In text2
			tw.WriteLine(str)
		Next
	End Using
End Using

About the upper example, it should be working, at least if the "fileName" contains the correct path:

Dim filePath As String = "C:\1\testFile234.txt"
Dim sTemp As String = "my database value goes here1"
System.IO.File.WriteAllText(filePath, sTemp)
'but this will always re-write the whole file (if any text previously in the file, it will be gone, only a new one will be inserted!

tq.

but i want is the savedialog diplay to enable the user to locate the file that they want to save it.I mean it is not only at C.

This article has been dead for over six months. Start a new discussion instead.