简体   繁体   中英

Using connection string in ASP.Net MVC controller to retrieve records

I am trying to retrieve records in my index ActionResult but keep getting a "The name 'connectionString' does not exist in the current context" error. Please see below code:

using Microsoft.Xrm.Tooling.Connector;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using TestMVCApp.DAL;

using TestMVCApp.Models;

namespace TestMVCApp.Controllers
{

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var objDAL = new DAL_InvoicesEntity();
            List<InvoicesModel> invInfo = objDAL.RetriveRecords(connectionString);

            ViewBag.invInfo = invInfo;
            return View();
        } 
    } 
}

DAL class file:

using System;

using System.Collections.Generic;

using Microsoft.Xrm.Sdk;

using TestMVCApp.Models;

using Microsoft.Xrm.Sdk.Query;

using Microsoft.Xrm.Tooling.Connector;

using System.Linq;


namespace TestMVCApp.DAL
{

    public class DAL_InvoicesEntity
    {
        public List<InvoicesModel> RetriveRecords(string connectionString)
        {
            var svc = new CrmServiceClient(connectionString);

            var query = new QueryExpression()
            {
                EntityName = "new_invoices",
                ColumnSet = new ColumnSet("new_invoicesid", "ttt_customer", "ttt_invoiceid", "ttt_paymentreceived", "ttt_commission", "ttt_adminfee", "ttt_discountamount"),
                TopCount = 10
            };

            var invoices = svc.RetrieveMultiple(query).Entities.ToList();

            var invoiceModels = invoices.Select(i =>
                new InvoicesModel
                {
                    InvoiceID = i.GetAttributeValue<Guid>("new_invoicesid"),
                    ClientName = i.GetAttributeValue<EntityReference>("ttt_customer"),
                    InvoiceNumber = i.GetAttributeValue<string>("ttt_invoiceid"),
                    AdminFee = i.GetAttributeValue<decimal>("ttt_adminfee"),
                    Discount = i.GetAttributeValue<decimal>("ttt_discountamount"),
                    PaymentReceived = i.GetAttributeValue<decimal>("ttt_paymentreceived")
                })
                .ToList();

            return invoiceModels;
        }
    }
}

Please assist if you can

You should define connectionString variable before using it.

public class HomeController : Controller
{
        private const connectionString = "YourConnectionString";
        public ActionResult Index()
        {

            var objDAL = new DAL_InvoicesEntity();
            List<InvoicesModel> invInfo = objDAL.RetriveRecords(connectionString);

            ViewBag.invInfo = invInfo;
            return View();
        } 
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM