if (confirm("Are you sure you want to delete this record?")) {
$.ajax({
url: "DataHandler.ashx",
type: "POST",
data: {
action: "removeRecord",
id: recordId
},
dataType: "json",
success: function(response) {
// Refresh jExcel grid after successful deletion
document.getElementById('spreadsheet').jexcel.refresh();
},
error: function(xhr) {
alert("Deletion failed: " + xhr.responseText);
}
});
}
}
</div>View Code</div><div>Row selection logic — captures the ID from the first column of the clicked row and stores it in a hidden input field:</div><div><div>```
// Capture row ID on cell selection
function onCellSelect(instance, colStart, rowStart, colEnd, rowEnd, origin) {
// Assuming ID is in column A (index 0), current row = rowStart
const idCellAddress = jexcel.getColumnNameFromId([0, rowStart]);
const selectedId = instance.getValue(idCellAddress);
document.getElementById("hiddenRecordId").value = selectedId || "";
}
string action = context.Request["action"];
if (action == "removeRecord")
{
string idValue = context.Request["id"];
if (!int.TryParse(idValue, out int recordId))
{
context.Response.StatusCode = 400;
context.Response.Write("{\"error\":\"Invalid ID format\"}");
return;
}
string connectionString = "server=192.168.1.100;database=FinanceDB;uid=appuser;pwd=SecurePass123;";
const string deleteSql = "DELETE FROM outstanding WHERE id = @recordId";
try
{
using (var connection = new SqlConnection(connectionString))
{
using (var command = new SqlCommand(deleteSql, connection))
{
command.Parameters.AddWithValue("@recordId", recordId);
connection.Open();
int rowsAffected = command.ExecuteNonQuery();
if (rowsAffected == 0)
{
context.Response.StatusCode = 404;
context.Response.Write("{\"error\":\"Record not found\"}");
return;
}
}
}
context.Response.Write("{\"success\":true}");
}
catch (Exception ex)
{
context.Response.StatusCode = 500;
context.Response.Write($"{{\"error\":\"{ex.Message.Replace("\"", "\\\"")}\"}}");
}
}
}
</div>View Code</div>