We need this database http://manas6/aspnet.35/mm.GridView1/

 

Creating Master Key

Shows Primary Key

You may also view any existing key using Relations key

Setting Froign Key to details tables.

At this point you need to refer a column common to each other

While coding and writing, I got distracted and missed screen shots, in correct order. The connection string I used, was mainconnection, registered in Web.config file

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

Step : 1

Using WHERE

We.config

<connectionStrings>
<add name="masterConnectionString" connectionString="Data Source=MANAS6\SQLEXPRESS;Initial Catalog=master;Integrated Security=True" providerName="System.Data.SqlClient"/>
<add name="mainconnection" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Club.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

 

Code Default.aspx

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

<!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>mm.GridView and Master Details </title>
<style type="text/css"">
#div1
{
width: 259px; table-layout: auto; background-color: #FFFFCC;position:absolute;top:150px;
}
#div2
{
width: 300px; table-layout: auto; background-color: #FFFFCC;position:absolute; top:21px;
left:300px;height:20px;
}
#div3
{
width: 298px; height:150px;
table-layout: auto; background-color: #FFFFCC;position:absolute; top:50px;
left:301px;
}
#div4
{
width: 268px; table-layout: auto; background-color: #FFFFCC;position:absolute; top:98px;
left:11px;height: 30px;
}
#div5
{
width: 280px;
table-layout: auto; background-color: #FFFFCC;position:absolute; top:204px;
left:10px;
}
#div6
{
width: 275px; left:10px; top:150px;
}
#div7
{
width: 405px; height:160px;
table-layout: auto; background-color: #FFFFCC;position:absolute; top:210px;left:301px;
}
#div8
{
width: 259px; table-layout: auto; background-color: #FFFFCC;position:absolute;top:10px;
}
#form1
{
height: 271px;
}
</style>
</head>
<body bgcolor="#CCCCCC">
<form id="form1" runat="server">
<div id="div6">
<asp:SqlDataSource ID="SqlDataSource1" runat="server" DataSourceMode="DataReader"
ConnectionString="<%$ ConnectionStrings:mainconnection %>"
SelectCommand="SELECT [FirstName] FROM [Main]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:mainconnection %>"
SelectCommand="SELECT * FROM [Main]"></asp:SqlDataSource>

</div>
<div id="div1">

<asp:Label ID="L1" runat="server" Text="Label"></asp:Label>

</div>
<div id="div2">

<asp:Label ID="L2" runat="server" Text="Label"></asp:Label></div>
<div id="div3" >

<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataSourceID="SqlDataSource3" Width="294px"
BorderColor="#333399" BorderStyle="Solid" BorderWidth="4px">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID"
SortExpression="ID" ReadOnly="True" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />
</Columns>
</asp:GridView>

</div>
<div id="div4">

<asp:DropDownList ID="DropDownList1" runat="server" Height="30px" Width="257px"
DataSourceID="SqlDataSource1" AutoPostBack="True" DataTextField="FirstName"
DataValueField="FirstName" OnSelectedIndexChanged="DL1_Selection">
</asp:DropDownList>

</div>
<div id="div5" >

<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="278px"
AutoGenerateRows="False" DataKeyNames="ID" DataSourceID="SqlDataSource2"
BorderColor="#006666" BorderStyle="Groove" BorderWidth="4px"
Caption="Detalis View" >
      <Fields>
          <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
        <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
        <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
       <asp:BoundField DataField="status" HeaderText="status" SortExpression="status" />
       <asp:BoundField DataField="position" HeaderText="position" SortExpression="position" />
     <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
     <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
    </Fields>
</asp:DetailsView>

<asp:SqlDataSource ID="SqlDataSource2" runat="server" DataSourceMode="DataReader"
ConnectionString="<%$ ConnectionStrings:mainconnection %>"
SelectCommand="SELECT * FROM [details] WHERE ([FirstName] = @FirstName)">
<SelectParameters>
<asp:ControlParameter Name="FirstName" Type="String" ControlID="DropDownList1" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>

</div>
<div id="div7"> Detail Static GridView>

</div>
<div id="div7"> Detail Static GridView
<asp:GridView ID="GridView2" runat="server"
AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource4"
Width="391px" BorderColor="#999966" BorderStyle="Ridge" BorderWidth="4px">
  <Columns>
          <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" />
         <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
         <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
        <asp:BoundField DataField="status" HeaderText="status" SortExpression="status" />
       <asp:BoundField DataField="position" HeaderText="position" SortExpression="position" />
       <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
       <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
   </Columns>
</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource4" runat="server" DataSourceMode="DataReader"
ConnectionString="<%$ ConnectionStrings:mainconnection %>"
SelectCommand="SELECT * FROM [details]"></asp:SqlDataSource>
</div>
<div id="div8"> Master Details with <br />DropDown and Details</div>
</form>
</body>
</html>
 

Code In Default.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
L1.Text = "!IsPostBack Page : Welcome";
}
if (IsPostBack)
{
L1.Text = "IsPostBack Page : Stay Tuned.";
}
}
protected void DL1_Selection(object sender, EventArgs e)
{
string cmdText = "Select * from PlayersInfo where FirstName = " + "'" + DropDownList1.SelectedItem.Text + "'";
L2.Text = "You Selected " + DropDownList1.SelectedItem.Text;

try
{
}
catch
{
L2.Text += "There was an error ";
}


finally
{
//sql_cnn.Close();

}

}
}
 

Runtime Analysis

Opening for the First time

Now select a name from the dropdown list

Now "IsPaostBack"