Solved

Attach file using vb.net

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 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