views:

49

answers:

1

Currently, I'm populating a drop down list when a file is created in a configurable folder.

if (!downloadRspDropDown.Items.Contains(new ListItem(txt, fileData.FullName))

Then, I add the file and remove "No Responses Available".

However, if the same file is resubmitted (i.e., the file name is the same but the timestamp is different), then I want to remove the older entry and replace it with a new entry in the drop down list.

I have the filename, so I go into the "else" block from the line of code above. From there, I'm checking to see if I have the same filename and a different creation time.

if (downloadRspDropDown.Items.Contains(new ListItem(txt, fileData.FullName) && downloadRspDropDown.Items.Contains(new ListItem(txt, fileData.CreationTime)

From here, I want to find the position, remove it, and add the new text. This approach isn't working. Can anyone offer an alternate approach?

A: 

I dont know if that is your current problem, but this line is definitly causing trouble:

if (!downloadRspDropDown.Items.Contains(new ListItem(txt, fileData.FullName))

What you probably want to achieve is a check for equality based on a value. But this line of code performs an equality check based on a reference and can't be true, because you create a new ListItem that definitly has another reference than the item already added to the Dropdownlist.

Try to use the .FindByValue method instead.

Edit based on comment: Try something like this:

ListItem _li = downloadRspDropDown.Items.FindByValue(fileData.FullName);
if(_li != null)
{
 downloadRspDropDown.Items.Remove(_li);
}
citronas
That line of code was functioning properly and I was able to create the new entry in the drop down list if the filename wasn't present. The problem that I'm having is with the later line of code and trying to detect that the name is there, but the creation time is different. Since, I've already overwritten the older timestamp, I want to remove that entry and add the filename with the latest timestamp.
jmac
Post more code, your answer is very vague. How about adding a new item, and afterwards deleting the old? At that point of execution you should be aware of the old and the new value
citronas
I'm trying to paste a section of code in and this site is preventing me from adding the comment.
jmac
private void PopulateResponseDropDown() { List<FileInfo> fileList = new List<FileInfo>(); string rspFolder = Path.Combine(config.BulkQueryResponseFolder, CurrentUser); if (Directory.Exists(rspFolder)) { string[] files = Directory.GetFiles(rspFolder, "*.xls"); foreach (string file in files) { fileList.Add(new FileInfo(file)); } } else { Directory.CreateDirectory(rspFolder); }
jmac
if (fileList.Count > 0) { fileList.Sort(new FileCreationDateComparer()); foreach (FileInfo fileData in fileList) { string txt = fileData.Name + " - " + fileData.CreationTime.ToString("yyyy/MM/dd HH:mm:ss"); if (!downloadRspDropDown.Items.Contains(new ListItem(txt, fileData.FullName))) { downloadRspDropDown.Items.Add(new ListItem(txt, fileData.FullName)); }
jmac
This is the key piece here:else { if (downloadRspDropDown.Items.Contains(new ListItem(txt, fileData.FullName) downloadRspDropDown.Items.Add(new ListItem(txt, fileData.FullName)); } }
jmac
So I guess it works then? If you are comfortable with my help, upvote my answer please
citronas
No it doesn't work. Jaxidian and yourself asked me to post additional code. I did this above. I'm able to locate whether fileData.FullName is present. If it's not, then I add it.My problem is when I'm checking to see if fileData.FullName is there and a CreationTime is different. If this is the case, then I want to remove the older list item and put a new list item in the drop down list (i.e., the older file has already been overwritten).
jmac