0

Question title could be confusing because i didn't find appropriate sentence to specify my problem. I have an entity which contains 60 properties [Columns]. Now i want to insert into this entity. Normal insert code would be:

tblEmployee objEmployee= new tblEmployee();

tblEmplyee.Name="abc"; // Col 1

tblEmplyee.Email="[email protected]"; // Col 2

tblEmplyee.S1="s1"; // Col 3
tblEmplyee.S2="s2"; // Col 4
.
.
.
tblEmplyee.S60="s60"; // Col 60
db.AddTotblEmployee(objEmployee);
db.SaveChanges(); 

But i have a condition, i need to insert values in S1 to S60 variably. e.g case 1: from S1 to S17 value 1, S18 to S50 value 2 and S51 to S60 value 3. case 2: from S1 to S11 value 1, S11 to S40 value 2, S40 to S43 value 3 and S44 to S60 value 4.

And this pattern can be different every time and depend on user input by UI.

Is there any way to access tblEmployee's column like array indexes ?

    tblEmployee objEmployee= new tblEmployee();

    tblEmplyee.col["1"]="abc"; // Col 1

    tblEmplyee.col["2"]="[email protected]"; // Col 2

    tblEmplyee.col["3"]="s1"; // Col 3
    .
    .
    .

or any other way?

1

1 Answer 1

0

Hi here is the code you can use

System.Reflection.PropertyInfo[] properties = typeof(tblEmployee).GetProperties();
        tblEmployee objEmployee = new tblEmployee();
        for(int i=0;i<properties.Length;i++)
        {
            properties[i].SetValue(objEmployee, 1);//First param is obj and 2nd is value for the column which will be saved in db.
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.