1/15/2013

Load c# DataGrid Using LINQ

So tonight I decided to create a project in Visual Studio c# and connect to a SQL-Server local instance using LINQ.

First I created the project.  Then created a data source by adding a new class using ASP LINQ.

After entering the server name and database name using the wizard I selected a table.



Then connected the Data Grid to use the new data source, then modified the Default.aspx.cs file as follows:


using System;
using System.ComponentModel.DataAnnotations;
using System.Web.DynamicData;

namespace JonsASPDataLinqSQLWeb
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
            System.Collections.IList visibleTables = Global.DefaultModel.VisibleTables;
            if (visibleTables.Count == 0)
            {
                throw new InvalidOperationException("There are no accessible tables. Make sure that at least one data model is registered in Global.asax and scaffolding is enabled or implement custom pages.");
            }
            Menu1.DataSource = visibleTables;
            Menu1.DataBind();
            Menu1.Enabled = true;
            Form.Visible = true;
        }

    }
}

So the project contains the following files:


Press F6 to compile and F5 to run and presto, we have data on the ASPX form:


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Thoughts to Ponder