Let spell out the scenario:
2 tables:
table: membermember, at least 2 fields: memberid, membermomentno, ...
table: moment, at least 2 fields: momentno, momentname, ...
membermomentno field is linked to momentno
Say you want to draw data from this 2 tables and populate a datagrid. Adapt the following code to suit your needs:
on the aspx page:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default8.aspx.cs" Inherits="Default8" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<ASP:DataGrid id="MyDataGrid" runat="server"
Width="800"
BackColor="#cccccc"
BorderColor="black"
ShowFooter="false"
CellPadding="5"
CellSpacing="5"
Font-Name="Verdana"
Font-Size="12pt"
HeaderStyle-BackColor="#abcdef"
EnableViewState="false"
/>
</div>
</form>
</body>
</html>
on the code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class Default8 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection myConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename='C:\Users\Peter Leow\Documents\Visual Studio 2010\Projects\SampleDatabaseWalkthrough\SampleDatabaseWalkthrough\SampleDatabase.mdf';Integrated Security=True;Connect Timeout=30;User Instance=True");
SqlDataAdapter myCommand = new SqlDataAdapter("SELECT memberid, momentname FROM membermoment JOIN moment ON membermomentno = momentno", myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds);
DataView source = new DataView(ds.Tables[0]);
MyDataGrid.DataSource = source;
MyDataGrid.DataBind();
}
}