I have input data in excel and csv format.so depending upon whether user is giving excel or csv ,i have to use that.i have attached the code.

if (excelfileupload.HasFile)
{


String FilePath = Server.MapPath("~/Files/FileName.csv");
System.IO.File.Delete(FilePath);



DropDownList1.Visible = true;
DataSet excelds = (DataSet)Session["excelDataSet"];
DataTable samples = excelds.Tables[0];
ArrayList rowcontents = new ArrayList();


if (samples.Rows.Count > 0)
{
for (int j = 0; j < samples.Columns.Count; j++)
{
rowcontents.Add(samples.Columns[j].ColumnName);
}

}
DropDownList1.DataSource = rowcontents;
DropDownList1.DataBind();


} 

if (csvfileupload.HasFile)
{


DropDownList1.Visible = true;
DataSet csvds = (DataSet)Session["csvDataSet"];
DataTable csvsamples = csvds.Tables[0];
ArrayList csvrowcontents = new ArrayList();

if (csvsamples.Rows.Count > 0)
{
for (int j = 0; j < csvsamples.Columns.Count; j++)
{
csvrowcontents.Add(csvsamples.Columns[j].ColumnName);
}

}
DropDownList1.DataSource = csvrowcontents;
DropDownList1.DataBind();

}

if am using first excel,then csv,both are working.but if again i am using excel,(since csv is now also having its uploaded file)it is showing column names of csv file only.I want to show column names of file that i have chosen then.how i can do this. after excel,csv file is coming.But if i am uploading excel after csv,then the column names of the old csv file is shown in the drop down list.
I am uploading excel and csv file from master page menu only.how i can correct the problem

Recommended Answers

All 4 Replies

Not sure i am understanding.. have u tried clearing the dropdown list items before each code block? as you are using the same drop down

no.how can i do that?can you please tell me...

As per what jfarrugia mentioned, try using

DropDownList1.Items.Clear();

It is a good idea to do this before any databinding. It will ensure there is no data in the drop down list when the new data is added.

Also, you are using two if statements and not an if else statement for which to bind the values from. Are you meaning to do this, or is it mutually exclusive? ie, can it be csvfileupload.HasFile and excelfileupload.HasFile, or only one of the two?

Just a thought. Hope that helps.

As per what jfarrugia mentioned, try using

DropDownList1.Items.Clear();

It is a good idea to do this before any databinding. It will ensure there is no data in the drop down list when the new data is added.

Also, you are using two if statements and not an if else statement for which to bind the values from. Are you meaning to do this, or is it mutually exclusive? ie, can it be csvfileupload.HasFile and excelfileupload.HasFile, or only one of the two?

Just a thought. Hope that helps.

Hi sir, it can be any one type according to user.i tried your suggestion..but problem is still there.after excel file upload,if am uploading csv,then its column names are coming in the dropdownlist.but again if am uploading excel,same old csv data column names are coming...please suggest me one solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.