DataContext db = new DataContext(@"Data Source=AMAW021\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True");

This document is an extension of doc_mm.Obj1.htm. Where we have defended the following items.(LINK)

  • A data class in App_code
  • A database ObjectData1.mdf

Now add another page and name as suits you the best. I just created a Defaut2.aspx

 

Code that connects to the database, where web config holds the contention string, and ObjClass contains the connect and data set objects.

Web.config

<connectionStrings>
<add name="ObjDataConnection" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ObjectData1.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>

Objclass

 

In the above, a connection string constructed with a name space " System.Web.Configuration;" , then a class SqlDataAdapter  from System.Data.SqlClient namespace, processes an instruction to get the data from a table, and finally a "DataSet class from System.Data, holds the data ( that can be edited, in Contrast to to DataReader)

Default2.aspx contains, one Label, ObjectDataSource and GridView Control. Note, there is other control associated with this page, ObjectDataSource, works as a Proxy to an object type, TypeName defines the class and SelectMethod defines a method in that class that will fetch data in as a DataReader or DataSet. Once data is set in the memory locations, GirdView binds to those data collectively and present those data in a Row format.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>mm.ObjData1: Default2 page</title>
</head>
<body bgcolor="#cccccc">
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:ObjectDataSource ID="ObD1" TypeName="ObjClass1"
SelectMethod="MyData" runat="server" />

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
DataSourceID="ObD1" BackColor="#FFFFCC" Width="534px">
<Columns>
<asp:CommandField ShowSelectButton="True" />
</Columns>
</asp:GridView>

</div>
</form>
</body>
</html>

Run Time