I cannot find any examples in the help files to show how to create a button in a custom file that can call the ExecuteScalar function. I need to simple call a MSSQL stored procedure by clicking a button. Here is what I have so far. It compiles but does not do anything when clicking the button. Can you tell me what I am doing wrong?
<button id="sendButton" type="button" class="btn btn-primary">Send</button>
<script>
document.getElementById("sendButton").click(function(event) {
object value = ExecuteScalar("Execute dbo.usp_Transmit_JSON");
});
</script>
You cannot mix server codes with client codes. You can add your own API action to perform the client side request. Read “Create your own API action” in help file topic: REST API.
All I want to do is run a simple stored procedure with a button click. This is not related to the API itself. Do you have a simple example? This information is not in the help file you directed me to.
As explained, you cannot mix client codes and server codes or running server codes in client side. So Ajax request to the server is required. The simplest option will be to write your own API request as suggested.
In a grid, you could add a ListActionHere is some code
public void Page_Load() {
ListActions.Add("workorder", "Unlock Work Order", IsLoggedIn(), Config.ActionPostback, Config.ActionSingle, "Are you sure you want to unock the selected WorkOrder?", "fa fa-star ew-icon");
}
and in Row_CusotmAction
public bool Row_CustomAction(string action, Dictionary<string, object> row) {
if (action == "workorder") {
try {
var dbhelper = GetConnection("DB");
var comm = dbhelper.GetStoredProcCommand("UpdateWorkOrderByID");
comm.Parameters.Add("@id", SqlDbType.VarChar);
comm.Parameters["@id"].Value = Convert.ToString(row["Id"]);
comm.ExecuteNonQuery();
}
catch(SqlException ex) {
StringBuilder errorMessages = new StringBuilder();
for (int i = 0; i < ex.Errors.Count; i++) {
errorMessages.Append("Index #" + i + "\n" +
"Message: " + ex.Errors[i].Message + "\n" +
"LineNumber: " + ex.Errors[i].LineNumber + "\n" +
"Source: " + ex.Errors[i].Source + "\n" +
"Procedure: " + ex.Errors[i].Procedure + "\n");
}
FailureMessage = "Failed to update record, ID = " + Convert.ToString(row["Id"]) + "\n" + errorMessages.ToString();
return false;
}
}
SuccessMessage = "WorkOrder with ID: \"" + Convert.ToString(row["Id"]) + "\" unlocked sucessfully!";
return true;
}
similar in other pages like Add/Edit/Delete you can have OtherActions - see docs.