using savefiledialog

caterpillar

New member
Joined
Sep 1, 2006
Messages
2
Programming Experience
Beginner
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 :confused: 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 !:p
My code is :

VB.NET:
Expand Collapse Copy
    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
VB.NET:
Expand Collapse Copy
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
VB.NET:
Expand Collapse Copy
    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
 
Last edited by a moderator:
VB.NET:
Expand Collapse Copy
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim filename As String
        With SaveFileDialog1

            .DefaultExt = ".csv"
            .AddExtension = True
            .Filter = "Comma Separated Values (*.csv)|*.csv"
            .FilterIndex = 0
            .InitialDirectory = "c:\"
            .OverwritePrompt = True
            Dim result As DialogResult = .ShowDialog()
            If result = DialogResult.OK Then
                filename = .FileName

            End If
        End With

        MsgBox(filename)

    End Sub


Here is a little something I whipped up real fast using VS 2003. This a good example of how to use a save file dialog. Play around with some of these options and you should be able to produce your desired result.
 
Back
Top