| | |
using savefiledialog
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2006
Posts: 2
Reputation:
Solved Threads: 0
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
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
Last edited by gagansharma7; Jun 29th, 2007 at 12:07 pm.
![]() |
Similar Threads
- Encrypt File using User Supplied Password (VB.NET)
- how to give extension to a file?? (C#)
- savefiledialog in a dialog window (C++)
- Copy one database to another (VB.NET)
- Math Functions (C++)
- Help! What Language is this? (Assembly)
Other Threads in the VB.NET Forum
- Previous Thread: Regarding Property
- Next Thread: Passing data from datagridview to textboxes
| Thread Tools | Search this Thread |
.net .net2008 2005 2008 access account array basic beginner bing browser button buttons center check code crystalreport cuesent data database datagrid datagridview date datetimepicker designer dissertation dissertations dissertationtopic dropdownlist excel fade file-dialog filter forms ftp generatetags google gridview html images input insert intel internet listview mobile monitor net networking objects open output panel passingparameters pdf picturebox port position print printing problem remove save searchbox searchvb.net select serial settings shutdown soap sqlserver survey table tcp temperature text textbox timer timespan toolbox transparency trim update user vb vb.net vb.netformclosing()eventpictureboxmessagebox vb2008 vba vbnet visual visualbasic visualbasic.net visualstudio.net visualstudio2008 web winforms wpf wrapingcode year






