Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generating XML from DataSet and Saving It 1

Status
Not open for further replies.
Jul 10, 2008
32
US
Hello, I have run into a problem developing a small VB 2005 application for a call center. The program runs against Microsoft SQL Server 2005. The program runs a query to pull records from one table in a database, based on the dates the user selects from DateTimePicker (which correspond to a Date column in the table. Basically, the function is "Select all calls made between date1 and date2). I do this by declaring a DataAdapter object, then I set the SelectCommand properties for Connection, CommandText, and Parameters. Then I declare a DataSet object and fill it with the data returned by the query and close the database connection. Then, I am supposed to take that data and generate an XML file with it. This is the code I have now:

Code:
' Write query results to XML file.
objDataSet.WriteXml("C:\SampleXML.xml")

The problem with this is obvious: everytime you run the query, it will overwrite the existing XML file. I want to implement a "Save As" dialog box so that when the user selects the 2 dates and hits "Execute", the query runs and a Save As dialog box pops up asking what to name the file and where to store it, and THEN have the XML file written to that name/location.

I'm very new to VB. I'm just not sure how to implement a SaveAsDialog so that it will work with automatically generating an XML file. Anyone have suggestions on how I can get my program to do what I want it to do?
 
First, place a SaveFielDialog on your form. Then, in your code do this:

SaveFileDialog1.Filter = "XML Files (*.xml)|*.xml
SaveFileDialog1.DefaultExt = "xml"
SaveFileDialog1.AddExtension = True
SaveFileDialog1.OverwritePrompt = True
'use the next line if you always want the same initial directory in the SaveFileDialog
SaveFileDialog1.InitialDirectory = "path\to\initial\directory"
'note: put an actual path in place of "path\to\initial\directory"

If SaveFileDialog1.ShowDialog = DialogResult.OK Then

Dim SaveName As String

SaveName = SaveFileDialog1.FileName

' Write query results to XML file.
objDataSet.WriteXml(SaveName)

End If

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top