This topic is locked
[SOLVED]

 Button to execute a stored procedure

6/3/2020 9:52:43 PM
ASPRunner.NET General questions
Lowkeck authorDevClub member

I have a stored procedure that simply merges data from 2 tables and inserts the records into a 3rd table. Is there any way to add a button that simply executes the stored procedure? I don't even need to pass any values with this SP. Something like this.
CustomQuery("EXEC populate_schedules");

FrankR_ENTA 6/4/2020

Look in the DB API. One of the functions will execute any SQL statement. Your SQL statement will be an invocation of the proc.
Then, using a custom button, you will be able to use C# to invoke the DB API function.

T
Tim 6/4/2020

https://xlinesoft.com/asprunnernet/docs/how_to_execute_stored_procedures_from_events.htm
As you can see from the help file, it depends on which DB you use. If you are using MSSQL (like me) then, in the server part of the button code, you do it like this:



tDAL.CustomQuery("EXEC StoredProcNameHere");


I use stored procedures all the time. Because I can't properly format SQL statements in ASPR events, I have started doing any SQL needed in a stored procedure and then just calling it (and passing parameters, if needed) from ASPR. It keeps the code (both ASPR and SQL) much cleaner and easier to review later.
Good luck.

Tim

FrankR_ENTA 6/4/2020



https://xlinesoft.com/asprunnernet/docs/how_to_execute_stored_procedures_from_events.htm
As you can see from the help file, it depends on which DB you use. If you are using MSSQL (like me) then, in the server part of the button code, you do it like this:



tDAL.CustomQuery("EXEC StoredProcNameHere");


I use stored procedures all the time. Because I can't properly format SQL statements in ASPR events, I have started doing any SQL needed in a stored procedure and then just calling it (and passing parameters, if needed) from ASPR. It keeps the code (both ASPR and SQL) much cleaner and easier to review later.
Good luck.

Tim


Stored Procedures are a good way to approach it.
Note, though, that with the SQL View support that was added in 10.4, you can copy and paste SQL specific to your DB, if you choose.

T
Tim 6/4/2020

Thanks Frank. My understanding of SQL Views in 10.4 (which is something I haven't played with yet) is that you can now use a SP instead of a table, which is great. But I was talking more about SQL statements in the events. I got tired of the SQL being in one long sting, like this:



string sql = "update mytable set field1 = '" + values("foo").ToString() + "', field2 = '" + values("bar").ToString() + "', updatedon = GetDate() where id = 123";

CommonFunctions.db_exec(sql, null);

[/code]
That one's actually not that bad, but you get the idea. Sometimes I have to scroll a mile to the right to see the entire statement.

I prefer to put that SQL in a nicely formatted SP and in the ASPR event just do:



tDAL.CustomQuery("EXEC MySP @para1 = '" + values("foo").ToString() + ', @para2 = '" + values("bar").ToString() + "';");


Sorry for the long example but I thought I'd put it in there in case someone looks at it a says "that's crazy. you really should do it this way". I'm always open to better ways of doing things.
Thanks,

Tim

FrankR_ENTA 6/4/2020



Thanks Frank. My understanding of SQL Views in 10.4 (which is something I haven't played with yet) is that you can now use a SP instead of a table, which is great. But I was talking more about SQL statements in the events. I got tired of the SQL being in one long sting, like this:



string sql = "update mytable set field1 = '" + values("foo").ToString() + "', field2 = '" + values("bar").ToString() + "', updatedon = GetDate() where id = 123";

CommonFunctions.db_exec(sql, null);

[/code]
That one's actually not that bad, but you get the idea. Sometimes I have to scroll a mile to the right to see the entire statement.

I prefer to put that SQL in a nicely formatted SP and in the ASPR event just do:



tDAL.CustomQuery("EXEC MySP @para1 = '" + values("foo").ToString() + ', @para2 = '" + values("bar").ToString() + "';");


Sorry for the long example but I thought I'd put it in there in case someone looks at it a says "that's crazy. you really should do it this way". I'm always open to better ways of doing things.
Thanks,

Tim


Tim, I originally had the same belief, that it was for Stored Proc invocation, but it is more than that. You can copy and paste SQL there. I have done it.

But yeah, using a proc is for most cases the best way to do it.

Lowkeck authorDevClub member 6/4/2020



I have a stored procedure that simply merges data from 2 tables and inserts the records into a 3rd table. Is there any way to add a button that simply executes the stored procedure? I don't even need to pass any values with this SP. Something like this.
CustomQuery("EXEC populate_schedules");


Thanks Frank & Tim! That was exactly what I was looking for. I am SQL guy, a bit clueless with C# and VB.Net. Thankyou!