Jak mogę przenieść obraz z jednej szuflady z obrazkami na inny? VB.NET & SQL

0

Pytanie

Staram się stworzyć system logowania, w którym zapisuję poświadczenia i pożądany obraz profilu w bazie danych programu access. pożądany efekt polega na tym, że, gdy login jest identyczna, forma 1 zamyka się i otwiera formularz 2 z zapisanym obrazem, pobranych w rogu.

starałem się zrobić to

Forma 1 kod:

Imports System.Data.OleDb
Imports System.IO
Public Class Form1

    Private PictureFormat As String

    Private Sub FillPictureBoxFromFile()
        With OpenFileDialog1
            .Filter = "(*.jpg)|*.jpg|(*.png)|*.png"
            .RestoreDirectory = True
            .Title = "Select a file to open"
            If .ShowDialog = DialogResult.OK Then
                PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
            End If
            PictureFormat = Path.GetExtension(OpenFileDialog1.FileName)
        End With
    End Sub

    Private cnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\geral\source\repos\Login Fase 3 Final\Login Fase 3 Final\bin\Release\DBLoginPic.mdb"

    Private Sub saveimage()
        Dim arrimage() As Byte
        Using mstream As New System.IO.MemoryStream
            If PictureFormat.ToLower = ".png" Then
                PictureBox1.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Png)
            ElseIf PictureFormat.ToLower = ".jpg" Then
                PictureBox1.Image.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)
            End If
            arrimage = mstream.GetBuffer()
            Dim Filesize As Long
            Filesize = mstream.Length
        End Using
        Using con As New OleDbConnection(cnStr),
                cmd As New OleDbCommand("Insert into TBLoginPic (Imagen) Values (@Imagen)", con)
            With cmd
                .Parameters.Add("@Imagen", OleDbType.Binary).Value = arrimage
                '.Parameters.Add("@Nombre", OleDbType.VarChar).Value = TextBox1.Text
                con.Open()
                .ExecuteNonQuery()
            End With
        End Using
    End Sub

    Private Function GetDataByName(name As String) As DataTable
        Dim dt As New DataTable
        Using conn As New OleDb.OleDbConnection(cnStr),
                cmd As New OleDbCommand("Select * from TBLoginPic where Usuario= @Buscar", conn)
            cmd.Parameters.Add("@Buscar", OleDbType.VarChar).Value = TBusuario.Text
            conn.Open()
            Using reader = cmd.ExecuteReader
                dt.Load(reader)
            End Using
        End Using
        Return dt
    End Function


    Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click

    End Sub

    Private Sub TBusuario_TextChanged(sender As Object, e As EventArgs) Handles TBusuario.TextChanged

    End Sub

    Private Sub TBcontraseña_TextChanged(sender As Object, e As EventArgs) Handles TBcontraseña.TextChanged

    End Sub

    Private Sub BtnLoguearse_Click(sender As Object, e As EventArgs) Handles BtnLoguearse.Click
        If Me.TBLoginPicTableAdapter.BuscarDatos(Me.DBLoginPicDataSet.TBLoginPic, TBusuario.Text, TBcontraseña.Text) Then

            Dim dt As DataTable
            Try
                dt = GetDataByName(TBusuario.Text)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                Exit Sub
            End Try

            TBusuario.Text = dt(0)("Usuario").ToString
            Dim arrimage = DirectCast(dt(0)("Imagen"), Byte())
            Dim mstream As New System.IO.MemoryStream(arrimage)
            PictureBox1.Image = Image.FromStream(mstream)

            Form2.Show()
            _selectedImage = PictureBox1.Image
            Me.Close()
        Else
            MsgBox("Datos Erroneos")
        End If
    End Sub

    Private Sub BtnRegistrarse_Click(sender As Object, e As EventArgs) Handles BtnRegistrarse.Click


        Me.TBLoginPicBindingSource.AddNew()
        Me.TBLoginPicBindingSource.EndEdit()
        Me.TBLoginPicTableAdapter.Update(Me.DBLoginPicDataSet)


        Try
            saveimage()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Exit Sub
        End Try
        MsgBox("Image has been saved in the database")

    End Sub

    Private Sub BtnExaminar_Click(sender As Object, e As EventArgs) Handles BtnExaminar.Click
        FillPictureBoxFromFile()
    End Sub

    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DBLoginPicDataSet.TBLoginPic' table. You can move, or remove it, as needed.
        Me.TBLoginPicTableAdapter.Fill(Me.DBLoginPicDataSet.TBLoginPic)



    End Sub

    Private Sub TBLoginPicBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
        Me.Validate()
        Me.TBLoginPicBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.DBLoginPicDataSet)

    End Sub
End Class

Forma 2 Kod:

Module imagen
    Public _selectedImage As Image
    Public ReadOnly Property SelectedImage As Image
        Get
            Return _selectedImage
        End Get
    End Property
End Module

Public Class Form2



    Private Sub TBLoginPicBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
        Me.Validate()
        Me.TBLoginPicBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.DBLoginPicDataSet)

    End Sub

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'DBLoginPicDataSet.TBLoginPic' table. You can move, or remove it, as needed.
        Me.TBLoginPicTableAdapter.Fill(Me.DBLoginPicDataSet.TBLoginPic)
        _selectedImage = PBPerfil.Image

    End Sub

    Public Sub PBPerfil_Click(sender As Object, e As EventArgs) Handles PBPerfil.Click

    End Sub
End Class

i będzie tego błędu

System.Wyjątek InvalidCastException: System "nie można prowadzić obiekt".DBNull System "do wprowadzenia".Byte[]'.'

Oto jak wygląda forma 1

Oto jak wygląda forma 2

Oto jak wygląda moja baza danych (nie martw się, nie ma tu żadnych danych osobowych))

Tutaj staram się wysłać go z formy 1 w 2

Oto jak próbuję dostać go w formie 2

jakieś porady na temat tego, co mogłem zrobić lepiej?

database forms image picturebox
2021-11-24 05:29:42
1

Najlepsza odpowiedź

0

Trzeba sprawdzić, czy zer. Jeśli w kolumnie obrazu brak danych, przy próbie manipulowania nimi wystąpi błąd. Radziłbym ci wybrać domyślny obraz, aby wyświetlić, jeśli w bazie danych nie ma go.

    If dt(0)("Usuario") IsNot Nothing OrElse Not IsDBNull(dt(0)("Usuario")) Then
        TBusuario.Text = dt(0)("Usuario").ToString
        Dim arrimage = DirectCast(dt(0)("Imagen"), Byte())
        Dim mstream As New System.IO.MemoryStream(arrimage)
        PictureBox1.Image = Image.FromStream(mstream)
    Else
        PictureBox1.Image = Image.FromFile("DefaultImage.png")
    End If
2021-11-24 08:18:30

jak mogę dodać to zdjęcie? czy trzeba wskazywać drogę, podobny ciągu połączenia, na przykład: Provider=Microsoft.Jet.OLEDB.4.0;source=C:\Użytkownicy\ogólne\źródła\repozytorium\Faza wejścia 3 Ostateczna\Faza wejścia 3 Ostateczna\bin\Release\DBLoginPic.mdb. czy mam po prostu wpisać tam ścieżka do pliku? czy istnieje jakiś inny sposób?
THEwed123wet

W innych językach

Ta strona jest w innych językach

Русский
..................................................................................................................
Italiano
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................