locked
error in function c# that returns null to a javascript variable RRS feed

  • Question

  • User1557998713 posted

    I have a function <%%> that returns different types of values to a javascript variable.

    1.- If null is returned, either dynamic null or null string, the error occurs:
    The call is ambiguous between the following methods or properties: 'System.IO.TextWriter.Write (string, params object [])' and 'System.IO.TextWriter.Write (char [])' "} HResult -2146233088

    2.- <%%> <- What is this called inside an .aspx called?

    function Fcn_Main()
    {   try
        {   var Test  = <%=Fcn_Test("STRING")%>;
            var Test1 = <%=Fcn_Test("INTEGER")%>;
            var Test2 = <%=Fcn_Test("ERROR")%>;  // ERROR. 
        }catch(ErrorExcp)}
    }
    
    <%dynamic Fcn_Test(string Opcion)
    {   dynamic MyDyn = null;
    
        switch (Opcion)
        {   case "STRING":  MyDyn = "IS CORRECT"; break;
            case "INTEGER": MyDyn = 0; break;
            case "ERROR":   break;
        }
        return MyDyn;
    }%>

    Solved my problem.

    In the case of:var Test2 = <%=Fcn_Test("ERROR")%>;  javascript needs to compose: var Test2 = null;

    in Fcn_Test() I add:

    if (MyDyn != null && MyDyn is string) MyDyn = "'" + MyDyn + "'";

    if (MyDyn == null) MyDyn = "null";

    Friday, July 3, 2020 4:46 AM

Answers

  • User753101303 posted

    Hi,

    Keeping your current approach, maybe something such as :

    switch (Opcion)
        {   case "STRING":  MyDyn = "'IS CORRECT'"; break; //quoted string
            case "INTEGER": MyDyn = 0; break;
            case "ERROR":  MyDyn="null"; break;
        }

    Depending on your actual code you could likely use a class such as https://docs.microsoft.com/en-us/dotnet/api/system.web.script.serialization.javascriptserializer?view=netframework-4.8 to have the correct string representation being written for you regardless of the actual server side value you are trying to render. I assume your actual code is starting from a strongly typed C# variable?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 3, 2020 11:38 AM

All replies

  • User288213138 posted

    Hi zequion,

    1.- If null is returned, either dynamic null or null string, the error occurs:
    The call is ambiguous between the following methods or properties: 'System.IO.TextWriter.Write (string, params object [])' and 'System.IO.TextWriter.Write (char [])' "} HResult -2146233088

     If it is null, ASP.NET doesn’t know what method to call.

    2.- <%%> <- What is this called inside an .aspx called?

    Do you want to know the function of <%%> in aspx? if so you can refere to below link, this article contains an introduction to the following ASP.NET inline expressions.

    Introduction to ASP.NET inline expressions in the .NET Framework

    The embedded code block is used to preserve backward compatibility with classical ASP. The code in the block can execute programming statements and call functions in the current page class during the page-rendering phase.

    Best regards,

    Sam

    Friday, July 3, 2020 7:24 AM
  • User1557998713 posted

    And then what is there to do? because the problem is right there.

    Friday, July 3, 2020 7:56 AM
  • User288213138 posted

    Hi zequion,

    And then what is there to do? because the problem is right there.

    You only need to set MyDyn to not be null when Opcion is ERROR.

    Best regards,

    Sam

    Friday, July 3, 2020 8:02 AM
  • User1557998713 posted

    ok but i prefer a technical solution because the real function returns many values and i can't return what i want.

    Friday, July 3, 2020 10:31 AM
  • User753101303 posted

    Hi,

    Keeping your current approach, maybe something such as :

    switch (Opcion)
        {   case "STRING":  MyDyn = "'IS CORRECT'"; break; //quoted string
            case "INTEGER": MyDyn = 0; break;
            case "ERROR":  MyDyn="null"; break;
        }

    Depending on your actual code you could likely use a class such as https://docs.microsoft.com/en-us/dotnet/api/system.web.script.serialization.javascriptserializer?view=netframework-4.8 to have the correct string representation being written for you regardless of the actual server side value you are trying to render. I assume your actual code is starting from a strongly typed C# variable?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 3, 2020 11:38 AM