Share via


SaveFileDialog for CSV-Export

Question

Friday, July 3, 2009 12:06 PM

Hi there,
i want to have a SaveFileDialog for CSV-Export. Here is my code:

 Private Sub btnExportCSV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExportCSV.Click

        Dim da As New SqlDataAdapter("select * from tbl_gesamtminuten", conn)
        Dim ds As New DataSet()

        da.Fill(ds, "tbl_gesamtminuten")

        DataGridView1.DataSource = ds.Tables("tbl_gesamtminuten").DefaultView

        'DataGridView1.DataBind()

        Dim dt As DataTable = ds.Tables("tbl_gesamtminuten")

        'Dim saveFileDialog1 As New SaveFileDialog()
        'saveFileDialog1.Filter = "CSV|*.csv"
        'saveFileDialog1.Title = "Speichern CSV-Datei"
        'saveFileDialog1.ShowDialog()


        CreateCSVFile(dt, "c:\csvData.csv")



    End Sub

I want that the user can choose the location for saving the file.

Many thanks for all answers.

Mull

All replies (3)

Friday, July 3, 2009 12:18 PM ✅Answered

I think you just need a final bit of wiring in your code:

        Dim saveFileDialog1 As New SaveFileDialog()
        saveFileDialog1.Filter = "CSV|*.csv"
        saveFileDialog1.Title = "Speichern CSV-Datei"
        If saveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then

            createcsvfile(dt, saveFileDialog1.FileName)

        End If

Hope this helpsHomepage | Articles | Blog


Friday, July 3, 2009 12:16 PM

Many thanks for all answers.

Answer to what? I don't see a question in your post.

But I think you want to do

CreateCSVFile(dt, saveFile.FileName)

You also should check the return value of ShowDialog() to make sure the user didn't cancel the operation.Mattias, C# MVP


Friday, July 3, 2009 6:07 PM

Hi Tom John,
ty for your help. Thats my code:

 Private Sub btnExportCSV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExportCSV.Click

        With savedlg
            .Filter = "CSV|*.csv"
            .Title = "Speichern CSV-Datei"
        End With

        If savedlg.ShowDialog = Windows.Forms.DialogResult.OK Then

            Dim da As New SqlDataAdapter("select * from tbl_gesamtminuten", conn)
            Dim ds As New DataSet()

            da.Fill(ds, "tbl_gesamtminuten")

            Dim dt As DataTable = ds.Tables("tbl_gesamtminuten")

            CreateCSVFile(dt, savedlg.FileName)

        End If

    End Sub

Mull