Click here to Skip to main content
16,014,591 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello All,

Please give me code for file upload in asp.net using vb code into database and display on to a website as a hyperlink.if it is delete the file should be deleted from database.

please give me step wise instruction and code for this.

EDIT - Moved from answer

Hello All,

Please tell me where i went wrong it giving the following error
Access to the path 'C:\Documents and Settings\ananth\My Documents\Visual Studio 2005\WebSites\text\FileUpload' is denied.

Below is the code iam using
VB
Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.IO
Imports System.Data
Imports System.Configuration
Imports System.Security.Principal
Imports System.Web.Security
Partial Class FileUpload
    Inherits System.Web.UI.Page
 
    Dim myConnection As SqlConnection
    Dim myCommand As SqlCommand
    Dim rdr As SqlDataReader

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim ra As Integer
        If Not (myFile.PostedFile Is Nothing) Then
 
            Dim intFileNameLength As Integer
            Dim strFileNamePath As String
            Dim strFileNameOnly As String
 
            'Logic to find the FileName (excluding the path)
            strFileNamePath = myFile.PostedFile.FileName
            intFileNameLength = InStr(1, StrReverse(strFileNamePath), "\")
            strFileNameOnly = Mid(strFileNamePath, (Len(strFileNamePath) - intFileNameLength) + 2)
 
            myFile.PostedFile.SaveAs("C:\Documents and Settings\ananth\My Documents\Visual Studio 2005\WebSites\text\FileUpload" & strFileNameOnly)
            lblFileContentType.Text = "File Upload Success:-" & CStr(myFile.PostedFile.ContentLength) & " bytes"
            'lblFileContentType.Text = "Content type: " & MyFile.PostedFile.ContentType
            'lblFileSize.Text = "File size: " & CStr(myFile.PostedFile.ContentLength) & " bytes"

            myConnection = New SqlConnection("server=172.18.0.6;uid=sa;pwd=logmein;database=ananth")
            'you need to provide password for sql server
            myConnection.Open()
            myCommand = New SqlCommand("Insert into fileupload(filename,link) values('" + TextBox1.Text + "','" + FileUpload1.FileName + "')", myConnection)
            ra = myCommand.ExecuteNonQuery()
            'Since no value is returned we use ExecuteNonQuery
            Response.Write("Records Inserted" & ra)
            myConnection.Close()
        End If
 
    End Sub
End Class
Posted
Updated 14-Jun-11 1:22am
v2

Please consider buying a programming book, reading it, learning to use google, and in general trying to become a programmer instead of asking other people to do your work for free without even trying. There is no way a forum reply can give you all the code you need, and there's tons of articles on this site and elsewhere that would show you exactly what to do. If you got stuck still, because it's clear you don't know much about programming, then I'm sure we'd love to answer when you posted specific questions here, with code snippets and explanations of what you've tried and where you are stuck.
 
Share this answer
 
Start here[^].
 
Share this answer
 
OP wrote:
Access to the path 'C:\Documents and Settings\ananth\My Documents\Visual Studio 2005\WebSites\text\FileUpload' is denied

The error says itself, You have no permission to that particular folder. So set the permission for that folder.
ASP.NET User Does Not Have Permissions to Your Application Folder[^]

My suggestion is don't use folders in C:\ Drive, use any other drives.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900