How to determine name of files via RadUpload1.UploadedFiles

Thread is closed for posting
2 posts, 0 answers
  1. Joe
    Joe avatar
    51 posts
    Member since:
    Oct 2008

    Posted 09 Dec 2008 Link to this post

    I am using the following code:

    1 For Each f As UploadedFile In RadUpload1.UploadedFiles  
    2  
    3             Dim targetFolder As String = Session("UserDir")  
    4             Dim targetFileName As String = System.IO.Path.Combine(targetFolder, f.GetName)  
    5  
    6             If System.IO.File.Exists(targetFileName) Then 
    7                 'Sets error label to visable and displays message.  
    8                 lblUploadError1.Visible = True 
    9                 lblUploadError1.Text = "File name " & f.GetName & " already exists!" 
    10             Else 
    11             'Save uploaded file(s) info to database  
    12  End Using
    13 End IF
    Next
     

    The problem is that I want to display an error message for any of the 5 possible uploaded files by changing the lblUploadError1.Text thru lblUplaodError5.Text

    So somehow I need to associate the f.GetName with each of the files that were uploaded.

    Any suggestions?

    Thanks,
    Joe
  2. Joe
    Joe avatar
    51 posts
    Member since:
    Oct 2008

    Posted 09 Dec 2008 Link to this post

    Ok, I'm on a role today.... I've figured this one out too... Here's the final code. If anyone has a better, more elegent solution, please let me know.

    1 Imports Telerik.Web.UI  
    2 Imports System.IO  
    3 Imports System.Data.SqlClient  
    4 Imports Telerik.Web.UI.Upload  
    5  
    6 Protected Sub SubmitButton_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles SubmitButton.Click  
    7         'Set target directory path to user's directory  
    8         'RadUpload1.TargetPhysicalFolder = Session("userDir")  
    9  
    10         If RadUpload1.UploadedFiles.Count > 0 Then 
    11             System.Threading.Thread.Sleep(3000)  
    12         End If 
    13  
    14         For Each f As UploadedFile In RadUpload1.UploadedFiles  
    15  
    16             Dim targetFolder As String = Session("UserDir")  
    17             Dim targetFileName As String = System.IO.Path.Combine(targetFolder, f.GetName)  
    18  
    19             If System.IO.File.Exists(targetFileName) Then 
    20                 'Sets error label to visible and displays message.  
    21                 lblUploadError.Visible = True 
    22                 lblUploadError1.Visible = True 
    23                 'This add's all the preexisting file names to the label  
    24                 lblUploadError1.Text = lblUploadError1.Text & f.GetName & ", " 
    25             Else 
    26                 f.SaveAs(Session("UserDir") & f.GetName, False)  
    27  
    28                 ' Insert a new record into Call_Info table  
    29  
    30                 'Set Active Date on new user to today  
    31                 Dim UploadDate As DateTime = DateTime.Now  
    32  
    33                 Dim connectionString As String = ConfigurationManager.ConnectionStrings("eonPortalCS").ConnectionString  
    34                 Dim insertSql As String = "INSERT INTO Call_Info(UserId, File_Name, File_Location, Discipline, Call_Status, Upload_Date, Uploaded_By) VALUES(@UserId, @FileName, @FileLocation, @Discipline, @CallStatus, @UploadDate, @UploadedBy)" 
    35                 Dim myConnection As New System.Data.SqlClient.SqlConnection(connectionString)  
    36                 Dim myCommand As New System.Data.SqlClient.SqlCommand(insertSql, myConnection)  
    37                 Using myConnection  
    38                     myConnection.Open()  
    39                     myCommand.Parameters.AddWithValue("@UserId", Session("UploadUserID"))  
    40                     myCommand.Parameters.AddWithValue("@FileName", f.GetName)  
    41                     myCommand.Parameters.AddWithValue("@FileLocation", Session("UserDir"))  
    42                     myCommand.Parameters.AddWithValue("@Discipline", Session("UserDiscipline"))  
    43                     myCommand.Parameters.AddWithValue("@CallStatus""New")  
    44                     myCommand.Parameters.AddWithValue("@UploadDate", UploadDate)  
    45                     myCommand.Parameters.AddWithValue("@UploadedBy", Session("CurrentUserGuid"))  
    46                     myCommand.ExecuteNonQuery()  
    47                     myConnection.Close()  
    48                 End Using  
    49             End If 
    50         Next 
    51         RadUpload1.Enabled = False 
    52         SubmitButton.Enabled = False 
    53         Session("UploadUserID") = "" 
    54  
    55     End Sub 

    Thanks,
    Joe
Back to Top