How to create Constraints in ADO.NET Application

In this tutorial ,I am going to explain "How to use constraints in Ado.Net application ".This is very important concept on the basis of security.All constraints,which are used in .NET applications,are used for some specific purpose.All the constraints basically provide some restriction for end users.If any user want to access or modify the information, then he can't access or modify all information because developer already putted some restriction for accessing the information on your application .
I have already explained all constraints in Sql Server tutorials as
Please Read above links so that you can easily implement each constraints in below application.There are some steps to implement this concept in application which are given below:-

Step-1 First open your visual studio --> File -->New --> Website --> Select ASP.NET Empty website -->OK-->Open Solution Explorer-->Add a New web form -->Drag and drop Label,Text Box ,Button and Gridview controls on the form as shown below.

web form

Step-2 Now Double click on form (or press F7) -->Write the following codes as given below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class _Default : System.Web.UI.Page
    DataTable dtstudent = new DataTable();
    DataTable dtcourse = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
        dtstudent.Columns.Add(new DataColumn("stid", typeof(int)));
        dtstudent.Columns.Add(new DataColumn("sname", typeof(string)));
        dtstudent.Rows.Add(101, "Ram");
        dtstudent.Rows.Add(102, "Ramesh");
        dtstudent.Rows.Add(103, "Ajay");
        dtstudent.Rows.Add(104, "Schin");
        dtstudent.Rows.Add(106, "Neha singh");
        dtstudent.PrimaryKey = new DataColumn[] { dtstudent.Columns[0] };
        UniqueConstraint unique_c = new UniqueConstraint(dtstudent.Columns[1]);

        dtcourse.Columns.Add(new DataColumn("sid", typeof(int)));
        dtcourse.Columns.Add(new DataColumn("cname", typeof(string)));
        dtcourse.Rows.Add(101, "Dot Net");
        dtcourse.Rows.Add(102, "Java");
        dtcourse.Rows.Add(103, "C++");
        dtcourse.Rows.Add(104, "C & PHP");
        dtcourse.Rows.Add(105, "Python & Perl");
        ForeignKeyConstraint foreign_c = new ForeignKeyConstraint(dtstudent.Columns[0], dtcourse.Columns[0]);
        foreign_c.DeleteRule = Rule.None;
        foreign_c.UpdateRule = Rule.None;
        GridView1.DataSource = dtstudent;
        GridView2.DataSource = dtcourse;
        DataSet ds = new DataSet();
    protected void Button1_Click(object sender, EventArgs e)
            dtstudent.Rows.Add(TextBox1.Text, TextBox2.Text);
            Label2.Text = " Record successfully inserted in Parent table";
    protected void Button2_Click(object sender, EventArgs e)
       string str = "stid='" + TextBox3.Text+ "'"; 
   DataView dview = new DataView(dtstudent);
   dview.RowFilter = str;
   dtstudent = dview.ToTable();

   if (dtstudent.Rows.Count > 0)
       dtcourse.Rows.Add(TextBox3.Text, TextBox4.Text);
       Label3.Text = "Record successfully inserted in Child table";
       Label3.Text = "You can't insert the data in child table because in parent table this id is not present";
      protected void Button3_Click1(object sender, EventArgs e)
        if (dtcourse.Rows.Count <0)
            DataRow dr = dtstudent.Rows.Find(TextBox5.Text);
            Label4.Text = "Record not deleted successfully";  
            Label4.Text = "Record deleted successfully";

Step-3 Now Run the application (press F5) --> Enter the Text Box Fields and press insert or Delete button as shown below:-


Note :- In this application i have implemented only three constraints which are given below:-
  • Primary key constraints
  • Unique key constraints
  • Foreign key constraints   
You can implement other also.
Form More...
  1. Param keyword in c#
  2. Out parameter in c#
  3. Structure and class in c#
  4. Joins in Sql server
  5. Views in sql server
  6. Properties in c#
  7. How to take print receipt in win app
  8. Method hiding in c#
  9. Data mining in
  10. Create captcha image in application

Download whole attached file


Post a Comment

Powered by Blogger.