0

I have created a stored procedure to get corresponding results from sql server db which contains only the chosen date from the datepicker directive in my Angular App. I don't know how to access from WebAPI to the stored procedure in my SQL Server DB. In the following code I show you my approach:

Created stored Procedure in SQL Server:

USE [SAMPLE_DB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE getDatum
    @Dt datetime
AS
BEGIN
    SET NOCOUNT ON;

    SELECT b.Id, b.Datum, b.num_aucts
    FROM dbo.Beispiels AS b
    WHERE b.Datum = @Dt
END
GO

Ctrl in Angular:

//Resource from WebAPI
var Resource = $resource(baseUrl + '/api/beispiels/:Id', {
    Id: '@Id'
}, {});

$scope.list = Resource.query();

//definitions of Datepicker (UIbootstrap)
$scope.open = function ($event) {
   $event.preventDefault();
   $event.stopPropagation();

   $scope.opened = true;
};

$scope.dateOptions = {
   formatYear: 'yy',
   startingDay: 1
};

View in my Angular App:

<div class="row">
    <div class="col-md-6">
        <p class="input-group">
            <input type="text" 
                   class="form-control" 
                   datepicker-popup 
                   ng-model="dt" 
                   is-open="opened" 
                   min-date="" 
                   max-date="" 
                   datepicker-options="dateOptions"
                   ng-required="true" 
                   close-text="Close" 
                   />
            <span class="input-group-btn">
                <button type="button" class="btn btn-default" ng-click="open($event)">
                   <i class="glyphicon glyphicon-calendar"></i>
                </button>
            </span>
        </p>
    </div>
</div>
<div class="row">
    <div class="col-md-6">
        <table class="table table-bordered">
            <th>Id</th>
            <th>Datum</th>
            <th>Num Aucts</th>
            <tr ng-repeat="liste in list">
                <td>{{ liste.Id }}</td>
                <td>{{ liste.Datum }}</td>
                <td>{{ liste.num_aucts }}</td>
            </tr>
        </table>
    </div>
</div>

WebAPI Controller:

...
public class BeispielsController : ApiController
{
   private EasContext db = new EasContext();

   // GET: api/Beispiels
   [HttpGet]
   public IQueryable<Beispiel> GetBeispiels()
   {
      return db.Beispiels.AsQueryable();
   }
...
}

Db Context:

public partial class EasContext : DbContext
{
    public EasContext()
      : base("name=EasContext")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
       throw new UnintentionalCodeFirstException();
    }

    public virtual ObjectResult<getDatum> getDatum(Nullable<System.DateTime> dt)
     {
         var dtParameter = dt.HasValue ?
         new ObjectParameter("Dt", dt) :
         new ObjectParameter("Dt", typeof(System.DateTime));

         return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<getDatum>("getDatum", dtParameter);
     }
...

I want to synchronize the ngModel value dt with the value from the stored procedure @Dt, that means, when the user click on the datepicker and chooses a date. the date will displayed in the ngModel input field and then it should be insert to the stored procedure and returns the result only for example all data which date have "2013/01/03" or something else.

yuro
  • 2,189
  • 6
  • 40
  • 76
  • The following post may be helpful: http://stackoverflow.com/questions/20970416/using-stored-procedure-in-entity-framework – David Tansey Jul 14 '15 at 19:09
  • I would also consider adding the tag `entity-framework` to your question. – David Tansey Jul 14 '15 at 19:10
  • @DavidTansey I don't know how to integrate in my Angular app?! – yuro Jul 14 '15 at 20:30
  • 1
    I do not think the problems you are running into have anything to do with Angular integration. You should first try to make sure that you can execute the Action in your WebApi using a direct URL in the browser -- this will demonstrate that you have correctly defined the Stored Proc in EntityFramework and that your WebApi can call it and return results. Once that is resolved, then move forward with your Angular integration (which by that time should no problem at all). – David Tansey Jul 14 '15 at 21:13

0 Answers0