How to get label text from table reference on database based on TableName and FieldName

SummaryProblem

How to get label text from table reference on database based on TableName and FieldName dynamically from database and show on view createEmployee .

Details Problem

Meaning i need to get label text dynamically from database not static from model

SO that Every Time i need to change text of label i will change it from database depend on reference table

AND No need to change from code .

===============Goal From I post

I need function or any thing general let me get text of label from database because Title of labels on my suituation changed more)

===============Tools Used

Tools used sql server 2012 and visual studio 2017 asp.net core 2.1

Database Have two tables Employee and Reference File(may be increase models and views so that if any thing general is prefer)
====Code==========

Query Get Data from reference Table

SELECT TableName, FieldName,EnglishtextforLabel 
FROM ReferenceFile 
WHERE (FieldName = 'EmployeeId' ) 
AND TableName = 'Employee'

Models classes Include HRContext

 public class ReferenceFile(have 3 key as composit keys(Code,TableName,FieldName))
    {
        public int Code { get; set; }
        public string TableName { get; set; }
        public string FieldName { get; set; }
        public string EnglishtextforLabel{ get; set; }

    }
 public class Employee
    {
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public int EmployeeAge { get; set; }
    }
 public class HRContext : DbContext
    {
        public HRContext(DbContextOptions<HRContext> options)
: base(options)
        { }

        public DbSet<Employee> Employees { get; set; }
        public DbSet<ReferenceFile> ReferenceFiles { get; set; }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<Employee>()
               .HasKey(t => new { t.EmployeeId });

            modelBuilder.Entity<ReferenceFile>()
                .HasKey(t => new { t.Code,t.TableName,t.FieldName });
        }
    }


Employee View Create(this static and i dont need . i need view labels get from reference table)

<div class="row">
    <div class="col-md-4">
        <form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="EmployeeName" class="control-label"></label>
                <input asp-for="EmployeeName" class="form-control" />
                <span asp-validation-for="EmployeeName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="EmployeeAge" class="control-label"></label>
                <input asp-for="EmployeeAge" class="form-control" />
                <span asp-validation-for="EmployeeAge" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </form>
    </div>
</div>

==============
Here i need to get label on view create from table reference not static field name
=======Sample data========
Sample data for ReferenceTable

Code  TableName  FieldName    EnglishtextforLabel 
1      Employee  EmployeeId          Code
2      Employee  EmployeeName        Name        
3      Employee  EmployeeAge         Age

================
============Result I need==========

labels on view create must be Give me the Result below

 :
Code

Name


Age

================

can any one help me for answer question above

Please understand that you are using Entity Framework. The DbContext Class (your HRContext class derives from DbContext) is part of the Entity Framework. Are you using Database First or Code First? If you don’t understand that question then you should ask someone in your organization or class to help.

Do you still need help or have you gotten help elsewhere?

There is very much to learn about ASP.Net and Entity Framework and many other things. I am learning and there is much I need to learn too.

I do not understand that. Entity Framework has models and they can be used to get data dynamically from the database.

I also do not totally understand what you need to do. However I hope the following is close enough.

public partial class YourPageClass : System.Web.UI.Page
{
	HRContext db = null;

	protected void Page_Load(object sender, EventArgs e)
	{
		if (IsPostBack)
			return;
		db = new HRContext();
		var results = from res in db.ReferenceFiles
						where res.FieldName == "EmployeeId" && res.TableName = "Employee"
						select res;
		ReferenceFile ref = results.FirstOrDefault().;
	}
}

Note that with Entity Framework we usually use LINQ instead of SQL.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.