1

Im trying to read records from a database using Entity Framework , which I have not used before.

Im trying to run the load rule method:

class Rule
    {
        private STRATCODE stratCode;
        private STRATRULEDEF stratRuleDef;
        AREEntities AREDb = new AREEntities();

        public Rule(STRATRULEDEF StratRuleDef, STRATCODE StratCode)
        {
            this.stratRuleDef = StratRuleDef;
            this.stratCode = StratCode;    
        }

        public void LoadRule()
        {
            var query = from stratCode in AREDb.STRATCODES
                        where stratCode.CODEVALUE == "49300"
                        select stratCode;

            //StratCodeRepository StratCodeRepository = new StratCodeRepository(new AREEntities());
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\user\Desktop\NewText.txt",true))
            {
                foreach (STRATCODE row in query)
                {
                    file.WriteLine("{0} | {1} | {2} | {3} | {4} | {5}", stratCode.STRATRULEKEY, stratCode.CODETYPE, stratCode.CODEVALUE, stratCode.SYSTEMID, stratCode.CODESETKEY, stratCode.PAYMENTYEAR);
                }
            }
        }

My problem is that everytime I run it, it returns the right number of records but all of the data in the text file is empty and I can find no clues in my output log.

0 |  |  |  |  | 
0 |  |  |  |  | 
0 |  |  |  |  | 
0 |  |  |  |  | 
0 |  |  |  |  | 

Any ideas? I dont know how to even start with no clues

1 Answer 1

4

Your write is not using the foreach variable:

    using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\stephen.carmody\Desktop\NewText.txt", true))
    {
        foreach (var row in query)
        {
            file.WriteLine("{0} | {1} | {2} | {3} | {4} | {5}",
                row.STRATRULEKEY,
                row.CODETYPE, 
                row.CODEVALUE, 
                row.SYSTEMID, 
                row.CODESETKEY, 
                row.PAYMENTYEAR);
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Haha Thanks for the help, so easy yet I never would have seen it

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.