Microsoft Technologies, ASP.NET & SQL SERVER Tips with Rajat Jaiswal

Month: January 2009

As a programmer my first intention is to make a bug free code for that one of the part is data validation.What ever we want to insert /Update in database need to check.

For this we use data validation. With linq we can refine data in mainly two way. The first and basic way in which we do not have to do so much is Schema.

What ever OR mapping is done. The schema handle the database type which require to save in database. By this a user can not insert invalid datatype in DataBase.

Second and another important aspect is using validation rules or custom validation. This can be handle in two way

1) custom property validation support.

This can be understand by below example suppose I want to insert email address in database with proper email format. But if there is no validation for format check then user can insert and data in email field for this purpose we require cutome property validation.

As the class genrated with LINQ to sql is partial class so we can use it any where we want with just partial class use.

Imports system

Imports system.text.RegularExpression

Partial Class Customer

Private Sub OnstrEmailChanging(ByVal value As String)

Dim regMailEx As New System.Text.RegularExpressions.Regex(“^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$”)

Dim matchExp As System.Text.RegularExpressions.Match

matchExp = regMailEx.Match(value)

If matchExp.Success =false Then

Throw new applicationException (“e-mail is not valid!”)

end if

End Sub

End class

In the above way we can validate a entry. Main benefit of this we do not have to write on each page and call validation. Its works automatically when user changed the Email.

Another is custom entry object validation.

In this suppose you want to compare two or more than two different object or some other logic then we can use this it can be handle is partial class with onValidate method.

Serialization in .NET allows us to take an instance of an object and convert it into a format that is easily transmittable over the network, or even stored in a database or file system.

This object will actually be an instance of a custom type, including any properties or fields you may have set.

Serialization is the process of converting an object into a stream of bytes which is easily transferable over the network and storing its current state on any permanent storage media like file or database for later use.

De- Serialization:-

De-serialization is the reverse process of Serialization, where the stored object is taken and the original object is recreated.

.NET provides classes through its System.Runtime.Serialization namespaces that can be used for serializing and de-serializing objects.

Serialization can be divided into following types:

· Binary Serialization: Binary serialization allows the serialization of an object into a binary stream and restoration from a binary stream into an object.

· XML Serialization: XML serialization allows the public properties and fields of an object to be reduced to an XML document that describes the publicly visible state of the object.

· SOAP Serialization: SOAP serialization is similar to XML serialization in that the objects being serialized are persisted as XML.

Below is example how to make a serialize class

And how to serialize and deserialize class.

<Serializable()> _

Private Class pvt_clsTest

Public strName As String = String.Empty

End Class

Private Sub pvt_SearializeMe()

Try

Dim cls As New pvt_clsTest

cls.strName = Me.txtNeedToSerialize.Text

Dim mem As New MemoryStream()

Dim binaryFormat As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()

binaryFormat.Serialize(mem, cls)

Session(“arr”) = mem.GetBuffer

Me.txtSerialize.Text = mem.ToString

mem.Close()

Catch ex As Exception

Throw ex

End Try

End Sub

Private Sub pvt_DeserializeMe(ByVal arr As Byte())

Dim ascEn As New ASCIIEncoding

Dim mem As New MemoryStream(arr)

mem.Position = 0

Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()

Hello friends first of all happy new year to all of you. Today we will work on how to Save ,Update & delete record from database using linq.

For using this you have to work on Linq To sql class Object which you can find when you add new item . Its extension is “DBML”.

When you create object of this then it will add datacontext in end.

Suppose you given name mydb.dbml then its object will be “mydbDataContext”.

You will be find this in attach document (Pic -1). Its basically a ORM (object Relational mapper) just drop the table from database. You will find the relation structure as like in (pic -2) in document.

The drop table is now work as individual class and & the fields of the table work as property of class.

Suppose if you drop table Customer then it will work as customer class and fields are work as property of customer class.

Now suppose you want to add new customer in customer table in database.

Then you have to create a object of customer class like below

Dim dc as new MyDbDataContext (its object of your added dbml)

Dim obj as new customer

Obj.strFirstName = “RAJAT”

Obj.strLastName = “JAISWAL”

Dc.customer.InsertOnSubmit(obj)

Dc.submitOnChanges()

The above code add the data in database.

Now suppose you want to edit the same record from database then you have to write following code

Dim dc as new myDbDataContext

Dim lnq = (from a in dc.customer where lngId = 1).singleOrDefault()

Lnq.strFirstName =”RAJAT”

Lnq.strLastName =”JAI”

Dc.submitChanges()

In that way you can update the customer record.

Now suppouse you want to delete record from database. Then you have to write following code.