We help IT Professionals succeed at work.

How to connect to LINQ from ASP.NET

313 Views
Last Modified: 2013-11-07
I have added an Linq .dbml to my ASP.NET project. I have added 1 stored procedure.
From a web page I have added
Imports System.Linq
Imports System.Data.Linq.

When I try to declare an instance of the DataContext, it does not show up in intellisense with and without using the Dim db as new  and Dim db.

 I have never worked with LINQ before and need some guidance on getting it up and running. Can someone show me the light?
Comment
Watch Question

Author

Commented:
I have read just about everything out there. I can not find any help on why I can not access my datacontext. I'm trying to find out if I missed any steps, or has anyone had this problem before?
Hi
Why you want to create instance of the datacontext.
You can do everything on the aspx page.
Have a look at the code I have pasted below
I havent added anything in the code behind to display data.
first create a dbml.
AdventureWorks is the name of the dbml file and
so the contexttypename for LinqDataSource will be AdventureWorksDataContext.

This is the link from where i have created application
http://msdn.microsoft.com/en-us/library/bb470363.aspx
<asp:GridView ID="gvProperties" GridLines="None" DataSourceID="LinqDataSource2" 
            runat="Server" CellPadding="4" ForeColor="#333333" Visible="false" >
        <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <RowStyle BackColor="#E3EAEB" />
        <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#7C6F57" />
        <AlternatingRowStyle BackColor="White" />
    </asp:GridView> 

    <asp:LinqDataSource 
      ContextTypeName="AdventureWorksDataContext" 
      TableName="ProductSubcategories" 
      Where="ProductCategoryID = @SelectedCategory" 
      Select="new(ProductSubCategoryID, ProductCategoryID,Name, ModifiedDate, rowguid)" 
      ID="LinqDataSource2" 
      runat="server">
      <WhereParameters>
        <asp:ControlParameter 
          Name="SelectedCategory" 
          ControlID="GridView1" 
          DefaultValue="0" 
          PropertyName="SelectedValue" 
          Type="Int32" />
      </WhereParameters>
    </asp:LinqDataSource>

Open in new window

Author

Commented:
this was helpful, but I'm not there yet.

I am only able to declare the datacontext when I have tables in my .dbml. If I create my own class that who's properties reflect the recordset returning, the datacontext will not be available. Maybe if I tell you what I am trying to do it will be more helpful. I have a stored procedure that returns data from 3 tables (tblWorkSheet, tblRows, tblColumns). The goal is for the user to build a custom report by selecting from listbox controls any field/row from any worksheet. The listbox control data should contain rows/columns for the worksheet selected. I think it would be best to databind my controls to avoid having to requery the db each time the user select a different worksheet from the listbox.  I could create a datatable and load it with the query results and store it in a session variable so that I can requery each time the user selects a different worksheet, but my gut is telling me linq can help me.
Any advice?
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.