Hi,
The file index.aspx contains a javascript tag which refers to another aspx file (test.aspx).
The 'test.aspx' file must fetch data from a database, put the data in a hashtable and finally create an javascript object with the hashtable.
I can do everything except creating a javascript object. I have the solution in PHP, but cannot convert it in asp.net code.
Here the code:
index.aspx
----------
<%@ Page Language="VB" %>
<html>
<head></head>
<body>
<form ID="form1" runat="server" />
...
...
</form>
</body>
<script type="text/javascript" src="scripts/test.aspx"></script>
</html>
---------------------------------------------------
test.aspx
---------
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Collections.Generic" %>
<script runat="server">
Sub page_load()
Response.AddHeader("Content-Type", "text/javascript")
Dim comd As SqlCommand
Dim sql As String
Dim myhash = New Hashtable()
'here the records are fetched from the database and put in the hashtable
'i didn't put the whole code to keep it simple but this works
Using mConnection As New SqlConnection(param.ConnectionString)
mConnection.Open()
sql = "select nm from mytable"
...
If dtreader.HasRows Then
While dtreader.Read()
nm = dtreader.GetString(0)
if nm="1" then
myhash.Add(nm, "yes")
else
myhash.Add(nm, "no")
End While
End If
dtreader.Close()
End Using
'now i need to create an javascript object using the hashtable
'this PHP code: $myhash is the associative array equivalent to the hashtable
print("var mystat = new Object();\n");
foreach ($myhash as $name => $value)
{
print("mystat.$name = '$value';\n");
}
End Sub
</script>
-------------------------------------
I know how to get the keys and values of a hashtable in asp.net, but my problem is: how to do the same as the PHP code?
Dim objKey As Object
Dim objValue As Object
For Each objKey In locatiedata.Keys
objValue = locatiedata.Item(objKey)
Response.Write("PCstatusobjKey.ToString & " : " & objValue.ToString & "<br>")
Next objKey
Thanks
Tartuffe