Adding Linq-to-Sql class breaks simple ASP.NET web

2019-08-29 06:22发布

问题:

Using VS2012, latest SP/update applied.

I have been very frustrated trying to get an ObjectDataSource to work. See http://bit.ly/XTpdvN and http://bit.ly/XTpsHi.

I started a new Web Application project, compiling and running after each step, trying to make the steps as granular as possible.

1) Create new empty web application.
2) Add WebForm1.aspx.
3) Clean, rebuild, run (either View in Browser or in debugger).
4) Add new class to App_Code.
5) Repeat #3.
5) Add existing .mdf to App_Data.
6) Repeat #3.
7) Add Linq-to-Sql class to App_Code (Items.dbml). Leave it empty.
8) Clean, Rebuild, run in Debugger:

Compiler Error Message: CS0234: The type or namespace name 'Linq' does not exist in the namespace 'System.Data' (are you missing an assembly reference?)
Source Error:

Line 12: namespace ODS_Restart.App_Code Line 13: { Line 14: using System.Data.Linq; Line 15: using System.Data.Linq.Mapping; Line 16: using System.Data;

As far as I can tell, there is nothing in the code in App_Code that needs Linq:

namespace ODS_Restart.App_Code  
{   
    public class BAL  
    {  
        public static List<string> GetCountries()  
        {  
            return new List<string>() { "USA", "Aus", "NZ"};  
        }  
    }  
}  

As I said, very frustrating. Any insight on how to get past this problem would be greatly apprectiated....

回答1:

use one wizard for linq to sql program:

step 1. Take new project as windows form application . step 2. Drag one button and DataGridView from toolbox on it. step 2. Go to solution explorer. add database(.mdf) file to windows form application project which we've taken. step 3. Same way by right clicking on windows form application project which we've taken add linq to sql class(.dbml) file. step 4. Go to Server explorer add table in database taken in step 3. Add data to that table. step 5. open that .dbml file from solution explorer and drag table created from server explorer on it. step 6. Open form created in step 2. Double click on that button and add following code:

DataClasses1DataContext obj1 = new DataClasses1DataContext();
        List<pp> obj2 = obj.pps.ToList();
        dataGridView1.DataSource = obj1;

DataClasses1 is .dbml file added in step 3. pp is table name added in step 4. obj1 and obj2 are objects created. pps represents tablename(pp).