Saving file to a network database. OLE objects

korae

Member
Joined
Jan 24, 2010
Messages
22
Programming Experience
Beginner
I have this code that creates a template and stores that template to a local database and I want it also to store in a network database but I'm having problems on the other end it seems it's not storing the right data.

Server side Code:
VB.NET:
Dim cnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=|DataDirectory|\fp.mdb; Jet OLEDB:Database Password = joceradmin"
            Dim fpFilename As String
            Dim userID As String

            userID = InputBox("Please enter ID number to enroll", "Enter ID Number", , 100, 100)
            Dim name As String = userID

            fpFilename = name & ".fpt"

            Using fs As IO.FileStream = IO.File.Open(fpFilename, IO.FileMode.Create, IO.FileAccess.Write)
                Template.Serialize(fs)
            End Using

            Dim fs1 As FileStream

            fs1 = New FileStream(fpFilename, FileMode.Open, FileAccess.Read)
            Dim picByte As Byte() = New Byte(fs1.Length - 1) {}
            fs1.Read(picByte, 0, System.Convert.ToInt32(fs1.Length))
            fs1.Close()

            Label1.Text = BitConverter.ToString(picByte)

            Dim CN As New OleDbConnection(cnString)
            CN.Open()

            Dim strSQL As String = "INSERT INTO myfp values (" & userID & ", @Img)"
            Dim id As Integer = userID
            Dim strText As String = BitConverter.ToString(picByte)

            'strSQL = "INSERT INTO myfp values ('" & userID & "', @Img)"

            Dim imgParam As New OleDbParameter()

            imgParam.OleDbType = OleDbType.Binary
            imgParam.ParameterName = "Img"
            imgParam.Value = picByte

            Dim cmd As New OleDbCommand(strSQL, CN)

            cmd.Parameters.Add(imgParam)
            Try
                cmd.ExecuteNonQuery()
            
                For Each cc As ConnectedClient In clients
                    cc.SendMessage("/UPflag|" & id & "|" & strText)
                Next cc

                MessageBox.Show("Image successfully saved.")

                'delete physical .fpt file if exist
                If System.IO.File.Exists(fpFilename) = True Then
                    System.IO.File.Delete(fpFilename)
                End If

                cmd.Dispose()
                CN.Close()
                CN.Dispose()


Here's receiving end
VB.NET:
Dim data() As String = message.Split("|"c)
        Dim id As Integer = data(1)
        'Dim ByteArray() As Byte
        'ByteArray = System.Text.Encoding.ASCII.GetBytes(data(2))

        Dim myConnection As OleDbConnection = New OleDbConnection
        Dim myCommand As OleDbCommand = New OleDbCommand
        myConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\employees.mdb; Jet OLEDB:Database Password = joceradmin"
        myConnection.Open()
        myCommand.Connection = myConnection
        myCommand.CommandText = "Update employee set fp_status = 0 where id = " & id & ""
        myCommand.ExecuteNonQuery()

        Dim cnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=|DataDirectory|\fp.mdb; Jet OLEDB:Database Password = joceradmin"
        Dim fpFilename As String

        fpFilename = data(1) & ".fpt"

        'Using fs As IO.FileStream = IO.File.Open(fpFilename, IO.FileMode.Create, IO.FileAccess.Write)
        'Template.Serialize(fs)
        'End Using

        Dim fs1 As FileStream

        fs1 = New FileStream(fpFilename, FileMode.Open, FileAccess.Read)
        Dim picByte As Byte() = New Byte(fs1.Length - 1) {}
        fs1.Read(picByte, 0, System.Convert.ToInt32(fs1.Length))
        fs1.Close()

        Try
            Dim CN As New OleDbConnection(cnString)
            CN.Open()

            Dim strSQL As String = "INSERT INTO myfp values (" & data(1) & ", @Img)"

            Dim imgParam As New OleDbParameter()

            imgParam.OleDbType = OleDbType.Binary
            imgParam.ParameterName = "Img"
            imgParam.Value = picByte

            Dim cmd As New OleDbCommand(strSQL, CN)

            cmd.Parameters.Add(imgParam)
            cmd.ExecuteNonQuery()

            'delete physical .fpt file if exist
            If System.IO.File.Exists(fpFilename) = True Then
                System.IO.File.Delete(fpFilename)
            End If

            cmd.Dispose()
            CN.Close()
            CN.Dispose()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
 

Latest posts

Back
Top