Hello,

In VB.NET 1.1 and using WinForms, I am exporting the data displayed in a listview to a CSV file but I am specifying the filename and location while conversion. I need a dialog box where the user can choose the location to save the file. I know I have to use savefiledialog control for this but have no idea how to make it happen.
I am using a button control btnExport which calls Function Export() to read the data stream of bytes and then it calls Function SaveTextToFile() to set the exported csv file path as C:\test.csv.


Will someone put me on the right track. Thanks in advance !
My code is :

.................................................. .................................................. .................................................. .....................
Public Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
Dim ds As DataSet

Dim strData As String = Me.Export(ds, Me.CheckBox1.Checked)
Dim data() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes(strData)
Dim t As System.IO.TextWriter = New System.IO.StringWriter
SaveTextToFile(strData, "c:\test.csv")

End Sub

------------------------------------------------------------------------------------------------------------------------------------------

Public Function Export(ByVal ds As DataSet, ByVal exportcolumnheadings As Boolean) As String
Dim header As String
Dim body As String
Dim DS1 As New DataSet
Dim record As String
exportcolumnheadings = True

aid = Me.txtAID.Text

Dim sqlCmd As New SqlCommand
Dim DA1 As New SqlDataAdapter
Try
With sqlCmd
.Connection = gsqlconn
.CommandType = CommandType.StoredProcedure
.CommandText = "dbo.spname"
.CommandTimeout = 0

.Parameters.Add("@AID", SqlDbType.Int)
.Prepare()
.Parameters("@AID").Value = Me.txtAID.Text
'.Dispose()
End With

DA1.SelectCommand = sqlCmd
DS1.Clear()
DA1.Fill(DS1)
Dim dtb As DataTable
dtb = DS1.Tables(0)
MsgBox(dtb.Rows.Count.ToString + " AID Exported")

If exportcolumnheadings Then
For Each col As DataColumn In dtb.Columns
header = header & Chr(34) & col.ColumnName & Chr(34) & ","
Next
header = header.Substring(0, header.Length - 1)
header = UCase(header)
End If

For Each row As DataRow In dtb.Rows
Dim arr() As Object = row.ItemArray()
For i As Integer = 0 To arr.Length - 1
If arr(i).ToString().IndexOf(",") > 0 Then
record = record & Chr(34) & arr(i).ToString() & Chr(34) & ","
Else
record = record & arr(i).ToString() & ","
End If
Next
body = body & record.Substring(0, record.Length - 1) & vbCrLf
record = ""
Next

If exportcolumnheadings Then
Return header & vbCrLf & body
Else
Return body
End If
Catch ex As Exception
MessageBox.Show(ex.Message & gstrContactIT, "No Item Category", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
If Not DA1 Is Nothing Then DA1.Dispose() : DA1 = Nothing
If Not sqlCmd Is Nothing Then sqlCmd.Dispose() : sqlCmd = Nothing
End Try
End Function
.................................................. .................................................. .................................................. ......................
Public Function SaveTextToFile(ByVal strData As String, ByVal FullPath As String, _
Optional ByVal ErrInfo As String = "") As Boolean

Dim Contents As String
Dim bAns As Boolean = False
Dim objReader As StreamWriter
Try
objReader = New StreamWriter(FullPath)
objReader.Write(strData)
objReader.Close()
bAns = True
Catch Ex As Exception
ErrInfo = Ex.Message

End Try
Return bAns
End Function

.................................................. .................................................. ..........................


Thanks
Gagan

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.