0

Good Day! I am a newbie on ASP.NET (MVC) and trying to display a value (from a Stored Procedure) after the value of the date has been changed. This is what it looks like on the design Once the date is picked, the user will click the Create button and the value should be displayed on the "Sales" textbox

This is my Stored Procedure for it

CREATE PROCEDURE [dbo].[SP_DAILY_SALES] 
 -- Add the parameters for the stored procedure here
@Order_date date
 
AS
BEGIN TRANSACTION;
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
BEGIN TRY
 SET NOCOUNT OFF;



 DECLARE @SumTable table
 (
  dailySales decimal (18,2) 
 )


 DECLARE @dailySales decimal(18,2)
 SET @dailySales = (
     SELECT SUM(NET_AMOUNT)
     FROM [ORDER]
     WHERE ORDER_DATE=@Order_date)

INSERT INTO @SumTable (dailySales)
  VALUES (@dailySales)



    -- Insert statements for procedure here
  SELECT dailySales
  FROM @SumTable




 COMMIT TRANSACTION
END TRY

BEGIN CATCH
 ROLLBACK TRANSACTION;
END CATCH;

and this is where I call it (Repository)

   public bool DisplayDailySales(DateTime orderdate)
        {

            connection();
            SqlCommand com = new SqlCommand("SP_DAILY_SALES", con);

            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.Add("@Order_date", SqlDbType.DateTime).Value = orderdate;
            con.Open();
            int i = com.ExecuteNonQuery();
            con.Close();
            if (i >= 1)
            {
                return true;
            }
            else
            {

                return false;
            }


        }

My Controller

[HttpGet]
        public ActionResult Statistics()
        {

            OrderVM dSales = new OrderVM();
            var currentdate = DateTime.Now;
            dSales.ORDER_DATE = currentdate;

            return View();


        }


        [HttpPost]
        public ActionResult Statistics(OrderVM order)
        {
            try
            {
                DateTime orderdate = order.ORDER_DATE;
                ViewData["ORDERDATE"] = orderdate;



                if (ModelState.IsValid)
                {
                    OrderRepository orderRepo = new OrderRepository();
                    orderRepo.DisplayDailySales(orderdate);



                }
                return View();

            }
            catch
            {

                return View();
            }




        }

And lastly, in my view as of now is this

                <div class="panel-body">
            @using (Html.BeginForm())
                    {
                @Html.AntiForgeryToken()
                @Html.ValidationSummary(true)


    <div class="form-horizontal">

        <div class="form-group">
            @Html.LabelFor(model => model.ORDER_DATE, "Date", htmlAttributes: new { @class = "col-xs-4 col-sm-offset-1 col-sm-4" })
            <div class="col-lg-6 col-lg-6 col-md-6 col-md-6 col-sm-6 col-sm-6 col-xs-6 col-xs-6">
                @Html.TextBoxFor(model => model.ORDER_DATE, "{0:yyyy-MM-dd}", new { @class = "form-control", @type = "date", @id = "Orderdate" })
                @Html.ValidationMessageFor(model => model.ORDER_DATE, "", new { @class = "text-danger" })
            </div>
        </div>


        <div class="form-group">
            @Html.LabelFor(model => model.dailySales, "Sales", htmlAttributes: new { @class = "col-xs-4 col-sm-offset-1 col-sm-4" })
            <div class="col-lg-6 col-lg-6 col-md-6 col-md-6 col-sm-6 col-sm-6 col-xs-6 col-xs-6">
                @Html.TextBoxFor(model => model.dailySales, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.dailySales, "", new { @class = "text-danger" })
            </div>
        </div>


        <div class="form-group">
            <div class="col-lg-3 pull-right">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </div>


    </div>

           
                    }
           
        </div>

Hope someone can help me out on this. Thank You!

Kylie Irwin
  • 195
  • 3
  • 11
  • So where's your question? – James Z Jul 14 '16 at 07:11
  • @JamesZ Oh, I'm sorry. I was just wondering what's missing (or wrong) with my codes because when the Create button is clicked, it does not display anything. – Kylie Irwin Jul 14 '16 at 07:19
  • If you don't know about use SP in EntityFramework look at this [enter link description here](http://stackoverflow.com/questions/20901419/how-to-call-stored-procedure-in-entity-framework-6-code-first) – Zebra Jul 14 '16 at 07:19
  • you can do exec [dbo].[SP_DAILY_SALES] ' date you are selecting ' and check if query returns data – coder771 Jul 14 '16 at 11:22
  • Hi there @coder771 ! The SP does return correct data. – Kylie Irwin Jul 14 '16 at 12:07
  • is it returning anything at all? – coder771 Jul 14 '16 at 12:11
  • Hi @coder771 when I execute my SP on the Sql server, it returns the correct data. It just doesn't execute properly on my code. I think I'm missing something I just couldn't quite pin point it. – Kylie Irwin Jul 15 '16 at 02:27
  • are you filling model with the sales when returning view on controller. i dont think so. – coder771 Jul 15 '16 at 12:00

0 Answers0