Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Attach file using vb.net

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

789 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