calling sp in controller

calling sp in controller

montoyammontoyam Posts: 568Questions: 136Answers: 5

This not so much a datatables question other than it is in a asp.net mvc project that has datatables in it. I am having the user import a text file into a datatable: https://editor.datatables.net/examples/extensions/import

After it is imported and they have reviewed the data, I need the user to click a button to call a stored procedure. The stored procedure is parsing the data and putting into another table, which is the datasource for another datatable. But the stored procedure itself does not return any data. From what I am researching, since datatables uses MVC i need to put that call in a controller.

public class ParseImportDataController: ApiController
{
    [HttpGet]
    [HttpPost]
    public IHttpActionResult cleanAndImport()
    {
        var request = HttpContext.Current.Request;
        var settings = Properties.Settings.Default;
        string AsOfCookie = request.Cookies.Get("AsOfDate").Value;
 
        string strCon = settings.DbConnection;
        SqlConnection DbConnection = new SqlConnection(strCon);
        DbConnection.Open();
 
        SqlCommand command = new SqlCommand("sp_ImportFTE", DbConnection);
        command.CommandType = System.Data.CommandType.StoredProcedure;
        command.Parameters.Add(new SqlParameter("@EffectiveDate", AsOfCookie));
        command.ExecuteNonQuery();
        DbConnection.Close();
        return Ok(1); //no idea what to return
    }
}

I can't find out how I have the button click call this code in the controller. Can I use Buttons to add a custom button that will call the cleanAndImport function?

I have no idea if the code in the controller is correct, but I figure that will be the next struggle.

Any help would be greatly appreciated.

This question has an accepted answers - jump to answer

Answers

  • colincolin Posts: 15,237Questions: 1Answers: 2,599
    Answer ✓

    Duplicate of this thread.

  • allanallan Posts: 63,205Questions: 1Answers: 10,415 Site admin

    Yup - I've added a reply in the other thread (apologies - I wasn't able to reply to any threads yesterday).

    Allan

  • montoyammontoyam Posts: 568Questions: 136Answers: 5
    edited February 2020

    thanks. I realized the other thread was a discussion, not a question so it wouldn't show up in "Unanswered Questions". I wasn't sure if disucssions were tracked like 'Unanswered Questions' are.

  • colincolin Posts: 15,237Questions: 1Answers: 2,599

    Tbh I don't think anybody notices how it was created - they all go into the same list :)

This discussion has been closed.