This topic is locked
[SOLVED]

 Return Enum from SQL View using C#

7/25/2020 1:40:05 PM
ASPRunner.NET General questions
C
cboucher author

Is there an example of how to use C# code to return data for a SQL View in C# Mode? I need to return a list of Enum names and values so it can be used in a drop list. I've figured out how to get the list of names and values using Linq, but I don't know how to convert it to a proper XVar that ASPRunner.Net is expecting.
Thanks,

Craig

FrankR_ENTA 7/25/2020



Is there an example of how to use C# code to return data for a SQL View in C# Mode? I need to return a list of Enum names and values so it can be used in a drop list. I've figured out how to get the list of names and values using Linq, but I don't know how to convert it to a proper XVar that ASPRunner.Net is expecting.
Thanks,

Craig


Check this out. <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=92084&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' /> I went to the PHPRunner help, which has a complete example. See here: https://xlinesoft.com/phprunner/docs/code-views.htm
Then I ran that code sample through the Xlinesoft PHP to C# converter here: https://xlinesoft.com/php2dotnet/
Yielding this code below. I'm about to leave the house; otherwise, I would test it for you. But give it a try.



dynamic command = null, dataSource = null, result = null;

data = XVar.Clone(XVar.Array());

data.InitAndSetArrayItem(new XVar("id", 1, "name", "aaa"), null);

data.InitAndSetArrayItem(new XVar("id", 2, "name", "bbb"), null);

data.InitAndSetArrayItem(new XVar("id", 3, "name", "ccc"), null);

result = XVar.Clone(new ArrayResult((XVar)(data)));

if(XVar.Pack(!(XVar)(result)))

{

dataSource.setError((XVar)(DB.LastError()));

return false;

}

result = XVar.Clone(dataSource.filterResult((XVar)(result), (XVar)(command.filter)));

dataSource.reorderResult((XVar)(command), (XVar)(result));

return result;

FrankR_ENTA 7/25/2020

I couldn't resist. Had to test and get it working. Now I'm going to be late. <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=92085&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' />
That code above was close but not it. Needed some tweaks.
Here it is:


dynamic vdataSource = null;



dynamic data = XVar.Clone(XVar.Array());

data.InitAndSetArrayItem(new XVar("id", 1, "name", "aaa"), null);

data.InitAndSetArrayItem(new XVar("id", 2, "name", "bbb"), null);

data.InitAndSetArrayItem(new XVar("id", 3, "name", "ccc"), null);



dynamic result = XVar.Clone(new ArrayResult((XVar)(data)));



if(XVar.Pack(!(XVar)(result)))

{

vdataSource.setError((XVar)(DB.LastError()));

return false;

}



return result;


C
cboucher author 7/25/2020

Thanks so much! That worked perfectly. Here is my final code.

dynamic vdataSource = null;

dynamic data = XVar.Clone(XVar.Array());
var parities = Enum.GetValues(typeof(System.IO.Ports.Parity))

.Cast<System.IO.Ports.Parity>()

.Select((x, y) => new XVar("ParityEnum", y, "ParityName", x));



foreach (var x in parities)

data.InitAndSetArrayItem(x, null);
dynamic result = XVar.Clone(new ArrayResult((XVar)(data)));



if(XVar.Pack(!(XVar)(result)))

{

vdataSource.setError((XVar)(DB.LastError()));

return false;

}
return result;