Click here to Skip to main content
15,883,813 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am having trouble passing variables from my visual basic array to my javascript. The data is being pulled from an access database. Here is a sample of my code:

visual basic code with embedded javascript:
VB
Dim i
    for i=0 to numRecords-1
    datelabels(i) = rsGraph("DM")
    waterlevels(i) = rsGraph("WLVB")
    rsGraph.MoveNext
    next


...

C#
Response.Write("$(document).ready(function(){")

 'type mismatch Microsoft VBScript runtime error '800a000d' 

    Response.Write("var waterArray = ['" & datelabels & "'," & waterlevels & "];")

Response.Write("var plot2 = $.jqplot('chart2', [waterArray], {")


...

I was thinking that maybe I needed another for loop somewhere around my "var waterArray" array. Any help is appreciated.

Liz
Posted
Updated 18-Feb-15 17:17pm
v2
Comments
Sergey Alexandrovich Kryukov 18-Feb-15 16:18pm    
Do you mean VB.NET and ASP.NET? Anything else?
Such "type mismatch" never really takes place. Your problem is different...
—SA
Liz Heal 18-Feb-15 16:30pm    
I am using classic asp with vbscript for database access. I am creating arrays from that data in visual basic and then passing them into my javascript function, which uses jquery, to display a line chart. The error is pointing to the ['" & datelabels... waterlevels...

Frist, I tried changing that line to datelabels() with "i" as the subscript. Then I got an error saying the subscript is out of range.

Next, I tried adding a loop around that line to introduce another subscript. There were no errors at that point, but nothing showed up in my chart.
Sergey Alexandrovich Kryukov 18-Feb-15 16:45pm    
You should have clearly identify that in your question. There is no such language as just "VB".
Nevertheless, my answer is fully applicable; I'll add only one change "VB (whatever it is)"
—SA
Sergey Alexandrovich Kryukov 18-Feb-15 16:51pm    
...I also had to remove something else related to .NET. My advice still will work. By the way, any specific reason to live such hard life as using classic ASP and VB? :-)

Index out of range has nothing to do with "type mismatch" (again, there cannot be such thing is your case). Just checkup indices on the JavaScript which was actually generated. Besides, how about using JavaScript debugger.

—SA
Liz Heal 19-Feb-15 14:39pm    
I do like using the .NET framework using visual studio! Unfortunately, there are compatibility issues with the server here at work so I can't use a lot of that functionality :)

Thank you for responding to my post. I have a better understanding now :)

I have decided to post the solution to my own question in case there are developers with the same problem. As Sergey suggested in his solution above, JavaScript cannot read arrays taken directly from VBScript/Classic ASP. JavaScript is a very basic language and I was treating it like an object-oriented language. Therefore, the JavaScript must be written using Classic ASP/VBScript inside its array brackets.

Step 1:
Here is my database command in Classic Asp and SQL with the beginning of a loop statement:

Set rsChart = Server.CreateObject("ADODB.Recordset")
strSQLe = "SELECT”

rsChart.Open strSQLe, objConnection, adOpenStatic

If not rsChart.EOF And not rsChart.BOF then …

Step 2:

Here is the first part of my JavaScript function showing my usage of Classic ASP inside the JavaScript array brackets:
var rsWell =[<%

While Not rsChart.EOF

Response.Write(", ['" & rsChart("DM") & "', " & rsChart("WLVB") & "]")

rsChart.MoveNext

%>];

I had to add in the commas to separate the array pairs to enable parsing by the JavaScript. To get rid of the extra comma afterwards, I added a slice method:

var rsWell2 = rsWell.slice(1);

Step 3:

I could then pass the array directly into a JQuery plot function:
var plot1 = $.jqplot('chart', [rsWell2], {

Anyway, I hope this makes sense and helps someone else that is unfamiliar with the interaction of JavaScript and VBScript/Classic ASP.

-Liz H.
 
Share this answer
 
v2
There is no such problem by at least two important reasons.

First of all, there cannot be "type mismatch between VB and JavaScript", because JavaScript simply does not have the types in the same sense VB (whatever it is :-)) types are understood. JavaScript types are only 8 to 9 (one is implementation-dependent) and are described by strings: "undefined", "null", "string"… "object", (while .NET or non-.NET VB) allow any number of distinct types. See also: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof[^].

The second reason is this: if you are using ASP.NET or any other Web technology (and also a number of other, non-Web technologies), you never have any exchange between JavaScript and server-side objects. Never. Instead, you just generate some JavaScript text on the server side and put it in HTTP response. A Web browser gets the HTTP response, parses and handle it accordingly, which ultimately may lead to execution of some JavaScript code. You need to consider the JavaScript generated on server side as pure text. That's all.

Now, the above is the key to the solution. First of all, open the page in your browser, use View -> Page Source (or something like that, depending on your browser) and inspect actually generated JavaScript text. Makes sure it is correct. If not, find out what's wrong is going on the server side, where you can use the debugger. That's all.

—SA
 
Share this answer
 
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900