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
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.
Step-2 Now Double click on form (or press F7) -->Write the following codes as given below.I have already explained all constraints in Sql Server tutorials as
- How to add Primary and Unique key constraints
- How to add Foreign Key Constraint
- How to add Check constraint
- How to add Default key constraint
- How to make an identity column
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.
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]);
dtstudent.Constraints.Add(unique_c);
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;
dtcourse.Constraints.Add(foreign_c);
GridView1.DataSource = dtstudent;
GridView1.DataBind();
GridView2.DataSource = dtcourse;
GridView2.DataBind();
DataSet ds = new DataSet();
ds.Tables.Add(dtstudent);
ds.Tables.Add(dtcourse);
}
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)
{
if(dtstudent.Rows.Count>0)
{
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";
}
else
{
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);
dr.Delete();
Label4.Text = "Record not deleted successfully";
}
else
{
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
Form More...
- Param keyword in c#
- Out parameter in c#
- Structure and class in c#
- Joins in Sql server
- Views in sql server
- Properties in c#
- How to take print receipt in win app
- Method hiding in c#
- Data mining in asp.net
- Create captcha image in asp.net application
Download whole attached file
Download
0 comments:
Post a Comment