Storing Image in VB.Net

This tutorial shows you on how to store encrypted image from Visual Basic.Net to MS SQL Server 2000.
How it works?
1. Design MSSQL Server 2000 database
  • studentno - varchar 
  • name - varchar 
  • course - varchar 
  • mobileno - varchar 
  • picture - image 
2. Design user interface looks like figure 1 (see below)
3. Inside the button event, apply code snippet. (see below)
Code snippet
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
            cm = New SqlClient.SqlCommand
            With cm
                .Connection = cn
                .CommandType = CommandType.Text
                .CommandText = "INSERT INTO Student (studentno,name,course,mobileno,picture) VALUES (@sno,@sname,@scourse,@smobile,@sPicture)"
                .Parameters.Add(New System.Data.SqlClient.SqlParameter("@sno", SqlDbType.VarChar, 255, Me.txtSno.Text))
                .Parameters.Add(New System.Data.SqlClient.SqlParameter("@sname", SqlDbType.VarChar, 255, Me.txtName.Text))
                .Parameters.Add(New System.Data.SqlClient.SqlParameter("@scourse", SqlDbType.VarChar, 255, sCourse))
                .Parameters.Add(New System.Data.SqlClient.SqlParameter("@smobile", SqlDbType.VarChar, 255, Me.txtMobile.Text))
                .Parameters.Add(New System.Data.SqlClient.SqlParameter("@sPicture", SqlDbType.VarBinary, 2147483647, Me.OpenFileDialog1.FileName))

                Dim FileStream1 As System.IO.FileStream
                Dim FileInfo1 As System.IO.FileInfo

                FileInfo1 = New System.IO.FileInfo(OpenFileDialog1.FileName)
                FileStream1 = New System.IO.FileStream(OpenFileDialog1.FileName, IO.FileMode.Open)
                Dim Array1(CInt(FileInfo1.Length - 1)) As Byte
                Debug.WriteLine(FileStream1.Read(Array1, 0, CInt(FileInfo1.Length)))

                Dim String1 As String
                Dim ASCIIEncoding1 As New System.Text.ASCIIEncoding()
                String1 = ASCIIEncoding1.GetString(Array1)

                cm.Parameters("@sno").Value = Me.txtSno.Text
                cm.Parameters("@sname").Value = Me.txtName.Text
                cm.Parameters("@scourse").Value = Me.cboCourse.Text
                cm.Parameters("@smobile").Value = Me.txtMobile.Text
                cm.Parameters("@sPicture").Value = Array1
                MsgBox("Record saved.", MsgBoxStyle.Information)
                Call clear()
            End With
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub

Figure 1:
User Interface using Visual Basic.Net

Programming Language: VB.Net
Programming IDE: Visual Studio 2010
Database: MS SQL