Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Help inserting image to database please...

$
0
0

Hi I am trying to create a small utensils application for a cooking class... it contains ID, Name, Description, Pic, and PicLocation... Pic is varbinary(MAX) not image, I've read the articles...being phased out. I'm using Visual Studio 2010 w/SQLExpress 2008 R2... I am using WPF and entity framework 4.0.3... Everything works fine up to the actual save... then I can save the text, but not the pic or the piclocation.... Can anyone please help

Imports TestsDAL
Imports System.Windows.Data
Imports Microsoft.Win32

Public Class KitchenUtensilsMain

    Private db As New BoxEntities
    Private UtensilsData As UtensilsCollection

    Private UtensilsViewSource As CollectionViewSource
    Private WithEvents UtensilsView As ListCollectionView

    Private Sub uc1_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles uc1.Loaded

        Dim query = From u In db.Utensils
                    Select u

        Me.UtensilsData = New UtensilsCollection(query, db)

        Me.UtensilsViewSource = CType(Me.FindResource("UtensilsViewSource"), CollectionViewSource)
        Me.UtensilsViewSource.Source = Me.UtensilsData

        Me.UtensilsView = CType(Me.UtensilsViewSource.View, ListCollectionView)
    End Sub

  Private Sub btnAdd_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnAdd.Click

        Dim rec = CType(Me.UtensilsView.AddNew, Utensil)
        Me.UtensilsView.CommitNew()

        imagebox.Source = Nothing
        Dim FileDialog As New OpenFileDialog
        Try
            FileDialog.Title = "Select A File"
            FileDialog.InitialDirectory = ""
            FileDialog.Filter = "Image Files (*.gif,*.jpg,*.jpeg,*.bmp,*.png)|*.gif;*.jpg;*.jpeg;*.bmp;*.png"
            FileDialog.FilterIndex = 1
            FileDialog.ShowDialog()
            PicLocationTxtBx.Text = FileDialog.FileName()

            Dim bmp As New BitmapImage(New Uri(PicLocationTxtBx.Text.Trim))
            If bmp.Width < 484 And bmp.Height < 268 Then
                imagebox.Stretch = Stretch.None
                imagebox.VerticalAlignment = Windows.VerticalAlignment.Center
                imagebox.HorizontalAlignment = Windows.HorizontalAlignment.Center
                imagebox.Source = bmp
            Else
                imagebox.Stretch = Stretch.Uniform
                imagebox.VerticalAlignment = Windows.VerticalAlignment.Center
                imagebox.HorizontalAlignment = Windows.HorizontalAlignment.Center
                imagebox.Source = bmp
            End If
        Catch ex As Exception
            MsgBox("You didn't select any image file....")
        End Try

    End Sub

    Private Sub btnSave_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnSave.Click
        Try
            db.SaveChanges()
            MsgBox("Utensil Saved")
        Catch ex As Exception
            MsgBox(ex.ToString())
        End Try
    End Sub
End Class

Sorry for the length...appreciate any and all help.

Thanks again.


Adam





Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>