none
LINQ to SQL query not executing RRS feed

  • Question

  • Hello,

    This is my first LINQ to sql, I m tring to load the product name from database to list using VIEW but, foreach loop not executing because the connection state appears as closed. I tried to open the connection manually still the loop terminating without execution. what am i missing out???

    DataClasses1DataContext db = new DataClasses1DataContext();
    
            public void LoadProduct()
            {
                List<String> code = new List<string>();
                //db.Connection.Open();
                var query = from c in db.vw_Products
                            select c.PRODUCT_NAME;
                
                //MessageBox.Show(db.Connection.ConnectionString + db.Connection.State);
                
                foreach (var c in query)
                {
                    code.Add(c);
                }
    
            }

    Any help would be appreciated.

    Regards

    Preetha

    Friday, May 18, 2012 11:05 AM

Answers

  • You need to execute to query, add a ToList() to your query.

    Regards

    Friday, May 18, 2012 2:00 PM
  • Hi Yazhini;

    Linq to SQL works in a disconnected state and so if Linq to SQL is not executing a query it will not have a connection to the database. The following code will do the same thing as the code you posted be less verbose. Does the products table have any information in it?

    public void LoadProduct()
    {
        List<String> code = new List<string>();
    
        query = (from c in db.vw_Products
                 select c.PRODUCT_NAME).ToList();
        
    }

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Saturday, May 19, 2012 12:57 PM
  • DataClasses1DataContext db = new DataClasses1DataContext();
    
            public void LoadProduct()
            {
                List<String> code = (from c in db.vw_Products select c.PRODUCT_NAME).ToList()                        
            }


    Please mark as reply if helped.
    Also visit my blog http://msguy.net/

    Wednesday, May 23, 2012 9:55 AM

All replies

  • You need to execute to query, add a ToList() to your query.

    Regards

    Friday, May 18, 2012 2:00 PM
  • Hi YaZhini,

    Welcome to MSDN Forum.

    The code seems no problem. If possible, could you please upload a demo to SkyDrive and post the link here? Or, you can also email v-alll@microsoft.com

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us


    Saturday, May 19, 2012 7:09 AM
    Moderator
  • Hi Yazhini;

    Linq to SQL works in a disconnected state and so if Linq to SQL is not executing a query it will not have a connection to the database. The following code will do the same thing as the code you posted be less verbose. Does the products table have any information in it?

    public void LoadProduct()
    {
        List<String> code = new List<string>();
    
        query = (from c in db.vw_Products
                 select c.PRODUCT_NAME).ToList();
        
    }

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Saturday, May 19, 2012 12:57 PM
  • Hi Yazhini,

    Have you solved the issue? I look forward to hearing from you.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, May 23, 2012 6:51 AM
    Moderator
  • DataClasses1DataContext db = new DataClasses1DataContext();
    
            public void LoadProduct()
            {
                List<String> code = (from c in db.vw_Products select c.PRODUCT_NAME).ToList()                        
            }


    Please mark as reply if helped.
    Also visit my blog http://msguy.net/

    Wednesday, May 23, 2012 9:55 AM
  • Hi,

      I think you had a wrong connection string can you please cross check it.


    Sai Kumar K (MCP)
    Blog: Sai's Stuff.
    WebSite: SantoshTechnologies.
    MCP ID:Microsoft Transcript.

    Wednesday, May 23, 2012 10:35 AM