Solved

Attach file using vb.net

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 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

14 Experts available now in Live!

Get 1:1 Help Now