This topic is locked
[SOLVED]

Calling a stored procedure with parameters from a button server code in C#

6/22/2022 3:39:34 PM
ASPRunner.NET Tips and tricks
R
Rajesh author

I want to run a stored procedures in ASPrunner.net button for a record. I am looking for an example including C# server code to call a stored procedure from sql server with parameters.
Thanks in advance !

Rajesh

admin 6/22/2022

Check this article in the manual.

R
Rajesh author 6/23/2022

Hi Admin,

Thanks for the link.
I am new to C#.
I need to have 2 parameters, one is the fieldname of the current record and the other is session username. I am trying with just one variable first but getting the followin error.
"include\CommonEvents.cs(60,57): error CS0103: The name 'values' does not exist in the current context".
At this moment, I am using only the on server code in a button on a list page. (see attachment)

img alt

Thanks,
Rajesh

R
Rajesh author 6/23/2022

Here is what I tried but with an error while build
"include\CommonEvents.cs(61,73): error CS1525: Invalid expression term 'params'"
It looks like parameters passing from client to server has some issue, any help is appreciated.

-Rajesh

==========================================================================
Client Before:
params["ModifiedBy"] = $SESSION["UserID"];

Server:
// Call procedure with 2 parameters ID and parameter ModifiedBy
dynamic record = button.getCurrentRecord();
result["ID"] = record["ID"];
//string sql = DB.PrepareSQL("EXEC spSetMODcurrent ':1'", values["ID"].ToString());
string sql = DB.PrepareSQL("EXEC spSetMODcurrent ':1,:2'", result["ID"],params["ModifiedBy"]);
DB.Exec(sql);

P
ppradhan@live.com 6/23/2022

@Rajesh, not sure if session in phprunner and asprunner is same, but we use $_SESSION["UserID"] in phprunner.
You have used $SESSION without underscore....

R
Rajesh author 6/23/2022

I am using Netrunner10.6 , server code is working with hardcoded values now.
Going forward to find how to get the ID from current record and current session userid and incorporate it into DB.PrepareSQL.

string sql = DB.PrepareSQL( "EXEC sp_SetMODcurrent 4498,'Test' ");
DB.Exec( sql );

R
Rajesh author 6/23/2022

Following server code is partially working(ID is working), but I am still not able to add the current username, temporarily using 'Test' as username for testing purpose.
Can someone suggest what how can I capture the username in client before, and use in the server code in the
string sql as second parameter. I am using ASPrunner.NET 10.6
Thanks!

dynamic record = button.getCurrentRecord();
result["ID"] = record["ID"];
string sql = "exec sp_SetMODcurrent " + result["ID"].ToString() + ", Test";
DB.Exec( sql );

T
Tim 6/24/2022

Rajesh,

Try this in the server section of the button:

XVar data = button.getCurrentRecord();

string sql = DB.PrepareSQL("exec sp_SetMODcurrent " + data["ID"].ToString() + ",'" + XSession.Session["Username"].ToString() + "'");
DB.Exec( sql );

I hope it helps. Good luck.

And you may want to post questoins like this in the "ASPRunner.Net General questions" forum instead of the tips and tricks. I think you'll get more responses.

Thanks,
Tim

jadachDevClub member 6/24/2022

Do this:

XVar data = button.getCurrentRecord();
string sql = DB.PrepareSQL("EXEC sp_SetMODcurrent ':1', ':2'", data["ID"].ToString(), XSession.Session["UserID"].ToString());
DB.Exec(sql);
R
Rajesh author 6/25/2022

@jadach, @Tim and Admin.
Thanks you for the solution and suggestion.
It worked.

Rajesh