This topic is locked
[SOLVED]

What is the best way to run a Stored Procedure after I save on an edit page?

6/16/2025 4:23:09 PM
ASPRunner.NET General questions
M
MSchell author

When my user saves a record on an Edit page, I want to run a stored procedure and pass a value from the edit page to the SP.

How can I acomplish this?

ChatGPT 6/16/2025

Certainly! To run a stored procedure from an ASPRunner.NET application when a user saves a record on an Edit page, you can use the following steps:

Steps to Execute a Stored Procedure on Save

  1. Open your ASPRunner.NET project:
    Launch ASPRunner.NET and open your project where you want to implement this functionality.


  2. Navigate to the Edit Page:
    Go to the Edit page where you want to execute the stored procedure after saving a record.


  3. Use the After Edit Event:
    ASPRunner.NET provides various events you can hook into. For your use case, you will want to use the After Edit event, which is triggered after the record is saved.


  4. Access the Stored Procedure:
    In the After Edit event, you can call your stored procedure using ADO.NET. You will need to write some C# code to do this.


  5. Example Code:
    Here’s an example of how you might implement this:

    protected void AfterEdit(object sender, EventArgs e)
    {
    // Get the value from the edit page
    string valueToPass = Request.Form["YourFieldName"];

    using (SqlConnection conn = new SqlConnection("YourConnectionString"))
    {
    using (SqlCommand cmd = new SqlCommand("YourStoredProcedureName", conn))
    {
    cmd.CommandType = CommandType.StoredProcedure;

    // Pass the value from the edit page to the stored procedure
    cmd.Parameters.Add(new SqlParameter("@YourParameterName", valueToPass));

    conn.Open();
    cmd.ExecuteNonQuery();
    }
    }
    }

    Note: Replace "YourFieldName" with the name of the field you want to pass to the stored procedure, "YourConnectionString" with your actual database connection string, "YourStoredProcedureName" with the name of your stored procedure, and "@YourParameterName" with the parameter name expected by your stored procedure.


  6. Testing:
    After implementing the above code, compile your project and test the Edit page. When you save the record, it should now call the stored procedure and pass the specified value.



Important Considerations

  • Error Handling: It’s a good idea to add error handling around your database access code to gracefully handle any exceptions that might occur.
  • Security: Ensure that your database connection string is secure and that you are handling input parameters correctly to prevent SQL injection.
  • Parameter Types: Make sure that the data types of the parameters you’re passing match those defined in your stored procedure.

By following these steps, you should be able to execute a stored procedure with a value passed from the edit page effectively. If you have any further questions or need more assistance, feel free to ask!

Sergey Kornilov admin 6/16/2025

Did you have a chance to check this relevant article in the manual?
https://xlinesoft.com/asprunnernet/docs/how_to_execute_stored_procedures_from_events.htm

M
MSchell author 6/18/2025

Edit Page - After Record updated

string sql = DB.PrepareSQL("EXEC SendSSIS_PTF_EMAIL ':1'", values["PTFTrackingNumber"].ToString());
DB.Exec(sql);

Doesn't appear to work, builds successfully, no errors

Sergey Kornilov admin 6/19/2025

Did you perform any troubleshooting?

I.e. did you try to hardcode the stored procedure parameter?

Did you use SQL Server profiler to see the exact SQL query of stored procedure being executed?

M
MSchell author 6/20/2025

this Stored Procedure was running an SSIS Integration Package. It appears to be a security issue that to execute an SSIS package from a Stored Procedure requires a windows account and not a SQL account "The CLR methods specifically reject non-Windows authentication" I do have a Proxy account, but can't figure out how to pass it downstream for the Web Server to use it.

Any suggestions?

The error usin a SQL account is:
The server principal "NSPInquiry" is not able to access the database "SSISDB" under the current security context.

The method referenced didn't return any errors. I changed it to this, and was able to capture the error:

string PTFNumber = values["PTFTrackingNumber"].ToString();
tDAL.CustomQuery("EXEC [SendSSIS_PTF_EMAIL] @PTFNumberProjParameter = N'" + PTFNumber + "'");

Sergey Kornilov admin 6/20/2025

This is not something we can help you with. Talk to your web server or SQL Server admin. Maybe they know how to configure it properly.