none
LINQ to SQL in VC# 2008 Express: 3 Errors-DataClass1DataContext does not contain a contructor, the type or namespace could not be found, the type of expression in the select clause is incorrect RRS feed

  • Question

  • Hi all,

    In my Visual C# 2008 Express, I created a Windows Forms application "salysle1LINQtoSQL". I added a DataGridView, a LINQ to SQL Classes (for DataClasses), and a Data Connection (Northwind.sdf in Database Explorer) in my application.  I have the following code:

    using System;
    
    using System.Collections.Generic;
    
    using System.ComponentModel;
    
    using System.Data;
    
    using System.Data.SqlClient;
    
    using System.Drawing;
    
    using System.Linq;
    
    using System.Text;
    
    using System.Windows.Forms;
    
    
    
    namespace salysle1LINQtoSQL
    
    {
    
        public partial class Form1 : Form
    
        {
    
            public Form1()
    
            {
    
                InitializeComponent();
    
            }
    
    
    
    
    
    
    
            private void Form1_Load(object sender, EventArgs e)
    
            {
    
                SqlConnection conn;
    
                SqlCommand comm;
    
                SqlDataAdapter adapter;
    
                DataSet ds = new DataSet();
    
    
    
                // --Connect to the Northwind database in SQL Server 2008 Express
    
                conn = new SqlConnection(@"Data Source = NAB-1234567\SQLSCOTT;" +
    
                        "Initial Catalog=Northwind; Integrated Security=True");
    
                comm = new SqlCommand("SELECT productname, unitprice FROM products WHERE unitprice <20", conn);
    
                adapter = new SqlDataAdapter(comm);
    
                adapter.Fill(ds);
    
    
    
            }
    
    
    
        public void SimpleQuery()
    
         {
    
           NorthwindDataClasses1DataContext dc = new NorthwindDataClasses1DataContext();
    
    
    
           var q =
    
            from a in dc.GetTable<Order>()
    
            select a;
    
    
    
           dataGridView1.DataSource = q;
    
          }
    
       }
    
     }
    
    
    
    
    
    
     
    I got the following 3 errors:

    Error 1 'salysle1LINQtoSQL.NorthwindDataClasses1DataContext' does not contain a constructor that takes '0' arguments C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\salysle1LINQtoSQL\salysle1LINQtoSQL\Form1.cs 40 46 salysle1LINQtoSQL
    Error 2 The type or namespace name 'Order' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\salysle1LINQtoSQL\salysle1LINQtoSQL\Form1.cs 43 31 salysle1LINQtoSQL
    Error 3 The type of the expression in the select clause is incorrect.  Type inference failed in the call to 'Select'. C:\Documents and Settings\e1enxshc\My Documents\Visual Studio 2008\Projects\salysle1LINQtoSQL\salysle1LINQtoSQL\Form1.cs 44 9 salysle1LINQtoSQL

    Please help and tell me how to correct these problems.

    Thanks,
    Scott Chang
    Wednesday, August 19, 2009 7:48 PM

Answers

  • It looks like you need to add a Using System.Data.Linq; in your declarations. Also you may be running into an issue trying to use LINQ to SQL against a SQL Compact (.sdf) database. The LINQ to SQL designer does not work against SQL Compact, only SQL Server and SQL Express. As a result, you may not have the default connection string set on the Context creating your first error. You can try to pass the connection string into the context's constructor.

    Jim Wooley
    www.ThinqLinq.com
    http://www.LinqInAction.net - "LINQ In Action", The book is now available. Don't wait for the movie
    • Marked as answer by Yichun_Feng Wednesday, August 26, 2009 1:37 AM
    Thursday, August 20, 2009 8:50 PM
    Moderator