Solved

Attach file using vb.net

Posted on 2004-09-27
2
314 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now