Stored Procedure in a web form view RRS feed

  • Question

  • User-2068991853 posted

    Hey guys,

    I have a web form view on Visual Studio 2013 built out with the following code:

    @model contoso.Models.WAPIaaSVMName
        ViewBag.Title = "New";
        Layout = "~/Views/Shared/_Layout.cshtml";
    @using (Html.BeginForm("Add", "WAPIaaSVMName", FormMethod.Post, new { @class = "form" }))
        <div class="form-group">
            <b>IaaS Virtual Machine Name</b>
            @Html.TextBoxFor(m => m.IaasVMName, new { @class = "form-control", required = "required" })
        <div class="form-group">
            <b>Security Configuration</b>
            @Html.TextAreaFor(m => m.SecurityClassification, new { @class = "form-control", required = "required", maxlength = "4" })
        <div class="form-group">
        List<SelectListItem> listItems = new List<SelectListItem>();
        listItems.Add(new SelectListItem
                 Text = "True",
                 Value = "True"
        listItems.Add(new SelectListItem
                 Text = "False",
                 Value = "False",
            @Html.DropDownListFor(m => m.assigned, listItems, "-- Select Status --", new { @class = "form-control", required = "required" })
        <input type="submit" class="btn btn-primary" value="Save" />

    The web form works great and does exactly what I want. Except now my client wants me to have the page run a SQL stored procedure called "ReserveWAPIaasVMNames" when the submit button is clicked. Is there anyway to do that with this code?


    Gary Winhoven

    Tuesday, June 28, 2016 7:18 PM


All replies

  • User2053451246 posted

    Except now my client wants me to have the page run a SQL stored procedure called "ReserveWAPIaasVMNames" when the submit button is clicked.
    This is done all the time.  In the action that runs when you click the submit button write the standard code that executes a stored procedure:

    using (var connection = new SqlConnection("YOUR_CONNECTION_STRING"))
    	using (var command = new SqlCommand("YOUR_STORED_PROCEDURE_NAME", connection))
    		command.CommandType = CommandType.StoredProcedure;
    		command.Parameters.AddWithValue("@YOUR_PARAMETER_NAME", "YOUR_PARAMETER_VALUE");
    		await connection.OpenAsync();
    		await command.ExecuteNonQueryAsync();

    Tuesday, June 28, 2016 7:57 PM
  • User-2068991853 posted

    Thank you!

    Only two issues,though. Visual Studio returns this error for both 'await' operators: "Error 1 The 'await' operator can only be used within an async method." Is there something I'm missing? And is there any way to make my Parameter Value an entry that is put into the web form? Specifically, the "IaaSVMName".

    Tuesday, June 28, 2016 8:31 PM
  • User2053451246 posted

    If you are not using an async action:

    public async Task<ActionResult> Edit(MyViewModel model)

    remove the two spots where await is typed and the two spots where Async is typed.

    As for getting the parameter value, it's the same way you get any value that was submitted from the form.

    Tuesday, June 28, 2016 8:41 PM
  • User61956409 posted

    Hi Gary Winhoven,

    You could refer to the following links to learn how to execute Stored Procedure using DBContext in Entity Framework.



    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 6, 2016 8:51 AM