Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
VB
Private Sub video()
        If IsPostBack Then
            Dim path As String = Server.MapPath("~/UploadedVideos/")
            Dim fileOK As Boolean = False
            If FileUpload1.HasFile Then
                Dim fileExtension As String
                fileExtension = System.IO.Path. _
                GetExtension(FileUpload1.FileName).ToLower()
                Dim allowedExtensions As String() = _
                {".mov", ".wmv", ".avi", ".vob", ".mp4"}
                For i As Integer = 0 To allowedExtensions.Length - 1
                    If fileExtension = allowedExtensions(i) Then
                        fileOK = True
                    End If
                Next
                If fileOK Then
                    Dim Conn As New SqlConnection(ConfigurationManager.ConnectionStrings("projectConnectionString").ConnectionString)

                    Conn.Open()
                    Try
                        Dim FilePath = path & FileUpload1.FileName
                        'Here you can fire your insert statement in filepath value u can pass directly that value
                        Dim SQL As String = "INSERT INTO EmpData ( [Video]) VALUES (@VideoUrl)"
                        Dim cmd As New SqlCommand(SQL, Conn)
                        cmd.Parameters.Add("@VideoUrl", SqlDbType.NVarChar, 50, FilePath)
                        FileUpload1.PostedFile.SaveAs(path & _
                        FileUpload1.FileName)
                        lblError.Text = "File uploaded!"
                        cmd.ExecuteNonQuery()
                        Conn.Close()
                    Catch ex As Exception
                        lblError.Text = "File could not be uploaded." & ex.Message
                    End Try

                Else
                    lblError.Text = "Cannot accept files of this type."
                End If
            End If
        End If
    End Sub



this was my code for the uploading a video file to a specific folder in my webform application project but the problem is when i call that video() from the save button this error show in the browser :T
he connection was reset the connection to the server was reset while the page was loading.
this is the code for my save button where i call the video()


VB
Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim connection As SqlConnection = Nothing
        Try
            Dim img As FileUpload = CType(PicUpload, FileUpload)
            Dim imgByte As Byte() = Nothing
            If img.HasFile AndAlso Not img.PostedFile Is Nothing Then
                'To create a PostedFile
                Dim File As HttpPostedFile = PicUpload.PostedFile
                'Create byte Array with file len
                imgByte = New Byte(File.ContentLength - 1) {}
                'force the control to load data in array
                File.InputStream.Read(imgByte, 0, File.ContentLength)
            End If
            ' Insert the employee name and image into db
            Dim conn As String = ConfigurationManager.ConnectionStrings("projectConnectionString").ConnectionString
            connection = New SqlConnection(conn)

            connection.Open()
            Dim sql As String = "INSERT INTO EmpData (DateOfUpload, DescOfWork,WorkDone,ComOfEmp,Picture,EmpUsername)VALUES (@en1m,@en2m,@en3m,@en4m,@eimg,@en5m)SELECT @@IDENTITY"
            Dim cmd As SqlCommand = New SqlCommand(sql, connection)
            cmd.Parameters.AddWithValue("@en1m", Calendar1.SelectedDate)
            cmd.Parameters.AddWithValue("@en2m", TypeOfWork.Text.Trim())
            cmd.Parameters.AddWithValue("@en3m", WorkOfTheDay.Text.Trim())
            cmd.Parameters.AddWithValue("@en4m", ComOfEmp.Text.Trim())
            cmd.Parameters.AddWithValue("@eimg", imgByte)
            cmd.Parameters.AddWithValue("@en5m", Request.QueryString("id"))
            Dim id As Integer = Convert.ToInt32(cmd.ExecuteScalar())


        Catch ex As Exception
            MsgBox(ex.Message)

        Finally
            connection.Close()
        End Try
        Call video()
        Response.Redirect("Employee.aspx?id=" + TextBox1.Text, False)
    End Sub


plz if sm1 can help me out i will appreciate it thank you in advanced
Posted
Updated 4-Apr-12 23:29pm
v2

Find <httpruntime maxrequestlength="2097151" enable="true"> in web.config if you have.Otherwise Add in
<system.web>
.
.

as
C#
system.web>
   <!--
           Set compilation debug="true" to insert debugging
           symbols into the compiled page. Because this
           affects performance, set this value to true only
           during development.
       -->
   <compilation debug="true">
     <assemblies>
       <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
       <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
       <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
       <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
       <add assembly="System.Web.Extensions.Design, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
       <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
       <add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
       </assemblies>
     <buildProviders>
       <add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.Common, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
     </buildProviders>
   </compilation>
   <!--
           The <authentication> section enables configuration
           of the security authentication mode used by
           ASP.NET to identify an incoming user.
       -->
   <authentication mode="Windows" />
   <!--<httpRuntime executionTimeout="1900" maxRequestLength="2090000" />-->

   <httpRuntime maxRequestLength="2097151" enable="true" />
<customErrors mode="Off" defaultRedirect="GenericErrorPage.htm">
           <error statusCode="403" redirect="NoAccess.htm" />
           <error statusCode="404" redirect="underconstruction.htm" />
         <error statusCode="500" redirect="underconstruction.htm"/>
       </customErrors>

   <pages enableEventValidation="false">
     <controls>
       <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
       <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
     </controls>
   </pages>
   <httpHandlers>
     <remove path="*.asmx" verb="*" />
     <add path="*.asmx" verb="*" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false" />
     <add path="*_AppService.axd" verb="*" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false" />
     <add path="ScriptResource.axd" verb="GET,HEAD" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false" />
     <add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="false" />
   </httpHandlers>
   <httpModules>
     <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
     <add name="OnlineActiveUsers" type="OnlineActiveUsers.OnlineUsersModule" />
   </httpModules>
 </system.web>


in web.config
 
Share this answer
 
v2
Comments
kroz18 5-Apr-12 5:47am    
Error 25 Unrecognized configuration section authentication. C:\Users\shadow\Documents\Visual Studio 2010\WebSites\WebSite2\Web.config 43
uspatel 5-Apr-12 5:52am    
see updated answer
uspatel 5-Apr-12 5:53am    
<httpruntime maxrequestlength="2097151" enable="true"> line shoul be add in <system.web> section
kroz18 5-Apr-12 6:04am    
thank you sooo much it worked but i still have a slight of a problem as you can see i wanted to add the file path to my database table EmpData in the video field but when the save button pressed the file is uploaded to the file but the file path is not saved if you can help me in this i qill apreciate it and thank you again :)
uspatel 5-Apr-12 6:19am    
is there any error message?
VB
Private Sub video()
       Dim connection As SqlConnection = Nothing
       Dim img As FileUpload = CType(PicUpload, FileUpload)
       Dim imgByte As Byte() = Nothing
       If img.HasFile AndAlso Not img.PostedFile Is Nothing Then
           'To create a PostedFile
           Dim File As HttpPostedFile = PicUpload.PostedFile
           'Create byte Array with file len
           imgByte = New Byte(File.ContentLength - 1) {}
           'force the control to load data in array
           File.InputStream.Read(imgByte, 0, File.ContentLength)
       End If
       If IsPostBack Then
           'declare the file path to store uploads
           Dim path As String = Server.MapPath("~/UploadedVideos/")


           Dim fileOK As Boolean = False

           'check the file to make sure it is of the required type
           Dim fileExtension As String
           fileExtension = System.IO.Path. _
               GetExtension(FileUpload1.FileName).ToLower()
           'declare the allowed extentions
           Dim allowedExtensions As String() = _
               {".mov", ".wmv", ".avi", ".mp4", ".m4v", ".flv"}
           'loop to check the extention type
           For i As Integer = 0 To allowedExtensions.Length - 1
               If fileExtension = allowedExtensions(i) Then

                   fileOK = True

               End If



           Next


           '******************* Write Video Immage And Details into the Database *******************************

           Dim conn As String = ConfigurationManager.ConnectionStrings("projectConnectionString").ConnectionString


           Try
               FileUpload1.FileName.DefaultIfEmpty()
               Dim FilePath = path & FileUpload1.FileName

               connection = New SqlConnection(conn)

               connection.Open()

               Dim sql As String = "INSERT INTO EmpData (DateOfUpload, DescOfWork,WorkDone,ComOfEmp,Picture,EmpUsername,Video)VALUES (@en1m,@en2m,@en3m,@en4m,@eimg,@en5m,@VideoUrl)SELECT @@IDENTITY"
               Dim cmd As SqlCommand = New SqlCommand(sql, connection)

               cmd.Parameters.AddWithValue("@en1m", Calendar1.SelectedDate)
               cmd.Parameters.AddWithValue("@en2m", TypeOfWork.Text.Trim())
               cmd.Parameters.AddWithValue("@en3m", WorkOfTheDay.Text.Trim())
               cmd.Parameters.AddWithValue("@en4m", ComOfEmp.Text.Trim())
               cmd.Parameters.AddWithValue("@eimg", imgByte)
               cmd.Parameters.AddWithValue("@en5m", Request.QueryString("id"))
               If fileOK Then
                   cmd.Parameters.AddWithValue("@VideoUrl", FilePath)
               Else
                   cmd.Parameters.AddWithValue("@VideoUrl", DBNull.Value)
               End If

               Dim id As Integer = Convert.ToInt32(cmd.ExecuteScalar())
               FileUpload1.PostedFile.SaveAs(path & _
                  FileUpload1.FileName)


           Catch ex As Exception
               MsgBox(ex.Message)

           End Try

       End If





   End Sub
 
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