SERIALIZING A CLASS - OVERVIEW:
Serializing a class is more complicated than serializing
say, an ArrayList. This example shows how to serialize a
custom class to a database table.
LAST TECHNICAL REVIEW: 27-AUG-2004
ARTICLE:
This custom class represents user information.
The class is contained in this listing:
UserInfo.vb
Imports System
Namespace myComponents
<serializable()> Public Class UserInfo
Public Username As String
Public Password As String
Public Email As String
End Class
End Namespace
The UserInfo class contains
three properties: Username, Password, and E-mail. Notice that
the class is declared with
the <serializable()>
attribute. Without this attribute, you could not serialize
the class.
Before you can use the UserInfo class, you'll need
to compile it. Execute the following from the command line:
vbc
/t:Library UserInfo.vb
Next, copy the compiled class, UserInfo.dll,
you your applications /bin directory.
Placing the UserInfo.dll file in your /bin
directory exposes the class to your ASP.NET application.
Next,
you need to create the Microsoft SQL Server database table
where you will store the serialized version of the UserInfo class.
You'll serialize the class to an Image column. The SQL CREATE TABLE statement show below creates the necessary
database table when executed from Query Analyzer.
CreateUserList.sql
CREATE TABLE UserList (
user_id INT NOT NULL IDENTITY,
username VARCHAR( 50 ),
userinfo Image )
Now that you have the UserInfo class and
a database table in whoch to store it, you can create a page that serializes
the UserInfo class. The page below
enables
you to assign values to the properties of the UserInfo class through a form
and serialize it to the UserList database table.
Note that the SerializeClass.aspx
page opens a connection to a database named myData. You'll need
to modify the
database connection string to open a connection
to the particular database where you created the UserList table.
SerializationClass.aspx
<%@ Import Namespace="myComponents" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Runtime.Serialization.Formatters.Binary" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<Script runat="Server">
Sub Button_Click( s As Object, e As EventArgs )
Dim objUserInfo As UserInfo
Dim objMemoryStream As MemoryStream
Dim objBinaryFormatter As BinaryFormatter
Dim conMyData As SqlConnection
Dim cmdInsert As SqlCommand
objUserInfo = New UserInfo
objUserInfo.Username = txtUsername.Text
objUserInfo.Password = txtPassword.Text
objUserInfo.Email = txtEmail.Text
objMemoryStream = New MemoryStream
objBinaryFormatter = New BinaryFormatter
objBinaryFormatter.Serialize( objMemoryStream, objUserInfo )
conMyData = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;Database=myData" )
cmdInsert = New SqlCommand( _
"Insert Userlist ( username, userinfo ) Values ( @username, @userinfo )",
_
conMyData )
cmdInsert.Parameters.Add( "@username", txtUsername.Text )
cmdInsert.Parameters.Add( "@userinfo", objMemoryStream.ToArray()
)
conMyData.Open()
cmdInsert.ExecuteNonQuery()
conMyData.Close()
End Sub
</Script>
<html>
<head><title>SerializeClass.aspx</title></head>
<body>
<form runat="Server">
Username:
<br><asp:TextBox
id="txtUsername"
runat="Server" />
<p>
Password:
<br><asp:TextBox
id="txtPassword"
runat="Server" />
<p>
Email:
<br><asp:TextBox
id="txtEmail"
runat="Server" />
<p>
<asp:Button
Text="Submit!"
OnClick="Button_Click"
runat="Server" />
</form>
</body>
</html>
When you submit the form, the Button_Click subroutine
is executed. This subroutine creates an instance of the UserInfo class
and assigns the values from the form fields to the properties of the class.
Note that the use of a MemoryStream in
the above example is simalar to a FileStream. However,
it does not represent a file on the hard drive. A MemoryStream represents
a block of memory.
STILL NEED HELP?
If you are
still having problems, come to the #asp.net channel on DALnet.
Or, you can chat with RockydotNet now using InstantHelp.