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

  • Thread starter Thread starter Bajtitou
  • Start date Start date
B

Bajtitou

Guest
Hi,

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

1451899.png

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.

Continue reading...
 
Back
Top