Click here to Skip to main content
15,886,518 members
Articles / Programming Languages / Visual Basic
Article

ErrorLog and File Handling

Rate me:
Please Sign up or sign in to vote.
1.38/5 (6 votes)
12 Jun 2008CPOL 15.5K   79   9  
Upload and download a file and execute Error log in a particular file

Introduction

In this article files have been directly pushed into the database as "images." At the same time there is also provision for downloading. To keep the file in a database is much more secure than to keeping it in a local drive. Also, logging the error in a particular file and writing it in a text file has been done.

Using the Code

SQLExpress 2005 has been used for storing the file data,size and content type. For error handling (i.e Error Log) a class file has been used.

--For Uploading and Downloading the file--

VB
    Protected Sub btnAttach_Click(ByVal sender As Object,
            ByVal e As System.EventArgs) Handles btnAttach.Click
        Dim iLength As Integer = CType(File1.PostedFile.InputStream.Length, Integer)
        If iLength = 0 Then Exit Sub 'not a valid file 
        Dim sContentType As String = File1.PostedFile.ContentType
        Dim sFileName As String, i As Integer
        Dim bytContent As Byte()
        ReDim bytContent(iLength) 'byte array, set to file size 

        'strip the path off the filename 
        i = InStrRev(File1.PostedFile.FileName.Trim, "\")
        If i = 0 Then
            sFileName = File1.PostedFile.FileName.Trim
        Else
            sFileName = Right(File1.PostedFile.FileName.Trim,
            Len(File1.PostedFile.FileName.Trim) - i)
        End If

        Try
            File1.PostedFile.InputStream.Read(bytContent, 0, iLength)
            
            Dim dbConn As SqlConnection = New SqlConnection(
                ConfigurationManager.ConnectionStrings("ConnectionString").ToString())
            Dim str As String
            str = "select * from tblAttachments where attachmentID=1"


            Dim cmd As SqlCommand = New SqlCommand(str, dbConn)
            dbConn.Open()
            Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
            Dim ds As DataSet = New DataSet()
            Dim cb As New SqlCommandBuilder(da)

            da.Fill(ds, "tblAttachments")
            Dim myRow As DataRow
            myRow = ds.Tables("tblAttachments").NewRow()

            myRow("FileName") = sFileName
            myRow("FileSize") = iLength.ToString()
            myRow("FileData") = bytContent
            myRow("ContentType") = sContentType

            ds.Tables("tblAttachments").Rows.Add(myRow)
            da.Update(ds, "tblAttachments")

            dbConn.Close()
            Dim int1 As Integer = 10
            Dim int2 As Integer = 0
            Dim intResult As Integer

           
            intResult = int1 / int2

        Catch ex As Exception
            Response.Write(ex.Message)
            Dim el As New ErrorLogger
            el.WriteToErrorLog(ex.Message, ex.StackTrace, "Error")

            MsgBox("File Upload Successfully and Error logged in C:\Errors.")
            'Handle your database error here 
            '  dbnConn.Close()
        End Try
    End Sub
    

 Protected Sub btnDownload_Click(ByVal sender As Object,
     ByVal e As System.EventArgs) Handles btnDownload.Click


        
        Try
            
            Dim dbConn As SqlConnection = New SqlConnection(
                ConfigurationManager.ConnectionStrings("ConnectionString").ToString())
            Dim dr As System.Data.SqlClient.SqlDataReader
            Dim str As String
            str = "select * from tblAttachments where attachmentID=19"
            Dim cmd As SqlCommand = New SqlCommand(str, dbConn)
            dbConn.Open()
            dr = cmd.ExecuteReader
            If dr.Read Then
                Response.Clear()
                Response.Buffer = True
                Response.ContentType = dr("ContentType").ToString
                Response.AddHeader("Content-Disposition", "attachment;filename=" +
                    dr("FileName").ToString())
                Response.Buffer = True
                Response.BinaryWrite(CType(dr("FileData"), Byte()))

            Else
                Response.Write("File Not Found.")
            End If

            dbConn.Close()


        Catch ex As Exception
            'Handle your database error here 
            '  dbnConn.Close()
        End Try
    End Sub
End Class

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
I am a Senior S/W Developer associated with a S/W MNC for the last 3 yrs.I am working in C# in .netframework and using Infragistics,AJAX and SQLServer2000.

Comments and Discussions

 
-- There are no messages in this forum --