Click here to Skip to main content
Licence CPOL
First Posted 12 Jun 2008
Views 6,704
Downloads 29
Bookmarked 10 times

ErrorLog and File Handling

By | 12 Jun 2008 | Article
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--

    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)

About the Author

Indranil Paul

Web Developer

India India

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 12 Jun 2008
Article Copyright 2008 by Indranil Paul
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid