I have to create a .kml file from the SQL 2005 database which contains data about real estate properties. The file name is genKML.aspx
Imports System
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text.StringBuilder
Imports System.Xml
Imports System.Net
Partial Class myCDN_genKML
Inherits System.Web.UI.Page
Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
My.Response.Clear()
My.Response.ContentType = "application/vnd.google-earth.kml+xml"
My.Response.ContentEncoding = System.Text.Encoding.UTF8
Dim stream As New System.IO.MemoryStream
Dim XMLwrite As New XmlTextWriter(stream, System.Text.Encoding.UTF8)
Dim i As Integer = 1
Dim recProp As Integer = 0
Dim strDes As String = ""
Dim strCod As String = ""
Dim myReader As System.Data.SqlClient.SqlDataReader
Dim conn As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(SqlDataSource1.ConnectionString)
Dim cmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand()
cmd.Connection = conn
conn.Open()
cmd.CommandText = "SELECT * FROM properties WHERE propertyStatus = 'Active'"
myReader = cmd.ExecuteReader()
While myReader.Read()
recProp = recProp + 1
End While
myReader.Close()
If recProp > 0 Then
XMLwrite.WriteStartDocument()
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteStartElement("kml")
XMLwrite.WriteAttributeString("xmlns", "http://www.opengis.net/kml/2.2")
XMLwrite.WriteWhitespace(Environment.NewLine)
cmd.CommandText = "SELECT [propertyID],[propertyName],[propertyType],[perish],[className],[bedrooms],[baths],[price],[MLS],[physicalDescription],[agentName],[propertyLatitude],[propertyLongitude],[porpertyZlevel] FROM [properties] WHERE propertyStatus = 'Active'"
myReader = cmd.ExecuteReader()
If myReader.Read() Then
While i <= recProp
strDes = ""
strCod = ""
XMLwrite.WriteStartElement("Placemark")
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteElementString("name", myReader.GetSqlValue(1).ToString)
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteElementString("visibility", "1")
XMLwrite.WriteWhitespace(Environment.NewLine)
strDes = "<![CDATA[Class : <b>" + myReader.GetSqlValue(4).ToString + "</b><br>Price: " + myReader.GetSqlValue(7).ToString + " | MLS: " + myReader.GetSqlValue(8).ToString + "<br><b><a href=" + Chr(39) + "http://www.real-propertiesrus.com/property.aspx?p=" + myReader.GetSqlValue(0).ToString + "&returnURL=default.aspx" + Chr(39) + " Target=" + Chr(39) + "_parent" + Chr(39) + ">More details</a></b>]]>"
XMLwrite.WriteElementString("description", strDes.ToString)
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteStartElement("Point")
XMLwrite.WriteWhitespace(Environment.NewLine)
strCod = myReader.GetSqlValue(11).ToString + "," + myReader.GetSqlValue(12).ToString + "," + myReader.GetSqlValue(13).ToString
XMLwrite.WriteElementString("coordinates", strCod.ToString)
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteEndElement()
XMLwrite.WriteWhitespace(Environment.NewLine)
XMLwrite.WriteEndElement()
XMLwrite.WriteWhitespace(Environment.NewLine)
i = i + 1
myReader.Read()
End While
End If
XMLwrite.WriteEndDocument()
XMLwrite.Flush()
Dim writer As New IO.StreamWriter(Server.MapPath("myReal.kml"))
Dim reader As IO.StreamReader
stream.Position = 0
reader = New IO.StreamReader(stream)
writer.Write(reader.ReadToEnd().Replace("<", "<").Replace(">", ">"))
writer.Close()
reader.Dispose()
writer.Dispose()
Response.Redirect("Default.aspx?GOOGLE MAP KML GENERATED")
End If
End Sub
End Class
This code was working perfectly, however, recently, when user click on the "btnAdd" button, user gets a "save as" message instead of saving "myReal.kml" file to server.
Thank you all for helping out.