The program can be used directly to run through.
----------------------------------------------------
Testconstraint.aspx
----------------------------------------------------
<%@ Page language= "C #" debug= "true"%>
<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.SqlClient"%>
<script language= "C #" runat= "Server" >
Public DataSet DS;
void Page_Load (Object Sender,eventargs E)
{
if (! IsPostBack)
{
SqlConnection myconnection = new SqlConnection ("server= (local);d atabase=northwind; Trusted_connection=yes ");
SqlDataAdapter Mydataadapter1=new SqlDataAdapter ("select * from Suppliers", MyConnection);
SqlDataAdapter Mydataadapter2=new SqlDataAdapter ("SELECT * FROM Products", myconnection);
Ds=new DataSet ();
Mydataadapter1.fill (ds, "Suppliers");
Mydataadapter2.fill (ds, "products");
Mydatagrid.datasource=ds. tables["Suppliers"]. DefaultView;
Mydatagrid.databind ();
Mydatagrid2.datasource=ds. Tables["Products"]. DefaultView;
Mydatagrid2.databind ();
}
}
void Mydatagrid_delete (Object Sender,datagridcommandeventargs e)
{
String conn= "server= (local);d atabase=northwind; Trusted_connection=yes ";
String selectcommandtext1= "SELECT * from Suppliers";
String selectcommandtext2= "SELECT * FROM Products";
SqlDataAdapter Mydataadapter1=new SqlDataAdapter ();
Mydataadapter1.selectcommand=new SqlCommand ();
MYDATAADAPTER1.SELECTCOMMAND.COMMANDTEXT=SELECTCOMMANDTEXT1;
Mydataadapter1.selectcommand.connection=new SqlConnection (conn);
SqlDataAdapter Mydataadapter2=new SqlDataAdapter ();
Mydataadapter2.selectcommand=new SqlCommand ();
MYDATAADAPTER2.SELECTCOMMAND.COMMANDTEXT=SELECTCOMMANDTEXT2;
Mydataadapter2.selectcommand.connection=new SqlConnection (conn);
Ds=new DataSet ();
Mydataadapter1.fill (ds, "Suppliers");
Mydataadapter2.fill (ds, "products");
Createconstraint ();
int index= (int) E.item.itemindex;
Ds. tables["Suppliers"]. Rows[index]. Delete ();
Ds. tables["Suppliers"]. AcceptChanges ();
Mydataadapter1.update (ds, "Suppliers");
Mydatagrid.datasource=ds. tables["Suppliers"]. DefaultView;
Mydatagrid.databind ();
Mydatagrid2.datasource=ds. Tables["Products"]. DefaultView;
Mydatagrid2.databind ();
}
void Createconstraint ()
{
DataColumn Parentcolumn,childcolumn;
ForeignKeyConstraint Myforeignkeyconstraint;
ParentColumn = ds. tables["Suppliers"]. columns["SupplierID"];
Childcolumn = ds. Tables["Products"]. columns["SupplierID"];
Myforeignkeyconstraint = new ForeignKeyConstraint ("Supplierforeignkeyconstraint", ParentColumn, ChildColumn);
Myforeignkeyconstraint.deleterule = Rule.cascade;
Myforeignkeyconstraint.updaterule = Rule.cascade;
Myforeignkeyconstraint.acceptrejectrule = Acceptrejectrule.cascade;
Ds. Tables["Products"]. Constraints.add (Myforeignkeyconstraint);
Ds. EnforceConstraints =true;
}
</script>
<body>
<form runat= "Server" >
<asp:datagrid id= Mydatagrid "runat=" Server "
width="
Backcolor= "#ccccff"
bordercolor= "BLACK"
Showfooter= "false"
cellpadding=3
cellspacing = "0"
font-name= "Verdana"
font-size= "8pt"
headerstyle-backcolor= "#aaaadd"
Ondeletecommand = "Mydatagrid_delete"
datakeyfield= "SupplierID"
>
<Columns>
<asp:buttoncolumn text= "Delete Employee" commandname= "Delete"/>
</Columns>
</ASP:DataGrid>
<asp:datagrid id= "MyDataGrid2" runat= "Server"
Width= "800"
Backcolor= "#ccccff"
Bordercolor= "BLACK"
Showfooter= "false"
Cellpadding=3
cellspacing= "0"
Font-name= "Verdana"
Font-size= "8pt"
Headerstyle-backcolor= "#aaaadd"
/>
</form>
</body>