944,050 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 14208
  • VB.NET RSS
Jun 29th, 2007
0

using savefiledialog

Expand Post »
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
Last edited by gagansharma7; Jun 29th, 2007 at 12:07 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gagansharma7 is offline Offline
2 posts
since May 2006
Jun 29th, 2007
0

Re: using savefiledialog

Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Regarding Property
Next Thread in VB.NET Forum Timeline: Passing data from datagridview to textboxes





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC