0

I want to have a global method that all of my controllers can access to it. in this method I want to connect to Database as I connect in my controllers. following code works fine in my controllers but when i move this code to App_Code it has some Error ! I need this method in all of my controllers so what do I do ? instead of .FirstOrDefault() I have .FirstOrDefaultAsync() ! why ?

using Sitaad.Models;
using System.Web.Mvc;
namespace Sitaad.App_Code
{
    public class Reg
    {
        private string _lang = "Fa";
        private ApplicationDbContext db = new ApplicationDbContext();

        public string T(string txt)
        {
            string query = "select Text from Translations where Code = '@p1' and TranslateId = (select TranslateId from Translations where Text = '@p2')";
            Translations t = db.Translation.SqlQuery(query,
                                           TurboSofts.Lang.T.GetLang(),
                                           txt).FirstOrDefault(); /* this line can't run */
            return t.Text;
                }

            }

        }
SdSaati
  • 798
  • 9
  • 18
  • 1
    What is the error ? Where are you creating the object `db` ? – Shyju Jul 13 '16 at 00:10
  • [Unable to call App_Code class from a code-behind](http://stackoverflow.com/a/14206137/296861) – Win Jul 13 '16 at 00:13
  • the db is : `private ApplicationDbContext db = new ApplicatoinDbContex();` and whole of this method is in a class in App_Code folder – SdSaati Jul 13 '16 at 00:29
  • I Found half of the solution I had to inherit from Controller class `public class Reg {...}` => `public class Reg : Controller {...}` but still I don't have .ToList() in `T` method instead of .ToList() i have a .ToListAsync() why ? – SdSaati Jul 13 '16 at 00:45
  • @Shyju I edited my question – SdSaati Jul 13 '16 at 01:02

0 Answers0