I'm a university student doing a project for a unit. We're creating a web application dashboard, and I can't seem to remove duplicates from a listBox (customerNameListBox).
public partial class Graphs : System.Web.UI.MasterPage
{
string FullDataCSV = Path.Combine(HttpContext.Current.Server.MapPath
("~/App_Data/Full_Data.csv"));
List<CSVEntry> CSVList = new List<CSVEntry>();
public void ReadFile()
{
try
{
StreamReader inputFile;
string line;
CSVEntry entry = new CSVEntry();
char[] delim = { ',' };
inputFile = File.OpenText(FullDataCSV);
while (!inputFile.EndOfStream)
{
line = inputFile.ReadLine();
string[] tokens = line.Split(delim);
entry.Value0 = tokens[0];
entry.customerName = tokens[22];
entry.Value29 = tokens[29];
CSVList.Add(entry);
}
}
catch
{
Response.Redirect("Error.aspx");
}
}
private void DisplayCustomerName()
{
foreach (CSVEntry entry in CSVList)
{
customerNameListBox.Items.Add(entry.customerName);
}
}
private void SortCustomerName()
{
CSVList = CSVList.OrderBy(x => x.customerName).ToList();
}
protected void Page_Load(object sender, EventArgs e)
{
ReadFile();
SortCustomerName();
DisplayCustomerName();
}
protected void historyButton_Click(object sender, EventArgs e)
{
Response.Redirect("History.aspx");
}
protected void exportButton_Click(object sender, EventArgs e)
{
Response.Redirect("Export.aspx");
}
protected void printButton_Click(object sender, EventArgs e)
{
Response.Redirect("Print.aspx");
}
}
I have tried using the below code to remove the duplicate items in the customerNameTextBox, but it's not working at all.
protected void goButton_Click(object sender, EventArgs e)
{
List<string> removals = new List<string>();
foreach (string s in customerNameListBox.Items)
{
removals.Add(s);
}
foreach (string s in removals)
{
customerNameListBox.Items.Remove(s);
}