Results 1 to 8 of 8

Thread: [2005] please help convert

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    258

    [2005] please help convert

    i got this sample form from MSDN, its a simpe windows application that allows the user to upload and retrieve images from a datasource within the program. The problem is i dont know which should be changed so that it can be used for my program wherein i am accessing a database from a remote connection. Please hepl thanks

    PHP Code:
    #Region "Using Statements"

    Imports System
    Imports System
    .Collections.Generic
    Imports System
    .ComponentModel
    Imports System
    .Data
    Imports System
    .Drawing
    Imports System
    .Text
    Imports System
    .Windows.Forms
    Imports System
    .IO
    Imports StoringRetrievingImage
    .MyImagesDataSetTableAdapters

    #End Region

    Public Class StoringRetrievingImagesForm

        Dim myPhotoDataSet 
    As DataSet
        Dim currentPage 
    As Integer 0

        
    ''' <summary>
        ''' 
    This event opens an OpenFileDialog so you can browse for 
        
    ''' the image you want to upload. The filename textbox is then
        ''' 
    filled for when the image is uploaded.
        
    ''' </summary>
        ''' 
    <param name="sender"></param>
        
    ''' <param name="e"></param>
        Private Sub browseImageButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles browseImageButton.Click

            browseImageOpenFileDialog.Title = "Select an Image to Import"
            browseImageOpenFileDialog.Multiselect = False
            browseImageOpenFileDialog.Filter = "JPEG Compressed Image(*.jpg)|*.jpg|Graphic Interchange Format (*.gif)|*.gif"
            browseImageOpenFileDialog.FilterIndex = 2
            browseImageOpenFileDialog.FileName = ""
            browseImageOpenFileDialog.ShowDialog()
            imageNameTextBox.Text = browseImageOpenFileDialog.FileName

        End Sub

        ''' 
    <summary>
        
    ''' This event prepares all the fields that we want to insert into the database.
        ''' 
    The photograph name and CategoryID are retrieved from their respective controls.
        
    ''' The image data is first selected from the image file name text box.  The file is then opened
        ''' 
    and saved into a FileStream object.  This stream is then saved into a byte array which
        
    ''' will be stored in the image data type.
        ''' 
    </summary>
        
    ''' <param name="sender"></param>
        ''' 
    <param name="e"></param>
        Private 
    Sub insertImageButton_Click(ByVal sender As System.ObjectByVal e As System.EventArgsHandles insertImageButton.Click
            
    Try

                
    Dim myMessage As String
                Dim myCategoryID 
    As Integer 0
                
    If imageNameTextBox.Text "" Then
                    MessageBox
    .Show("Please Enter a File Name.""Alert")
                Else

                    
    Dim myAdapter As CategoriesTableAdapter = New CategoriesTableAdapter()

                    
    browseImageOpenFileDialog.FileName imageNameTextBox.Text
                    Dim myFile 
    As String
                    
    For Each myFile In browseImageOpenFileDialog.FileNames

                        
    '' Create a new stream to load this photo into
                        Dim myStream 
    As FileStream = New FileStream(myFileFileMode.OpenFileAccess.Read)
                        
    '' Create a buffer to hold the stream of bytes
                        Dim myImageBuffer
    (myStream.Length) As Byte


                        
    '' Read the bytes from this stream and put it into the image buffer
                        myStream
    .Read(myImageBuffer0Convert.ToInt32(myStream.Length))
                        
    '' Close the stream
                        myStream
    .Close()

                        
    '' get the photograph name
                        Dim myPhotographName 
    As String photographNameTextBoxTextBox.Text

                        
    '' get the CategoryID
                        myCategoryID 
    Convert.ToInt32(categoriesComboBox.SelectedValue)

                        Try

                            
    '' Call the insertNewImage method passing the property parameters
                            myMessage 
    myAdapter.insertNewImage(myCategoryIDmyPhotographNamemyImageBuffer)
                            
    MessageBox.Show(myMessage"Information")

                        Catch 
    ex As Exception

                            MessageBox
    .Show("There was an error inserting this image. Please try again.""Alert")
                        
    End Try


                        
    myImageBuffer Nothing
                    Next

                    ResetDisplay
    ()
                
    End If
            Catch 
    ex As Exception
                MessageBox
    .Show("There was an error inserting this image.  Please try again.""Alert")
            
    End Try
        
    End Sub

      
        
    Private Sub StoringRetrievingImagesForm_Load(ByVal sender As System.ObjectByVal e As System.EventArgsHandles MyBase.Load

            Dim myAdapter 
    As CategoriesTableAdapter = New CategoriesTableAdapter()
            
    Dim myDataSet As DataSet
            
    Try

            
    myDataSet myAdapter.getCategories()
                
    categoriesComboBox.DataSource myDataSet.Tables(0)
                
    categoriesComboBox.DisplayMember "CategoryName"
                
    categoriesComboBox.ValueMember "CategoryID"

                
    nextLinkLabel.Visible False
                previousLinkLabel
    .Visible False
            
    Catch ex As Exception
                MessageBox
    .Show("There was an error starting this application. Please restart.""Alert")
                
    getImagesButton.Enabled False
                browseImageButton
    .Enabled False
                categoriesComboBox
    .Enabled False
                imageNameTextBox
    .Enabled False
                photographNameTextBoxTextBox
    .Enabled False
                insertImageButton
    .Enabled False
                nextLinkLabel
    .Enabled False
                previousLinkLabel
    .Enabled False

            End 
    Try

        
    End Sub

     

        
    Private Sub getImagesButton_Click(ByVal sender As System.ObjectByVal e As System.EventArgsHandles getImagesButton.Click
            
    Try
                
    ResetDisplay()
                
    ShowImage()
            Catch 
    ex As Exception
                MessageBox
    .Show("There was an error displaying the images.  Please try again.""Alert")
            
    End Try
        
    End Sub

      
        
    Private Sub ShowImage()

            Try

        
                
    Dim myAdapter As CategoriesTableAdapter = New CategoriesTableAdapter()

                
                
    Dim myCategoryID As Integer Convert.ToInt32(categoriesComboBox.SelectedValue)

             
                
    myPhotoDataSet myAdapter.getImages(myCategoryID)

              
                
    SetNextLabel()

                If 
    myPhotoDataSet.Tables(0).Rows.Count 0 Then

                  
                    photographNameLabel
    .Text myPhotoDataSet.Tables(0).Rows(currentPage).Item("Name").ToString()

                 
                    
    Dim myByteArray() As Byte myPhotoDataSet.Tables(0).Rows(currentPage).Item("Photograph")

                    If 
    myByteArray.Length 0 Then

                      
                        Dim myStream 
    As MemoryStream = New MemoryStream(myByteArrayTrue)
                        
    myStream.Write(myByteArray0myByteArray.Length)

                    
                        
    Dim FinalImage As Bitmap = New Bitmap(myStream)

                     
    PictureBox control
                        
    If FinalImage.Width 217 And FinalImage.Height 151 Then

                            Dim AlteredImage 
    As Bitmap = New Bitmap(FinalImage, New Size(217151))
                            
    photographPictureBox.Image AlteredImage

                        
    Else
                            
    photographPictureBox.Image FinalImage
                        End 
    If
                        
    '' Close the stream
                        myStream
    .Close()
                    
    End If


                Else
                    
    MessageBox.Show("There are no photographs in this category yet!""Alert")
                
    End If
            Catch 
    ex As Exception
                MessageBox
    .Show("There was an error displaying this image.""Alert")
            
    End Try

        
    End Sub


        
    Private Sub nextLinkLabel_LinkClicked(ByVal sender As System.ObjectByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgsHandles nextLinkLabel.LinkClicked

            currentPage 
    currentPage 1
            ShowImage
    ()
            
    previousLinkLabel.Visible True
        End Sub


        
    Private Sub SetNextLabel()

            If 
    myPhotoDataSet.Tables(0).Rows.Count 0 Then
                
    If myPhotoDataSet.Tables(0).Rows.Count 1 Then

                    nextLinkLabel
    .Visible True
                
    Else
                    
    nextLinkLabel.Visible False
                End 
    If

                If 
    myPhotoDataSet.Tables(0).Rows.Count = (currentPage 1Then
                    nextLinkLabel
    .Visible False
                End 
    If


            
    End If
        
    End Sub

       
        
    Private Sub previousLinkLabel_LinkClicked(ByVal sender As System.ObjectByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgsHandles previousLinkLabel.LinkClicked

            currentPage 
    currentPage 1
            ShowImage
    ()
            If 
    currentPage 0 Then
                previousLinkLabel
    .Visible False
            End 
    If
            If 
    Not (myPhotoDataSet.Tables(0).Rows.Count = (currentPage 1)) Then
                nextLinkLabel
    .Visible True
            End 
    If
        
    End Sub

        
    ''' <summary>
        ''' 
    This method reinitiates the controls on the form.
        
    ''' </summary>
        ''' 
    <remarks></remarks>
        Private 
    Sub ResetDisplay()

            
    photographPictureBox.Image Nothing
            photographNameLabel
    .Text "Photograph Name"
            
    currentPage 0
            previousLinkLabel
    .Visible False
            nextLinkLabel
    .Visible False
            photographNameTextBoxTextBox
    .Text ""
            
    imageNameTextBox.Text ""

        
    End Sub

    #End Region

        
    Private Sub categoriesComboBox_SelectedIndexChanged(ByVal sender As System.ObjectByVal e As System.EventArgsHandles categoriesComboBox.SelectedIndexChanged

        End Sub
    End 
    Class 

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    258

    Re: [2005] please help convert

    the database im using is MS SQL 2005.

    btw here is the dataadapter of the application

    Code:
    #Region "Using Statements"
    
    Imports System
    Imports System.Collections.Generic
    Imports System.Text
    Imports System.Data.SqlClient
    Imports System.IO
    Imports System.Data
    
    #End Region
    
    Namespace MyImagesDataSetTableAdapters
    
    
        Partial Public Class CategoriesTableAdapter
    
    
            ''' <summary>
            ''' The insertNewImage method takes all the information about the image and stores it
            ''' in the database.  This method accesses a stored procedure to insert the data and returns
            ''' the success statement or error message.
            ''' </summary>
            ''' <param name="CategoryID"></param>
            ''' <param name="photographName"></param>
            ''' <param name="myBuffer"></param>
            ''' <returns></returns>
            Public Function insertNewImage(ByVal CategoryID As Integer, ByVal photographName As String, ByRef myBuffer() As Byte) As String
    
                Dim message As String = ""
                Dim myConnection As SqlConnection
                myConnection = Connection()
    
                Try
    
                    myConnection.Open()
    
                    '' Create a stored procedure command
                    Dim myCommand As SqlCommand = New SqlCommand("sp_InsertPhoto", myConnection)
                    myCommand.CommandType = CommandType.StoredProcedure
    
                    '' Add the Name parameter and set the photographName as the value
                    myCommand.Parameters.Add("@name", SqlDbType.VarChar).Value = photographName
                    '' Add the image parameter and set myBuffer as the value.
                    myCommand.Parameters.Add("@image", SqlDbType.Image).Value = myBuffer
                    '' Add the CategoryID parameter and set the CategoryID as the value
                    myCommand.Parameters.Add("@categoryid", SqlDbType.Int).Value = CategoryID
    
                    '' Execute the insert
                    myCommand.ExecuteNonQuery()
    
                    '' Close the Connection
                    myConnection.Close()
    
                    '' Assign the success message
                    message = "Inserted Successfully!"
    
                Catch ex As Exception
    
                    '' Assign the error message
                    message = ex.Message.ToString()
                End Try
    
                Return message
            End Function
    
            ''' <summary>
            ''' The getCategories method is a general method that returns a DataSet with Category
            ''' information in it.
            ''' </summary>
            ''' <returns></returns>
            Public Function getCategories() As DataSet
    
                Dim myConnection As SqlConnection
                Dim myCommand As SqlCommand
                Dim myQuery As String = ""
                Dim myDataSet As DataSet = New DataSet()
                Dim myAdapter As SqlDataAdapter
                myConnection = Connection()
    
                Try
    
                    myConnection.Open()
                    myQuery = "SELECT * FROM Categories ORDER BY CategoryName"
                    myCommand = New SqlCommand()
                    myCommand.CommandText = myQuery
                    myCommand.Connection = myConnection
                    myAdapter = New SqlDataAdapter(myCommand)
                    myAdapter.Fill(myDataSet)
                    myConnection.Close()
    
                Catch ex As Exception
    
                    Throw ex
                End Try
    
                Return myDataSet
            End Function
    
            ''' <summary>
            ''' The getImages method accesses the database and return Image information
            ''' based on the CategoryID that is passed in.
            ''' </summary>
            ''' <param name="CategoryID"></param>
            ''' <returns></returns>
            Public Function getImages(ByVal CategoryID As Integer) As DataSet
    
                Dim myConnection As SqlConnection
                Dim myCommand As SqlCommand
                Dim myQuery As String = ""
                Dim myDataSet As DataSet = New DataSet()
                Dim myAdapter As SqlDataAdapter
                myConnection = Connection()
    
                Try
    
                    myConnection.Open()
                    myQuery = "SELECT * FROM Photographs WHERE CategoryID = " & CategoryID
                    myCommand = New SqlCommand()
                    myCommand.CommandText = myQuery
                    myCommand.Connection = myConnection
                    myAdapter = New SqlDataAdapter(myCommand)
                    myAdapter.Fill(myDataSet)
    
                Catch ex As Exception
    
                    Throw ex
                End Try
    
                Return myDataSet
            End Function
    
    
        End Class
    
    End Namespace
    Last edited by aerialz666; Jun 21st, 2007 at 12:10 PM.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: [2005] please help convert

    The code posted in the first post allows the user to choose a location to get files from. It sounds like you might already have a location (the database). Is that right?

    If that's the case, then the whole chunk of code for the browse button can be dispensed with. All the changes will be in the InsertImage box, but what those changes are depends on how the data is stored.

    Perhaps you could describe how the database is supposed to operate. Will you be querying a bunch of images from the database that you then want to deal with? Will a query get only one image, or many? etc.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    258

    Re: [2005] please help convert

    well its for a human resource system's employee profile so .. each employee displayed must display have an upload picture option and display it with the other info. so there.. thank you

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: [2005] please help convert

    So they'll have images in the database? Or will it be that the image will be in a file, and the database will retain the name of the file?
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    258

    Re: [2005] please help convert

    sir im just about to make it and im not sure also which is the right one to choose, if it should be inside the database or the database will retain the filename.. please suggest thanks

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: [2005] please help convert

    Actually, I have wondered about this very question. I would suggest that you go to the database section and post it as a new thread. I see advantages to doing it both ways, and would be curious to hear what others thought about it. In this case, the changes to the code would be less if the file name was saved in the DB rather than the picture, but I think it would be best to get advice on that specific question, as I don't have a good answer for it.
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2007
    Posts
    258

    Re: [2005] please help convert

    sir assuming that the file will be saved in the database, what changes should be done? I need to learn one of them thankyou!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width