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 + "'";
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.
zequion
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.
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
IIS.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
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
IIS.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
Member
6 Points
39 Posts
error in function c# that returns null to a javascript variable
Jul 03, 2020 04:46 AM|zequion|LINK
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?
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";
Contributor
3370 Points
1409 Posts
Re: error in function c# that returns null to a javascript variable
Jul 03, 2020 07:24 AM|samwu|LINK
Hi zequion,
If it is null, ASP.NET doesn’t know what method to call.
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
Member
6 Points
39 Posts
Re: error in function c# that returns null to a javascript variable
Jul 03, 2020 07:56 AM|zequion|LINK
And then what is there to do? because the problem is right there.
Contributor
3370 Points
1409 Posts
Re: error in function c# that returns null to a javascript variable
Jul 03, 2020 08:02 AM|samwu|LINK
Hi zequion,
You only need to set MyDyn to not be null when Opcion is ERROR.
Best regards,
Sam
Member
6 Points
39 Posts
Re: error in function c# that returns null to a javascript variable
Jul 03, 2020 10:31 AM|zequion|LINK
ok but i prefer a technical solution because the real function returns many values and i can't return what i want.
All-Star
48720 Points
18184 Posts
Re: error in function c# that returns null to a javascript variable
Jul 03, 2020 11:38 AM|PatriceSc|LINK
Hi,
Keeping your current approach, maybe something such as :
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?