Accessing SqlDataReader from ClearScript

Mar 5, 2014 at 6:37 AM
Hi,

This does not work:
Imports System.Data.SqlClient
Imports Microsoft.ClearScript

Public Class WebForm1
    Inherits System.Web.UI.Page

    Public engine As Windows.JScriptEngine = New Windows.JScriptEngine

    Public conn As SqlConnection = New SqlConnection("connection string")
    Public cmd As SqlCommand = New SqlCommand("select * from contacts", conn)
    Public rdr As SqlDataReader

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

        Try
            engine.Execute("conn.Open(); rdr = cmd.ExecuteReader();")

        Catch ex As Exception
            Response.Write(ex)
        Finally
            engine.Dispose()
            rdr.Close()
            rdr.Dispose()
            cmd.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
I get this error:

Microsoft.ClearScript.ScriptEngineException: Ambiguous match found. ---> System.Reflection.AmbiguousMatchException: Ambiguous match found. at System.DefaultBinder.SelectProperty(BindingFlags bindingAttr, PropertyInfo[] match, Type returnType, Type[] indexes, ParameterModifier[] modifiers) at System.RuntimeType.GetPropertyImpl(String name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) at System.Type.GetProperty(String name, BindingFlags bindingAttr, Type returnType) at System.Attribute.GetParentDefinition(PropertyInfo property) at System.Attribute.InternalGetCustomAttributes(PropertyInfo element, Type type, Boolean inherit) at System.Attribute.GetCustomAttributes(MemberInfo element, Type type, Boolean inherit) at System.Attribute.GetCustomAttribute(MemberInfo element, Type attributeType, Boolean inherit) at Microsoft.ClearScript.Util.MemberHelpers.GetAttribute[T](MemberInfo member, Boolean inherit) in ClearScript\Util\MemberHelpers.cs:line 136 at Microsoft.ClearScript.Util.MemberHelpers.GetScriptAccess(MemberInfo member) in ClearScript\Util\MemberHelpers.cs:line 124 at Microsoft.ClearScript.Util.MemberHelpers.IsBlockedFromScript(MemberInfo member) in ClearScript\Util\MemberHelpers.cs:line 97 at Microsoft.ClearScript.Util.MemberHelpers.IsScriptable(PropertyInfo property) in ClearScript\Util\MemberHelpers.cs:line 86 at Microsoft.ClearScript.Util.TypeHelpers.b__1a(PropertyInfo property) in ClearScript\Util\TypeHelpers.cs:line 300 at System.Linq.Enumerable.WhereSelectArrayIterator2.MoveNext() at System.Linq.Buffer1..ctor(IEnumerable1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable1 source) at Microsoft.ClearScript.HostItem.GetLocalPropertyNames() in ClearScript\HostItem.cs:line 451 at Microsoft.ClearScript.HostItem.GetAllPropertyNames() in ClearScript\HostItem.cs:line 497 at Microsoft.ClearScript.HostItem.UpdatePropertyNames(Boolean& updated) in ClearScript\HostItem.cs:line 557 at Microsoft.ClearScript.HostItem.b__39() in ClearScript\HostItem.cs:line 1236 at Microsoft.ClearScript.ScriptEngine.HostInvoke[T](Func1 func) in ClearScript\ScriptEngine.cs:line 784 at Microsoft.ClearScript.Windows.WindowsScriptEngine.HostInvoke[T](Func1 func) in ClearScript\Windows\WindowsScriptEngine.cs:line 573 --- End of inner exception stack trace --- at Microsoft.ClearScript.ScriptEngine.ThrowScriptError(IScriptEngineException scriptError) in ClearScript\ScriptEngine.cs:line 840 at Microsoft.ClearScript.Windows.WindowsScriptEngine.ThrowScriptError(Exception exception) in ClearScript\Windows\WindowsScriptEngine.cs:line 642 at Microsoft.ClearScript.Windows.WindowsScriptEngine.<>c__DisplayClass14.b__13() in ClearScript\Windows\WindowsScriptEngine.cs:line 611 at Microsoft.ClearScript.ScriptEngine.ScriptInvoke(Action action) in ClearScript\ScriptEngine.cs:line 800 at Microsoft.ClearScript.Windows.WindowsScriptEngine.ScriptInvoke(Action action) in ClearScript\Windows\WindowsScriptEngine.cs:line 603 at Microsoft.ClearScript.Windows.WindowsScriptEngine.Execute(String documentName, String code, Boolean evaluate, Boolean discard) in ClearScript\Windows\WindowsScriptEngine.cs:line 523 at Microsoft.ClearScript.ScriptEngine.Execute(String documentName, Boolean discard, String code) in ClearScript\ScriptEngine.cs:line 494 at Microsoft.ClearScript.ScriptEngine.Execute(String documentName, String code) in ClearScript\ScriptEngine.cs:line 473 at Microsoft.ClearScript.ScriptEngine.Execute(String code) in ClearScript\ScriptEngine.cs:line 454 at WebApplication1.WebForm1.Initialize() in WebApplication1\WebForm1.aspx.vb:line 17
Coordinator
Mar 5, 2014 at 3:59 PM
Greetings!

Unfortunately it looks like SqlDataReader currently cannot be used directly from script code. The root cause is this .NET bug.

We'll provide a workaround in the next ClearScript release. In the meantime, all we can recommend is that you avoid dealing with SqlDataReader in script code. Consider exposing an alternate API that encapsulates SqlDataReader within another .NET class.

Thanks for reporting this issue!
Mar 5, 2014 at 6:31 PM
Edited Mar 5, 2014 at 6:32 PM
Hey,

Well I had to take the longer route of using a SqlDataAdapter and DataSet and it worked but would have loved to use SqlDataReader... Hope your team can fix this soon for us... Thanks for the support!