withSolutions   Script Sample Showcase
 
Skip Navigation Links.   This script will allow for a file to be uploaded into a SQL database as part of a customers records.

        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)

  

                If FileUpload1.HasFile Then

  

                        Dim thisfileName As String = FileUpload1.FileName

                        Dim thisfullName As String = FileUpload1.PostedFile.FileName 'has path w/ filename

                        Dim thisfilePath As String = Mid(thisfullName, 1, (InStrRev(thisfullName, "\") - 1))

                        Dim thisfileExt As String = System.IO.Path.GetExtension(FileUpload1.FileName)

                        Dim thiscontentsize As String = FileUpload1.PostedFile.ContentLength 'for message text

                        Dim thiscontenttype As String = FileUpload1.PostedFile.ContentType

                        Dim thisfileDate As Date = Now.Date

  

                        If (thisfileExt = ".txt" Or thisfileExt = ".TXT" Or thisfileExt = ".pdf" Or thisfileExt = ".PDF") Then

                                Try

                                        Dim iLength As Integer = CType(FileUpload1.PostedFile.InputStream.Length, Integer)

                                        Dim bytContent As Byte()

                                        ReDim bytContent(iLength) 'byte array, set to file size

  

                                       FileUpload1.PostedFile.InputStream.Read(bytContent, 0, iLength)

  

                                        Dim objLoadConnection As New SqlConnection

                                       objLoadConnection.ConnectionString = ""

                                        objLoadConnection.Open()

  

                                        Dim objLoadCommand As New SqlCommand

                                        objLoadCommand.CommandType = CommandType.StoredProcedure

                                        objLoadCommand.CommandText = "UploadDocument"

                                        objLoadCommand.CommandTimeout = 480

                                        objLoadCommand.Connection = objLoadConnection

                                        Dim pCustID As SqlParameter = objLoadCommand.Parameters.Add("@custid", SqlDbType.Int)

                                        Dim pOMSID As SqlParameter = objLoadCommand.Parameters.Add("@omsid", SqlDbType.Int)

                                        Dim pDocumentDate As SqlParameter = objLoadCommand.Parameters.Add("@documentdate", SqlDbType.DateTime)

                                        Dim pOriginalFilePath As SqlParameter = objLoadCommand.Parameters.Add("@originalfilepath", SqlDbType.VarChar, 100)

                                        Dim pOriginalFileName As SqlParameter = objLoadCommand.Parameters.Add("@originalfilename", SqlDbType.VarChar, 100)

                                        Dim pDocumentType As SqlParameter = objLoadCommand.Parameters.Add("@documenttype", SqlDbType.VarChar, 100)

                                        Dim pDocumentData As SqlParameter = objLoadCommand.Parameters.Add("@documentdata", SqlDbType.Image)

                                        Dim pContentType As SqlParameter = objLoadCommand.Parameters.Add("@contenttype", SqlDbType.VarChar, 100)

  

                                        ' SET THE SQL VARIABLES

                                        pCustID.Value = thiscustid

                                        pOMSID.Value = thisomsid

                                        'pDocumentDate.Value = thisfileDate

                                        pDocumentDate.Value = Now

                                        pOriginalFilePath.Value = thisfilePath

                                        pOriginalFileName.Value = thisfileName

                                        pDocumentType.Value = "Order Paperwork"

                                        pDocumentData.Value = bytContent 'docdata

                                        pContentType.Value = thiscontenttype

                                        objLoadCommand.ExecuteNonQuery()

  

                                        'report to the user

                                        Label1.Text = "<b>The File Name: " & FileUpload1.PostedFile.FileName & "</b><br>" & _

                                                                    "CustID: " & thiscustid & "<br>" & _

                                                                    "OMSID: " & thisomsid & "<br>" & _

                                                                    "File Size: " & thiscontentsize & " kb<br>" & _

                                                                    "File Date: " & thisfileDate & "<br>" & _

                                                                    "Content type: " & thiscontenttype & "<br>" & _

                                                                    "<b>Has been added to Order" & thisomsid & "</b>"

                                        'close

                                        objLoadCommand = Nothing

                                        objLoadConnection.Close()

                                        objLoadConnection = Nothing

  

                                Catch ex As Exception

                                        Label1.Text = "ERROR: " & ex.Message.ToString()

                                End Try

                        Else

                                Label1.Text = "Only .txt or .pdf files allowed!"

                        End If 'thisfileExt

                Else

                        Label1.Text = "You have not specified a file."

                End If 'hasfile

       End Sub