Hi all,
I am using ASP.Net in my project . It is my first time that i use Asp.net. I also use ADO.NET for database connectivity. Can anybody have any idea how to use attribute controls in ADO.Net for validation. Thanks in advance.
Hi all,
I am using ASP.Net in my project . It is my first time that i use Asp.net. I also use ADO.NET for database connectivity. Can anybody have any idea how to use attribute controls in ADO.Net for validation. Thanks in advance.
Attributes is a common language runtime allows you to add keyword-like descriptive declarations, to annotate programming elements such as types, fields, methods, and properties. Attributes are saved with the metadata of a Microsoft .NET Framework file and can be used to describe your code to the runtime or to affect application behavior at run time. While the .NET Framework supplies many useful attributes, you can also design and deploy your own. Best of luck.
You can use the System.ComponentModel.DescriptionAttribute to provide some information about the class:
As you can see, the Description attribute is attached to the Customer class.Code:Public Class Customer Private _nam As String Public Property Name() As String Get Ret _name End Get Set(ByVal Value As String) _name = Value End Set End Property End Class
I will give you the example Custom Attributes.There are many attributes provided by the .NET Framework, but it gets really interesting when you build your own attributes. To do so, the Attribute class can be used to inherit from.
Public Class ExtendeDescriptionAttribute
Inherits System.Attribute
Private _desc As String
Private _displayr As Integer
Public Sub New(ByVal description As String, ByVal displayOrder As Integer)
MyBase.New()
_desc = description
_dispOrr = displayOrder
End Sub
Public Property Description() As String
Get
Ret _description
End Get
Set(ByVal Value As String)
_desc= Value
End Set
End Property
Public Property DispOr() As Integer
Get
Return _dispOr
End Get
Set(ByVal Value As Integer)
_dispOr = Value
End Set
End Property
End Class
The usage of attributes seems to be limited. By using custom attributes you can make more generic functionality. I will give the example that a set of attributes that allows developers to easily add validation, with a minimum of code:
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal Value As String)
_name = Value
End Set
End Property
As you can see, two attributes are attached to the Name property. They will enforce two validation rules for that property: the name cannot be empty and the maximum length is 30 characters. These attributes will be the only thing you need to add to your class properties to have validation.
Working with attributes in .NET is great. In mixture with Reflection you can create broad and extensible solutions for ordinary problems very easily. As you can see in the example explain in above reply, once the base functionality is built, extending it is very easy .So you can build more custom ValidatorAttributes to carry out more validation rules, for example checking the minimum and maximum value of numeric properties, capitalization, dates etc. Best of luck.
Bookmarks