Quantcast
Channel: ClearScript
Viewing all articles
Browse latest Browse all 2297

New Post: for each item in collection loop does not work for datatable

0
0
Hi.

The code:
Imports System.Data.SqlClient
Imports Microsoft.ClearScript

Public Class WebForm1
    Inherits System.Web.UI.Page

    Public vbs As Windows.VBScriptEngine = New Windows.VBScriptEngine

    Public conn As SqlConnection = New SqlConnection("connection string")
    Public da As SqlDataAdapter = New SqlDataAdapter("select top 10 * from table", conn), dt As DataTable = New DataTable

    Public Sub Initialize()
        js.AddRestrictedHostObject("me", HostItemFlags.GlobalMembers, Me)
        vbs.AddRestrictedHostObject("me", HostItemFlags.GlobalMembers, Me)

        Try
            conn.Open()

            vbs.Execute("da.Fill dt")
            vbs.Execute("for i = 0 to dt.Rows.Count - 1:for j = 0 to dt.rows.item(i).table.columns.count - 1:response.write dt.rows.item(i).item(j) & "" - "":next:response.write ""<br>"":next")

            ' ------------------------------------------------------------
        ' THIS DOES NOT WORK
        vbs.Execute("for each row in dt.rows: for each col in row.table.columns: response.write row(col.columnname).tostring: next: next")
            ' ------------------------------------------------------------

        Catch ex As Exception
            Response.Write(ex)
        Finally
            da.Dispose()
            dt.Clear()
            dt.Dispose()
            conn.Close()
            conn.Dispose()
        End Try
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub
End Class
When I use for i = 0 to object.count - 1 it works, but the second line where I use for each loop it gives me this error:

Microsoft.ClearScript.ScriptEngineException: Object not a collection at Microsoft.ClearScript.ScriptEngine.ThrowScriptError(IScriptEngineException scriptError) in c:\Users\Shripal\Documents\Visual Studio 2012\Projects\ClearScript\ClearScript\ScriptEngine.cs:line 840 at Microsoft.ClearScript.Windows.WindowsScriptEngine.ThrowScriptError(Exception exception) in c:\Users\Shripal\Documents\Visual Studio 2012\Projects\ClearScript\ClearScript\Windows\WindowsScriptEngine.cs:line 642 at Microsoft.ClearScript.Windows.WindowsScriptEngine.<>c__DisplayClass14.b__13() in c:\Users\Shripal\Documents\Visual Studio 2012\Projects\ClearScript\ClearScript\Windows\WindowsScriptEngine.cs:line 611 at Microsoft.ClearScript.ScriptEngine.ScriptInvoke(Action action) in c:\Users\Shripal\Documents\Visual Studio 2012\Projects\ClearScript\ClearScript\ScriptEngine.cs:line 800 at Microsoft.ClearScript.Windows.WindowsScriptEngine.ScriptInvoke(Action action) in c:\Users\Shripal\Documents\Visual Studio 2012\Projects\ClearScript\ClearScript\Windows\WindowsScriptEngine.cs:line 603 at Microsoft.ClearScript.Windows.WindowsScriptEngine.Execute(String documentName, String code, Boolean evaluate, Boolean discard) in c:\Users\Shripal\Documents\Visual Studio 2012\Projects\ClearScript\ClearScript\Windows\WindowsScriptEngine.cs:line 523 at Microsoft.ClearScript.ScriptEngine.Execute(String documentName, Boolean discard, String code) in c:\Users\Shripal\Documents\Visual Studio 2012\Projects\ClearScript\ClearScript\ScriptEngine.cs:line 494 at Microsoft.ClearScript.ScriptEngine.Execute(String documentName, String code) in c:\Users\Shripal\Documents\Visual Studio 2012\Projects\ClearScript\ClearScript\ScriptEngine.cs:line 473 at Microsoft.ClearScript.ScriptEngine.Execute(String code) in c:\Users\Shripal\Documents\Visual Studio 2012\Projects\ClearScript\ClearScript\ScriptEngine.cs:line 454 at WebApplication1.WebForm1.Initialize() in C:\Users\Shripal\Documents\Visual Studio 2012\Projects\WebApplication1\WebApplication1\WebForm1.aspx.vb:line 37

Viewing all articles
Browse latest Browse all 2297