You must Sign In to post a response.
  • Category: ASP.Net MVC

    How to call a storedprocedure in mvc using entity framework and triggermail

    HI ALL I AM NEW TO MVC SO PLZ HELP ME
    WHT I WNT IS BELOW IS MY CODE TO CALL A STOREDPROCEDURE AND WRITE LINQ LAMDA EXPRESSION QUERY AND MAIL IT SO BELOW IS MY CODE HOW TO WRITE LINQ LAMDAEXPRESSION QUER AND EXECUTE IT I HAVE STAARTED CODE BUT INCOMPLETE ANY BODY CAN COMPLETE THIS

    public void triggerResponseMail()
    {
    log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    logger.Info("Begin of job - triggerResponseMail");
    eProEntities db = new eProEntities();
    try
    {
    ObjectParameter result = new ObjectParameter("outflag", typeof(Int32));
    var outputSP = db.process_porespone();
    outputSP = db.process_porespone();
    if(outputSP >= 1)
    { ""var RES ="select distinct ISNULL(po.ODA063,'kokhoong.wong@sg.panasonic.com'),trk.SellerCode,trk.OrderNum,trk.OrderRspNum from PAN_ORDRSP_TRK trk, PO_AMS po where RspUploadId=775 and po.OrderId=trk.OrderId";"" THIS SHOULD BE WRITTEN IN LAMDA EXPRESSION


    }
    }
    catch (Exception ex)
    {
    logger.Error("Error in job", ex);
    }
    db.Dispose();
    logger.Info("End of job - triggerResponseMail");
    }

    IN GLOBAL.ASPX IN APPLICATION START
    protected void Application_Start()
    {
    JobManager.AddJob(new Common.POAperakFailureAlerter().triggerResponseMail, s => s.ToRunEvery(interval).Minutes());
    accessCofig = (UserRolesSection)ConfigurationManager.GetSection("appRoles");
    accessLevelConfig = accessCofig.UserRolesSections;


    }
  • #768719

    You can use entity framework and You can call a stored procedure in your DbContext class as follows

    this.Database.SqlQuery<YourEntityType>("storedProcedureName",params);

    in simple words
    All you have to do is create an object that has the same property names as the results returned by the stored procedure. For the following stored procedure:

    CREATE PROCEDURE [dbo].[GetResultsForCampaign]
    @ClientId int
    AS
    BEGIN
    SET NOCOUNT ON;

    SELECT AgeGroup, Gender, Payout
    FROM IntegrationResult
    WHERE ClientId = @ClientId
    END

    create a class look like

    create a class that looks like:
    public class ResultForCampaign
    {
    public string AgeGroup { get; set; }

    public string Gender { get; set; }

    public decimal Payout { get; set; }
    }

    then call procedure by

    using(var context = new DatabaseContext())
    {
    var clientIdParameter = new SqlParameter("@ClientId", 4);

    var result = context.Database
    .SqlQuery<ResultForCampaign>("GetResultsForCampaign @ClientId", clientIdParameter)
    .ToList();
    }


    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


Sign In to post your comments