Solved

Attach file using vb.net

Posted on 2004-09-27
2
317 Views
Last Modified: 2010-04-23
I've created an application using vb.net and i'm trying to create a method when a button si clicked, a small window is opened to allow attachment of image type file to the application.

is there any online resource that i can see/read or can anyone provide me a simple source code?
0
Comment
Question by:TPoly
2 Comments
 
LVL 2

Assisted Solution

by:fulp02
fulp02 earned 200 total points
ID: 12158828
I would Use The FileDialog class to select yout File
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsfiledialogclasstopic.asp

And what do you want to attach the image to ? to be displayed ? Mailed ?
0
 
LVL 3

Accepted Solution

by:
RacinRan earned 300 total points
ID: 12159867
Here is code that loads a pic into a SQL Server database ... you didn't say what you wanted to attach it to but this should point you in the right direction:

    Private Sub btnLoadSQL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadSQL.Click

        'Objects for SQL
        Dim oConn As New SqlConnection(gstrconnect), oCmd As New SqlCommand("spInsertImage")
        Dim oParam As New SqlParameter()
       'Gets the file path exculding extension from the FileDialog box mentioned by fulp02
        Dim strType As String = mstrFilePath.Substring(mstrFilePath.IndexOf("."))
        Dim intRows As Integer

        'Objects for File
        Dim fs As New FileStream(mstrFilePath, FileMode.Open)  'Opens file
        Dim br As New BinaryReader(fs) 'Reads the stream


        'Parameters for stream
        Dim length As Integer = CInt(fs.Length) 'Length is size of file in bytes
        Dim content(length) As Byte  'Creates a byte array of same size as file

        'Read stream into buffer which is the content variable
        br.Read(content, 0, length) 'Reads the file into content starting at 0 and ending at length

        'Send byte array (content) to sql
        With oParam
            .ParameterName = "@Image"
            .SqlDbType = SqlDbType.Image
            .Direction = ParameterDirection.Input
            .Value = content
        End With

        Try
            oConn.Open()

            With oCmd
                .CommandType = CommandType.StoredProcedure
                .Parameters.Add(oParam)
                .Parameters.Add("@Type", strType)
                .Connection = oConn
                intRows = .ExecuteNonQuery
            End With

        Catch ex As Exception
            MessageBox.Show(Err.Number & ":  " & ex.Message, "Error")
            Exit Sub
        Finally
            If oConn.State <> ConnectionState.Closed Then
                oConn.Close()
            End If
        End Try

    End Sub

Here is code for the dialog box ... it puts the image into a picture box on a form so you can see it before uplkoading it:

    Private Sub btnLoadVB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadVB.Click

        Try
            'Allow user to select an image
            OpenFile.ShowDialog()
            mstrFilePath = OpenFile.FileName

            'load image into picture box from file
            pb1.Image = pb1.Image.FromFile(mstrFilePath)

        Catch ex As Exception
            MessageBox.Show(Err.Number & ":  " & ex.Message, "Error")
            Exit Sub
        End Try

        pb1.Image.Dispose() 'Releases control of the file for other processes
        btnLoadSQL.Enabled = True


    End Sub


Racin
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question