I have an update panel. The contents of which contain a Google Map and a multiline text box in which users can paste Postal Codes. The Postal Codes are taken and used in a WHERE IN (list) mysql statement to return the latitude and longitude from our database. Once the Dataset is complete it is turning into a JSON string for create map markers.
The update panel is causing issues so though I would remove it, however in doing so the Dataset only gets the last record from the last entry in the list and the JSON data equally only contain the last entry.
Here is my asps code:-
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table border="0px" style="width: 100%; font-family: Arial, Helvetica, sans-serif; font-size: small; color: #000000;">
<tr>
<td class="style15" valign="top">
<table border="0px" style="border: 1px solid #000000; font-family: Arial, Helvetica, sans-serif; font-size: small; color: #000000;"width="100%">
<tr>
<td align="center" class="style14" colspan="2">
<asp:TextBox ID="PostcodeListTextBox" runat="server" Height="500px"
Width="240px" TextMode="MultiLine"></asp:TextBox>
</td>
</tr>
<tr>
<td align="center" class="style14" colspan="2">
<asp:Button ID="ShowLocsButton" runat="server" Text="Show Locations" />
</td>
</tr>
</table>
</td>
<td align="center" valign="top">
<div id="mapArea" style="border:1px solid black">
</div>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
And here is my code behind:-
Imports MySql.Data.MySqlClient
Imports MySql.Data
Imports System.Data
Imports System.Web
Imports System.IO
Imports System.Configuration
Imports System.Web.Script.Services
Imports System.Web.Script.Serialization
Partial Class admin_routing
Inherits System.Web.UI.Page
Dim i As Integer = 0
Dim markers As String = ""
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub ShowLocsButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ShowLocsButton.Click
Dim ConnString As String = ConfigurationManager.ConnectionStrings("ConnString").ConnectionString
Dim conn As New MySqlConnection(ConnString)
Dim command As New MySqlCommand
Dim ds As New DataSet
Dim sql As String
Dim dt As New DataTable
Dim markers As String = ""
Dim i As Integer = 0
Dim PCList As String = Replace(Me.PostcodeListTextBox.Text, vbLf, "','")
Dim da As New MySqlDataAdapter
Dim myJS As New StringBuilder
PCList = "'" & PCList & "'"
sql = "SELECT postcode, latitude,longitude FROM geocodes WHERE postcode IN (" & PCList & ")"
da = New MySqlDataAdapter(sql, conn)
da.Fill(ds, "locations")
Dim jsonData = GetJson(ds.Tables(0))
'Dim jsondata = DataSetToJSON(ds)
'Me.JsonTextBox.Text = jsonData
myJS.AppendLine("<script type=""text/javascript"">" & vbCrLf)
myJS.AppendLine("var mapOptions = {center: new google.maps.LatLng(54.236107, -4.548055999999974)," & vbCrLf)
myJS.AppendLine("zoom: 6," & vbCrLf)
myJS.AppendLine("mapTypeId : google.maps.MapTypeId.ROADMAP" + "};" & vbCrLf)
myJS.AppendLine("var myMap = new google.maps.Map(document.getElementById('mapArea'), mapOptions);" & vbCrLf)
myJS.AppendLine("var markers = JSON.parse(""<%=jsonData %>"");" & vbCrLf)
myJS.AppendLine("console.log(""<%=jsonData %>"");" & vbCrLf)
myJS.AppendLine("for (i = 0; i < markers.length; i++) {" & vbCrLf)
myJS.AppendLine("var data = markers[i];" & vbCrLf)
myJS.AppendLine("var myLatLng = new google.maps.LatLng(data.latitude, data.longitude);" & vbCrLf)
myJS.AppendLine("var marker = new google.maps.Marker({" & vbCrLf)
myJS.AppendLine("position: myLatLng," & vbCrLf)
myJS.AppendLine("map: myMap," & vbCrLf)
myJS.AppendLine("title: data.postcode" & vbCrLf)
myJS.AppendLine("});" & vbCrLf)
myJS.AppendLine("}" & vbCrLf)
myJS.AppendLine("</script>")
Dim str As String = myJS.ToString
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "Init", myJS.ToString, True)
End Sub
Public Shared Function GetJson(ByVal dt As DataTable) As String
Dim serializer As New System.Web.Script.Serialization.JavaScriptSerializer()
Dim rows As New System.Collections.Generic.List(Of System.Collections.Generic.Dictionary(Of String, Object))()
Dim row As System.Collections.Generic.Dictionary(Of String, Object) = Nothing
For Each dr As DataRow In dt.Rows
row = New System.Collections.Generic.Dictionary(Of String, Object)()
For Each col As DataColumn In dt.Columns
row.Add(col.ColumnName, dr(col))
Next
rows.Add(row)
Next
Return serializer.Serialize(rows)
End Function
End Class
Also I'm having trouble passing the JSON to the javascript.