Click here to Skip to main content
15,896,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
public delegate void FileDelegate(List<string>str);

public event FileDelegate AllFilesCompleted;

public ObservableCollection<FileUpload> files;

List<string> str = new List<string>();


void upload(object sender, EventArgs e)
{
FileUpload fu = sender as FileUpload;
if (fu.Status == FileUploadStatus.Complete)
{
if (uploading)
UploadFiles();
if (AllFilesCompleted != null)
{
if (files.Count == files.Count(q => q.Status == FileUploadStatus.Complete))
foreach (string data in files) //Error line
{
files.ElementAt(i).Name; //Error line
str.Add(data);
}
}
}
}

SQL
Hi,

When i build the program i got below errors. Please help me what i did mistake in code...


Cannot convert type 'DC.FileUpload.FileUpload' to 'string'

Only assignment, call, increment, decrement, and new object expressions can be used as a statement

The name 'i' does not exist in the current context
Posted

Hello,

First:
public ObservableCollection<FileUpload> files;

You've declared it as collection of FileUploads and in foreach you are trying to iterate strings.

Second:
files.ElementAt(i).Name;

You didn't declare i. Since you are using foreach, I guess that you want to take value of data variable there, since it's the element on that position.

Hope it helps.

Regards,
 
Share this answer
 
Comments
Rnshanmugavadivel 20-Feb-15 6:32am    
Hi,I'm fresher,So please help me.. how to correct the error.. Please explain me..

Thanks
To get rid of your first error...

Change foreach (string data in files) to
foreach (var data in files)
Then hover your mouse over the word var - you will see that it is of type FileUpload - as Caldazar87 pointed out, the collection is not a collection of string, but a collection of FileUpload.

The second error is probably because you used to have a for loop here with an iterator of i. You have now replaced that with a foreach and i no longer exists. You don't need to use files.ElementAt as the data variable is already giving you that element.

Which leads me to the next issue you have - you are not doing anything with that name - at the very least you appear to want to assign it to a string variable e.g.
var theName = data.Name;


Which will then present you with another problem in the line
str.Add(data);
- you declared str as a List<string></string> but data is a FileUpload object. I'm guessing you just want the name of the file so change that part from
C#
files.ElementAt(i).Name; //Error line
str.Add(data);
to
C#
str.Add(data.Name);


Finally, make sure that you read the Error List fully when trying to compile your program - all of these errors would be listed and usually there is enough of a clue to point you in the direction of the fix. If you are using Visual Studio, you can double click on a error to take you to the line of code that is at fault. Always start looking at the first error on the list as many of the others may disappear once that is fixed.
 
Share this answer
 
Comments
Rnshanmugavadivel 20-Feb-15 7:40am    
Thanks for detail Explanation.. It is easy to understand..

Thank you very Much...
CHill60 20-Feb-15 7:46am    
My pleasure!
Rnshanmugavadivel 20-Feb-15 8:09am    
Hi

one more doubt... i stored in names to list

public static List<string> Filenames = new List<string>();

How to pass the list from upload.cs to import.cs file
CHill60 20-Feb-15 8:28am    
You will need a method in import.cs that takes a List<string> as a parameter
Rnshanmugavadivel 20-Feb-15 9:08am    
please give some examples..

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