Wednesday, December 17, 2008

xml creation

Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Imports System.IO
Partial Class xmltest
Inherits System.Web.UI.Page
Dim i As Integer
Dim Filen As FileInfo
Dim conpubs As SqlConnection
Dim conn As String = WebConfigurationManager.ConnectionStrings("SQLConnection").ConnectionString
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
DBtoXML()
End Sub
Private Sub ErrMail(ByVal msg As String)
Dim lbl As New Label
lbl.Text = ""
Page.Controls.Add(lbl)
End Sub
Public Function DBtoXML() As Boolean
Try
conpubs = New SqlConnection(conn)
conpubs.Open()
Dim ds As New DataSet()
Dim da As New SqlDataAdapter("SELECT * FROM registration WHERE DoctorName like 'r%'", conpubs)
Dim filename As String = "DBtoXML.xml"
Filen = New FileInfo(Server.MapPath("~/conversions/") & filename)
If Filen.Exists Then
File.Delete(Server.MapPath("~/conversions/") & filename)
'Me.i = 0
'Do While Me.i < 100
' i += 1
' filename = "DBtoXML.xml"
' File = New FileInfo(Server.MapPath("~/conversions/") & filename)
' If Not File.Exists Then Exit Do
' If i = 100 Then ErrMail("DBtoXML File Name Failure /n Could not find a suitable filename for: " & Server.MapPath("~/conversions/") & filename) : Return False
'Loop
End If
da.Fill(ds) 'Used for backups, incase of a failure or incase I need it later on. ds.WriteXml(Server.MapPath("/conversions/") & filename) For Me.i = 0 To da.Tables("Stories").Rows.Count - 1 'I could also use, For Each dr As DataRow in da.Tables("Stories"), right? I'd prefer that. 'I need to name the file after a row... called... "SID" Next Catch ex As SqlException ErrMail("Database Exception - DBtoXML Conversion", (ex).ToString()) Return False End TryEnd FunctionPublic Function DBtoXML() As Boolean
ds.WriteXml(Server.MapPath("~/conversions/") & filename)
'For Me.i = 0 To da.Tables("Stories").Rows.Count - 1
' 'I could also use, For Each dr As DataRow in da.Tables("Stories"), right? I'd prefer that.
' 'I need to name the file after a row... called... "SID"
'Next
Catch ex As SqlException
ErrMail("Database Exception - DBtoXML Conversion" & (ex).ToString())
Return False
Finally
conpubs.Close()
End Try
End Function

End Class