These forums are read-only and considered to be an archive. Please use the new Community for future interaction and posts.

Uploaded event triggers itself randomly after upload

Hello, everyone. I have the following problem: I'm using the "uploaded" event to store uploaded files data into a database table. The files are uploaded and the event is triggered fine, but the problem is that, with no apparent reason, the event is triggered randomly from 2 to 6 times once the file is uploaded. I insist that the problem is not related to a loop within the event trap but the event triggering itself several times. Do you have any idea what could be happening? Thanks in advance for your help.



 Protected Sub FileVistaControl_Uploaded(ByVal sender As Object, ByVal e As GleamTech.Web.Controls.FileVistaUploadedEventArgs) Handles FileVistaControl.Uploaded
       

            Dim empresa As New HiddenField
            empresa = CType(menu1.FindControl("empresa"), HiddenField)
            Dim idempresa As String = empresa.Value.ToString
            Dim cliente As New HiddenField
            cliente = CType(menu1.FindControl("cliente"), HiddenField)
            Dim idcliente As String = cliente.Value.ToString

            Dim usuario As New HiddenField
            usuario = CType(menu1.FindControl("usuario"), HiddenField)
            Dim nombreusuario As String = usuario.Value.ToString

            
            Dim nombrearchivo As String
            Dim uploades As GleamTech.Web.FileTransfer.UploadedFile

            For Each uploades In e.UploadProgress.UploadedFiles
                nombrearchivo = uploades.Name
                Dim ruta As String = e.RelativePath
                If e.RelativePath <> "" Then nombrearchivo = e.RelativePath + "/" + nombrearchivo

                Dim stringAdd As String = "Insert into logs_archivos (archivo,idempresa,idcliente,tipo,usuario) values('" + nombrearchivo + "','" + idempresa + "','" + idcliente + "','nuevo','" + nombreusuario + "')"

                Dim objcommand As New SqlClient.SqlCommand(stringAdd, New  _
                SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("fileonlineConnectionString").ToString))
                objcommand.Connection.Open()
                objcommand.ExecuteNonQuery()
                objcommand.Connection.Close()

                objcommand.Dispose()
                objcommand = Nothing
                
            Next


    End Sub
miguel yáñez 1/24/2009 11:34 AM
Hi Miguel,
Note that the events of the control are static. So you need to make sure you register the events only when you first load the page. Reloading the page should not cause registering the same events again.

So in your Page_Load method where you register the events, you need to check the existence of events with a static variable:

        Private Shared areHandlersAdded As Boolean = False

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

            If Not areHandlersAdded Then
                
AddHandler FileVistaControl.Uploaded, AddressOf FileVistaControl_Uploaded
                
areHandlersAdded = True
            
End If

 End Sub
Cem Alacayir 1/27/2009 9:19 AM
After trying the suggestions made by Cem in order to have control over the event self-triggering, I have found a much worrying side problem. Beside the fact that the proposed solution has no effect whatsoever on what we try to solve, a state variables are used again during the rerun of the event code and use previous values (which is the oddest part of the story). We also try to debug the event code and we find that part of the code is run without stopping on the debug break. There has to be some active code within the control that keeps on executing itself. We find no way to debug it and, a more concerning issue, the value changes over the state variables.

 

Since there is no manual and no additional information on the control, I find myself lost on this matter.

 

Your help is greatly appreciated.
miguel yáñez 1/28/2009 9:09 AM
Miguel,

Would you mind to share VB code you are using to load the control into your page.

I am using C# and have converted to VB but was unable to make it work.

It would be truky appreciated.

Thank you
Mauro 2/14/2009 2:48 AM
Miguel,
I guess you are adding the control via tagPrefix. The markup code is compiled by ASP.NET separately on first access, this should be the cause of your problem.
I think if you add the control programmatically, then there won't be any problems.
However if you use an Application level variable rather than a static variable as I suggested earlier, it will work too for your case, see below:

If Application("Upload_Handler_Added") Is Nothing Then 
    AddHandler FileVistaControl.Uploaded, AddressOf FileVistaControl_Uploaded 
    Application("Upload_Handler_Added") = True 
End If 


Mauro, 
I recommend you to use an online C# to VB convertor, here is a good one. You can convert the example C# codes easily, it works pretty well. 

See below for an example VB code for basic loading of the control: 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) 
Dim userName As String = "some user" 
Dim userFolderName As String = "Folder of " & userName 
Dim userFolderPath As String = "c:\user folders\" & userName 

Dim fileVistaControl As FileVistaControl = DirectCast(LoadControl("~/FileVistaControl/filevista.ascx"), FileVistaControl) 

Dim rootFolder As FileVistaRootFolder 

rootFolder = New FileVistaRootFolder(userFolderName, userFolderPath) 
rootFolder.Permissions = FileVistaPermissions.Traverse Or FileVistaPermissions.List Or FileVistaPermissions.Download 
fileVistaControl.RootFolders.Add(rootFolder) 

PlaceHolder1.Controls.Add(fileVistaControl) 
End Sub 
Cem Alacayir 3/10/2009 5:22 AM
If I use the code above I get the message "Name PlaceHolder1 is not declared".

If I declare "Dim PlaceHolder1 As PlaceHolder" I get the following warning: "Variable 'PlaceHolder1' used before it is assigned a value...."

If I build the application with the warning, the control does not load.
Mauro 3/11/2009 10:19 AM
Mauro,
You need to put a placeholder control in the layout of your aspx page so that when the control is loaded programmatically it's inserted in this place. Placeholder control can be added in the aspx source as

<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

If you examine Example 2, you can easily see how this was done. 
Cem Alacayir 3/25/2009 5:39 PM