none
"An empty path name is not legal." Error happened When Inserting Image From DataGridView To DataBase. RRS feed

  • Question

  • Hi, 

    I Get This Error During Inserting Image From DatagridView To DataBase Via a PictureBox.

    This Is The Codes I am Using:

    Imports System.Data.OleDb
    Imports System.IO
    
    Public Class DataOperations
    
    
    Public Function AddPhotos(ByVal FileName As String, ByVal CoupleID As String, ByVal FullPath As String) As Boolean
            Using cn As New OleDb.OleDbConnection With
                    {
                        .ConnectionString = Builder.ConnectionString
                    }
                Using cmd As New OleDb.OleDbCommand With {.Connection = cn}
                    cn.Open()
                    cmd.CommandText =
                    <SQL>
                        INSERT INTO dbPhotos  
                    ( CoupleID, Photo,  PhotoName,FullPath )  
                    Values 
                    (  @CoupleID, @Photo, @PhotoName, @FullPath )
                    </SQL>.Value
    
                    cmd.Parameters.AddRange(
                    New OleDb.OleDbParameter() _
                    { 
                    New OleDb.OleDbParameter With  { .ParameterName = "@CoupleID",.DbType = DbType.String, .Value = CoupleID },
                    New OleDb.OleDbParameter With  {.ParameterName = "@Photo",.OleDbType = OleDb.OleDbType.Binary,.Value = FileImageBytes(FileName)},
                    New OleDb.OleDbParameter With { .ParameterName = "@PhotoName",.DbType = DbType.String, .Value = IO.Path.GetFileNameWithoutExtension(FileName).ToLower},
                    New OleDb.OleDbParameter With {.ParameterName = "@FullPath",.DbType = DbType.String, .Value = FullPath}
                    }
                )
                    Try
                        Dim Affected As Int32 = cmd.ExecuteNonQuery
                        If Affected = 1 Then
                            Return True
                        Else
                            Return False
                        End If
                    Catch ex As Exception
                        Return False
                    End Try
                End Using
            End Using
        End Function
     Public Function LoadDataTours2() As DataTable
    
            Dim selectStatement As String =
                    <SQL>
    
          SELECT      LesPontes.CID,LesPontes.CoupleID
                    , LesPontes.Pontes, LesPontes.Cage
                    , LesPontes.Race 
                    , dbPhotos.Photo,dbPhotos.PhotoName,dbPhotos.FullPath
                      FROM LesPontes,dbPhotos where Pontes like 'Ponte2' and LesPontes.CoupleID = dbPhotos.CoupleID   order by LesPontes.Cage       
                    </SQL>.Value
    
            Using cn As New OleDb.OleDbConnection With
                    {
                        .ConnectionString = Builder.ConnectionString
                    }
                Using cmd As New OleDb.OleDbCommand With {.Connection = cn}
                    cmd.CommandText = selectStatement
                    Dim dt As New DataTable
    
                    cn.Open()
                    dt.Load(cmd.ExecuteReader)
                    dt.Columns("CID").ColumnMapping = MappingType.Hidden
    
                    Return dt
    
                End Using
            End Using
        End Function
    
    End Class

    2__ Code To Add Image to DataBase:

     Private Sub btnAddImage_Click(sender As Object, e As EventArgs) Handles btnAddImage.Click
          
            Try         
                       Dim Ops As New Operations
                       If f.Picture.Image Is Nothing Then
                            MessageBox.Show("Please select a image")
                            Exit Sub
                        End If                
                        If ops.AddPhotos(f.FileName, f.txtCoupleCode.Text, f.txtFullPath.Text) Then
                        Else
                            MessageBox.Show("Failed to add image to database")
                        End If                             
            Finally
                Me.Dispose()
            End Try
        End Sub

    3__ Code To Reteve Image From Database and display it Into PictureBox:

     Private Sub Dgv_MouseClick(sender As Object, e As MouseEventArgs) Handles Dgv.MouseClick
    
    
            bstours.DataSource = ops.LoadDataTours1()
            Dgv.DataSource = bstours
    
            Try
                If Dgv.SelectedRows.Count = 0 Then
                    Exit Sub
                End If
                If Dgv.Rows.Count > 0 AndAlso Dgv.CurrentRow IsNot Nothing Then
                    Dim dr As Integer = Dgv.CurrentCell.RowIndex + 0
                    If dr > -1 Then
                        txtCages.Text = Dgv.Rows(dr).Cells(2).Value.ToString()
                        txtRaces.Text = Dgv.Rows(dr).Cells(3).Value.ToString()
                        txtFullFileName.Text = Dgv.Rows(dr).Cells(6).Value.ToString()                  
                        If bstours.Current IsNot Nothing Then
                            CurrentImage = Image.FromStream(
                    New IO.MemoryStream(
                        CType(bstours.Current, DataRowView).Row.Field(Of Byte())("Photo"))
                )
                            Picture.Image = CurrentImage
                            CurrentImage = My.Resources.UnUsed
                        Else
                            Picture.Image = My.Resources.UnUsed
                        End If
                    End If
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
    
        End Sub

    Thats all,

    Thank you Very Much and Best Regards.


    • Edited by Bajtitou Wednesday, June 19, 2019 9:52 AM
    Wednesday, June 19, 2019 9:50 AM

Answers

  • Probably f.FileName is null or empty before calling ops.AddPhotos. Maybe use f.txtFullPath.Text instead.

    Hi,

    Thank you very much for your suggest.

    I Solve The Problem This Way:

    1___ Code To Add Photo:

    Imports System.Data.OleDb
    Imports System.IO
    Public Class DataOperations
    Public Function AddPhotos( ByVal CoupleID As String, imageByteArray As Byte(), ByVal PhotoName As String ,ByVal FullPath As String) As Boolean
            Using cn As New OleDb.OleDbConnection With
                    {
                        .ConnectionString = Builder.ConnectionString
                    }
                Using cmd As New OleDb.OleDbCommand With {.Connection = cn}
                    cn.Open()
                    cmd.CommandText =
                    <SQL>
                        INSERT INTO dbPhotos  
                    ( CoupleID, Photo,  PhotoName,FullPath )  
                    Values 
                    (  @CoupleID, @Photo, @PhotoName, @FullPath )
                    </SQL>.Value
    
                    cmd.Parameters.AddRange(
                New OleDbParameter() _
                {
                     New OleDbParameter With
                    {
                        .ParameterName = "@CoupleID",
                        .DbType = DbType.String,
                        .Value = CoupleID
                    },
                    
                     New OleDbParameter With
                    {
                        .ParameterName = "@Photo",
                        .OleDbType = OleDbType.Binary,
                        .Value = imageByteArray
                    },
                    New OleDbParameter With
                    {
                        .ParameterName = "@PhotoName",
                        .DbType = DbType.String,
                        .Value = PhotoName
                    },
                    New OleDbParameter With
                    {
                        .ParameterName = "@FulPath",
                        .DbType = DbType.String,
                        .Value = FullPath
                 }
                   
                }
            )
                    Try
                        Dim Affected As Int32 = cmd.ExecuteNonQuery
                        If Affected = 1 Then
                            Return True
                        Else
                            Return False
                        End If
                    Catch ex As Exception
                        Return False
                    End Try
                End Using
            End Using
        End Function
     Public Function LoadDataTours2() As DataTable
            Dim selectStatement As String =
                    <SQL>
          SELECT      LesPontes.CID,LesPontes.CoupleID
                    , LesPontes.Pontes, LesPontes.Cage
                    , LesPontes.Race 
                    , dbPhotos.Photo,dbPhotos.PhotoName,dbPhotos.FullPath
                      FROM LesPontes,dbPhotos where Pontes like 'Ponte2' and LesPontes.CoupleID = dbPhotos.CoupleID   order by LesPontes.Cage       
                    </SQL>.Value
    
            Using cn As New OleDb.OleDbConnection With
                    {
                        .ConnectionString = Builder.ConnectionString
                    }
                Using cmd As New OleDb.OleDbCommand With {.Connection = cn}
                    cmd.CommandText = selectStatement
                    Dim dt As New DataTable
    
                    cn.Open()
                    dt.Load(cmd.ExecuteReader)
                    dt.Columns("CID").ColumnMapping = MappingType.Hidden
    
                    Return dt
    
                End Using
            End Using
        End Function
    
    End Class

    2___ Code For Saving Image To DataBase:

     Private Sub cmdAddPictures_Click(sender As Object, e As EventArgs) Handles cmdAddPictures.Click
            Dim f1 As New AddImages
              Dim bsDa As New BindingSource
            If f1.ShowDialog = DialogResult.OK Then
                If bsData.Current IsNot Nothing Then
                    Dim Row As DataRow = CType(bsData.Current, DataRowView).Row
                    Dim ops = New Operations
                    ops.AddImage1(
                 Row.Field(Of Byte())("CoupleID"),
                 Row.Field(Of String)("Photo"),
                 Row.Field(Of String)("PhotoName"),
                 Row.Field(Of String)("FullPath"))
                    MessageBox.Show(" Image has been saved", "Save", MessageBoxButtons.OK)
                Else
                    MessageBox.Show("Failed to add image to database")
                End If
            End If
            Try
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
    Thank you Very Much And Best Regards.

    • Marked as answer by Bajtitou Thursday, June 20, 2019 3:51 PM
    Thursday, June 20, 2019 1:06 PM

All replies

  • Hi

    Why do you post images of an error from a Function and then post all the code except the Function where the error is occurring?


    Regards Les, Livingston, Scotland

    Wednesday, June 19, 2019 10:04 AM
  • Hello,

    I have a complete example that shows how to insert an image from file. The actual import is pretty much the same as yours but a tad different.

    This is the backend class for working with data.

    Public Class Operations 
        Public Property PictureDataTable As DataTable 
        Public Property CategoriesDataTable As DataTable 
        Public Property ParkingDataTable As DataTable 
     
     
        Private cn As OleDb.OleDbConnection = Nothing 
    #If USETHIS1 Then 
        Private TempConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Dotnet_Development\Projects_Non_Business\MSDN_Extract_Images\MS_Access_Images\UsingAccess_2007_BinaryField\bin\Debug\Data\Database1.accdb" 
        Public Sub DemoMe() 
            Demo("KG") 
        End Sub 
        Private Sub Demo(ByVal UserName As String) 
            If Not String.IsNullOrWhiteSpace(UserName) Then 
                Using cn As New OleDb.OleDbConnection With {.ConnectionString = TempConnectionString} 
                    Using cmd As New OleDb.OleDbCommand With {.Connection = cn} 
                        cmd.CommandText = 
                        <SQL> 
                            SELECT Identifier, FirstName + ' ' + LastName As FullName, JoinDate, Customers.UserName 
                            FROM Customers 
                            WHERE (((Customers.UserName)=@UserName)); 
                        </SQL>.Value 
     
                        cmd.Parameters.AddWithValue("@UserName", UserName.ToUpper) 
                        cn.Open() 
                        Try 
                            Dim Reader As OleDb.OleDbDataReader = cmd.ExecuteReader 
                            If Reader.HasRows Then 
                                Reader.Read() 
                                Dim FullName As String = Reader.GetString(1) 
                                Dim JoinDate As Date = Reader.GetDateTime(2) 
                                ' 
                                ' Show data 
                                ' 
                                Console.WriteLine("Name:{0} Joined {1}", FullName, JoinDate.ToShortDateString) 
                                Reader.Close() 
                                cmd.CommandText = "SELECT Max(Customers.Identifier) As iMax FROM Customers" 
                                Dim MaxValue = CLng(cmd.ExecuteScalar) 
                                ' 
                                ' Show Max identifier value, can do a similar SQL for row count too 
                                ' 
                                Console.WriteLine("Max ID: {0}", MaxValue) 
                            End If 
                        Catch ex As Exception 
                            Console.WriteLine(ex.Message) 
                        End Try 
                    End Using 
                End Using 
            End If 
     
        End Sub 
     
    #End If 
     
        ''' <summary> 
        ''' Add image to database table 
        ''' </summary> 
        ''' <param name="FileName">Full file name including path</param> 
        ''' <param name="Category">Key from Category table</param> 
        ''' <param name="Description">User description</param> 
        ''' <returns></returns> 
        ''' <remarks> 
        ''' Only option parameter is Description 
        ''' </remarks> 
        Public Function AddImage(ByVal FileName As String, ByVal Category As Int32, ByVal Description As String) As Boolean 
            Dim conn As MS_AccessConnection = MS_AccessConnection.GetInstance() 
            cn = conn.GetConnection(Builder.ConnectionString) 
            Using cmd As New OleDb.OleDbCommand With {.Connection = cn} 
                cmd.CommandText = 
                    <SQL> 
                        INSERT INTO Pictures  
                        ( 
                            Category, 
                            Picture, 
                            Description, 
                            BaseName, 
                            FileExtension 
                        )  
                        Values 
                        ( 
                            @Category, 
                            @Picture, 
                            @Description, 
                            @BaseName, 
                            @FileExtension 
                        ) 
                    </SQL>.Value 
     
     
                cmd.Parameters.AddRange( 
                    New OleDb.OleDbParameter() _ 
                    { 
                        New OleDb.OleDbParameter With 
                        { 
                            .ParameterName = "@Category", 
                            .DbType = DbType.Int32, 
                            .Value = Category 
                        }, 
                        New OleDb.OleDbParameter With 
                        { 
                            .ParameterName = "@Picture", 
                            .OleDbType = OleDb.OleDbType.Binary, 
                            .Value = FileImageBytes(FileName) 
                        }, 
                        New OleDb.OleDbParameter With 
                        { 
                            .ParameterName = "@Description", 
                            .DbType = DbType.String, 
                            .Value = Description 
                        }, 
                        New OleDb.OleDbParameter With 
                        { 
                            .ParameterName = "@BaseName", 
                            .DbType = DbType.String, 
                            .Value = IO.Path.GetFileNameWithoutExtension(FileName).ToLower}, 
                        New OleDb.OleDbParameter With 
                        { 
                            .ParameterName = "@FileExtension", 
                            .DbType = DbType.String, 
                            .Value = IO.Path.GetExtension(FileName).Replace(".", "").ToLower 
                        } 
                    } 
                ) 
     
                Try 
                    Dim Affected As Int32 = cmd.ExecuteNonQuery 
                    If Affected = 1 Then 
                        Return True 
                    Else 
                        Return False 
                    End If 
                Catch ex As Exception 
                    Return False 
                End Try 
     
            End Using 
        End Function 
     
        Public Sub LoadImages() 
            Dim conn As MS_AccessConnection = MS_AccessConnection.GetInstance() 
            cn = conn.GetConnection(Builder.ConnectionString) 
     
            Using cmd As New OleDb.OleDbCommand With {.Connection = cn} 
                cmd.CommandText = 
                    <SQL> 
                        SELECT  
                            Identifier,  
                            Category 
                        FROM  
                            Category 
                        ORDER BY  
                            Category 
                    </SQL>.Value 
     
                CategoriesDataTable = New DataTable 
                CategoriesDataTable.Load(cmd.ExecuteReader) 
     
                Dim dr As DataRow 
                dr = CategoriesDataTable.NewRow 
                dr("Identifier") = 0 
                dr("Category") = "ALL" 
                CategoriesDataTable.Rows.InsertAt(dr, 0) 
     
     
                cmd.CommandText = 
                    <SQL> 
                        SELECT  
                            Identifier,  
                            Category,  
                            Picture,  
                            Description,  
                            BaseName, 
                            FileExtension, 
                            BaseName + '.' + FileExtension As FullFileName 
                        FROM  
                            Pictures; 
                    </SQL>.Value 
     
                PictureDataTable = New DataTable 
                PictureDataTable.Load(cmd.ExecuteReader) 
                Console.WriteLine() 
     
            End Using 
     
        End Sub 
        ''' <summary> 
        ''' Quick example to get one row for a social forum post 
        ''' </summary> 
        ''' <param name="Primarykey"></param> 
        ''' <returns></returns> 
        ''' <remarks></remarks> 
        Public Function LoadSingleImage(ByVal Primarykey As Integer) As Tuple(Of String, Byte()) 
     
            Dim FileName As String = "" 
            Dim ImageBytes As Byte() = {} 
     
     
            Dim conn As MS_AccessConnection = MS_AccessConnection.GetInstance() 
            cn = conn.GetConnection(Builder.ConnectionString) 
     
            Using cmd As New OleDb.OleDbCommand With {.Connection = cn} 
                cmd.CommandText = 
                    <SQL> 
                        SELECT  
                            Identifier,  
                            Category,  
                            Picture,  
                            Description,  
                            BaseName, 
                            FileExtension, 
                            BaseName + '.' + FileExtension As FullFileName 
                        FROM  
                            Pictures 
                        WHERE Identifier = ? 
                    </SQL>.Value 
                cmd.Parameters.AddWithValue("?", Primarykey) 
     
                Dim dt As New DataTable 
                dt.Load(cmd.ExecuteReader) 
                ImageBytes = dt.Rows(0).Field(Of Byte())("Picture") 
                FileName = dt.Rows(0).Field(Of String)("FullFileName") 
     
                Return New Tuple(Of String, Byte())(FileName, ImageBytes) 
     
            End Using 
     
        End Function 
        Public Sub LoadParkingImages() 
            Dim conn As MS_AccessConnection = MS_AccessConnection.GetInstance() 
            cn = conn.GetConnection(Builder.ConnectionString) 
     
            Using cmd As New OleDb.OleDbCommand With {.Connection = cn} 
                cmd.CommandText = 
                    <SQL> 
                        SELECT  
                            Identifier,  
                            Category,  
                            Picture,  
                            Description,  
                            BaseName, 
                            FileExtension, 
                            BaseName + '.' + FileExtension As FullFileName 
                        FROM  
                            Pictures 
                        WHERE Category = 3 
                    </SQL>.Value 
     
                ParkingDataTable = New DataTable 
                ParkingDataTable.Load(cmd.ExecuteReader) 
                Console.WriteLine() 
     
            End Using 
     
        End Sub 
        Public Sub New() 
            LoadImages() 
            'LoadParkingImages() 
        End Sub 
    End Class 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, June 19, 2019 11:04 AM
    Moderator
  • Hi

    Why do you post images of an error from a Function and then post all the code except the Function where the error is occurring?


    Regards Les, Livingston, Scotland

    Hi, 

    I'm sorry, you're right.
    Here is the code where the error is occurring:

       Public Function FileImageBytes(ByVal pFileName As String) As Byte()
            Dim fileStream As IO.FileStream = New IO.FileStream(
                                          pFileName,
                                          IO.FileMode.Open,
                                          IO.FileAccess.Read,
                                          IO.FileShare.Read)
    
            Dim imageStream As IO.StreamReader = New IO.StreamReader(fileStream)
    
            Dim byteArray(CInt(fileStream.Length - 1)) As Byte
    
            fileStream.Read(byteArray, 0, CInt(fileStream.Length))
            Return byteArray
        End Function
    Thank you very Much and Best Regards.

    Wednesday, June 19, 2019 12:03 PM
  • Hi

    Why do you post images of an error from a Function and then post all the code except the Function where the error is occurring?


    Regards Les, Livingston, Scotland

    Hi, 

    I'm sorry, you're right.
    Here is the code where the error is occurring:

       Public Function FileImageBytes(ByVal pFileName As String) As Byte()
            Dim fileStream As IO.FileStream = New IO.FileStream(
                                          pFileName,
                                          IO.FileMode.Open,
                                          IO.FileAccess.Read,
                                          IO.FileShare.Read)
    
            Dim imageStream As IO.StreamReader = New IO.StreamReader(fileStream)
    
            Dim byteArray(CInt(fileStream.Length - 1)) As Byte
    
            fileStream.Read(byteArray, 0, CInt(fileStream.Length))
            Return byteArray
        End Function
    Thank you very Much and Best Regards.

    That should work as it's the same method I have in my code sample which means the issue is someplace else.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, June 19, 2019 12:17 PM
    Moderator

  • That should work as it's the same method I have in my code sample which means the issue is someplace else.


    Hi ,

    I Tried with your Example but I stay geting the Same Error;

    This is The code:

    Imports DataAccess
    Public Class AddImages
        WithEvents bsData As New BindingSource
        Public da As New Operations
        Public CurrentImage As Image
        Private Sub AddImages_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            bsData.DataSource = da.PictureDataTable1
            Dgv.DataSource = bsData
        End Sub
    
      Private Sub cmdAddingImage_Click(sender As Object, e As EventArgs) Handles cmdAddingImage.Click
            If Picture.Image Is Nothing Then
                MessageBox.Show("Please select a image")
                Exit Sub
            End If
    
            DialogResult = Windows.Forms.DialogResult.OK
        End Sub
    
        Private Sub Dgv_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles Dgv.CellMouseClick
            Try
                If Dgv.SelectedRows.Count = 0 Then
                    Exit Sub
                End If
                If Dgv.Rows.Count > 0 AndAlso Dgv.CurrentRow IsNot Nothing Then
                    Dim dr As Integer = Dgv.CurrentCell.RowIndex + 0
                    If dr > -1 Then
    
                        ' txtFullFileName.Text = Dgv.Rows(dr).Cells(5).Value.ToString()
                        txtFullFileName.Text = Dgv.Rows(dr).Cells(5).Value.ToString()
                        ' Pictures 
                        If bsData.Current IsNot Nothing Then
    
                            CurrentImage = Image.FromStream(
                    New IO.MemoryStream(
                        CType(bsData.Current, DataRowView).Row.Field(Of Byte())("Picture"))
                )
    
                            Picture.Image = CurrentImage
                            CurrentImage = Nothing
                        Else
                            Picture.Image = Nothing
                        End If
                    End If
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
    End Class

    ScreenShot;

    Thank you Very Much.

    Wednesday, June 19, 2019 4:35 PM
  • Probably f.FileName is null or empty before calling ops.AddPhotos. Maybe use f.txtFullPath.Text instead.

    Thursday, June 20, 2019 6:22 AM
  • Probably f.FileName is null or empty before calling ops.AddPhotos. Maybe use f.txtFullPath.Text instead.

    Hi,

    Thank you very much for your suggest.

    I Solve The Problem This Way:

    1___ Code To Add Photo:

    Imports System.Data.OleDb
    Imports System.IO
    Public Class DataOperations
    Public Function AddPhotos( ByVal CoupleID As String, imageByteArray As Byte(), ByVal PhotoName As String ,ByVal FullPath As String) As Boolean
            Using cn As New OleDb.OleDbConnection With
                    {
                        .ConnectionString = Builder.ConnectionString
                    }
                Using cmd As New OleDb.OleDbCommand With {.Connection = cn}
                    cn.Open()
                    cmd.CommandText =
                    <SQL>
                        INSERT INTO dbPhotos  
                    ( CoupleID, Photo,  PhotoName,FullPath )  
                    Values 
                    (  @CoupleID, @Photo, @PhotoName, @FullPath )
                    </SQL>.Value
    
                    cmd.Parameters.AddRange(
                New OleDbParameter() _
                {
                     New OleDbParameter With
                    {
                        .ParameterName = "@CoupleID",
                        .DbType = DbType.String,
                        .Value = CoupleID
                    },
                    
                     New OleDbParameter With
                    {
                        .ParameterName = "@Photo",
                        .OleDbType = OleDbType.Binary,
                        .Value = imageByteArray
                    },
                    New OleDbParameter With
                    {
                        .ParameterName = "@PhotoName",
                        .DbType = DbType.String,
                        .Value = PhotoName
                    },
                    New OleDbParameter With
                    {
                        .ParameterName = "@FulPath",
                        .DbType = DbType.String,
                        .Value = FullPath
                 }
                   
                }
            )
                    Try
                        Dim Affected As Int32 = cmd.ExecuteNonQuery
                        If Affected = 1 Then
                            Return True
                        Else
                            Return False
                        End If
                    Catch ex As Exception
                        Return False
                    End Try
                End Using
            End Using
        End Function
     Public Function LoadDataTours2() As DataTable
            Dim selectStatement As String =
                    <SQL>
          SELECT      LesPontes.CID,LesPontes.CoupleID
                    , LesPontes.Pontes, LesPontes.Cage
                    , LesPontes.Race 
                    , dbPhotos.Photo,dbPhotos.PhotoName,dbPhotos.FullPath
                      FROM LesPontes,dbPhotos where Pontes like 'Ponte2' and LesPontes.CoupleID = dbPhotos.CoupleID   order by LesPontes.Cage       
                    </SQL>.Value
    
            Using cn As New OleDb.OleDbConnection With
                    {
                        .ConnectionString = Builder.ConnectionString
                    }
                Using cmd As New OleDb.OleDbCommand With {.Connection = cn}
                    cmd.CommandText = selectStatement
                    Dim dt As New DataTable
    
                    cn.Open()
                    dt.Load(cmd.ExecuteReader)
                    dt.Columns("CID").ColumnMapping = MappingType.Hidden
    
                    Return dt
    
                End Using
            End Using
        End Function
    
    End Class

    2___ Code For Saving Image To DataBase:

     Private Sub cmdAddPictures_Click(sender As Object, e As EventArgs) Handles cmdAddPictures.Click
            Dim f1 As New AddImages
              Dim bsDa As New BindingSource
            If f1.ShowDialog = DialogResult.OK Then
                If bsData.Current IsNot Nothing Then
                    Dim Row As DataRow = CType(bsData.Current, DataRowView).Row
                    Dim ops = New Operations
                    ops.AddImage1(
                 Row.Field(Of Byte())("CoupleID"),
                 Row.Field(Of String)("Photo"),
                 Row.Field(Of String)("PhotoName"),
                 Row.Field(Of String)("FullPath"))
                    MessageBox.Show(" Image has been saved", "Save", MessageBoxButtons.OK)
                Else
                    MessageBox.Show("Failed to add image to database")
                End If
            End If
            Try
            Catch ex As Exception
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
    Thank you Very Much And Best Regards.

    • Marked as answer by Bajtitou Thursday, June 20, 2019 3:51 PM
    Thursday, June 20, 2019 1:06 PM