Reputation: 1407
I need shows the file names from a folder into a GridView control.
I thinked use the Directory class.
In my database I have the column sFolder with this value for each row:
control/Imp/foo
I have tried this tutorial on the web but I can't get the file names from a folder into a GridView control.
I don't have error but the GridView is empty even if the path to the folder is correct.
My code below.
Can you help me?
Thank you in advance for any help, really appreciated
.cs
dt2 = new DataTable();
ds2 = new DataSet();
sql = @String.Format(" SELECT * FROM tbl_2 WHERE sFolder IN ('control/Imp/foo'); ");
using (OdbcConnection cn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
using (OdbcCommand cmd =
new OdbcCommand(sql, cn))
{
OdbcDataAdapter adapter =
new OdbcDataAdapter(cmd);
adapter.Fill(ds2);
if (ds2.Tables.Count > 0)
{
dt2 = ds2.Tables[0];
FilePath = Server.MapPath("/myFolder/" + ds2.Tables[0].Rows[0]["sFolder"].ToString().Replace('/', '\\'));
Response.Write(FilePath);
// the response write FilePath is C:\inetpub\wwwroot\aspnet\myFolder\control\Imp\foo //
string[] filesLoc = Directory.GetFiles(FilePath);
List<ListItem> files = new List<ListItem>();
foreach (string file in filesLoc)
{
files.Add(new ListItem(Path.GetFileName(file)));
}
gvDownload.DataSource = files;
gvDownload.DataBind();
}
}
}
return ds2;
.aspx
<asp:GridView ID="gvDownload" EmptyDataText="Data empty"
runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" GridLines="Vertical">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:BoundField DataField="Text" HeaderText="FileName" />
</Columns>
</asp:GridView>
#Edit 01
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}
private void BindData()
{
RetrieveProductsDowload();
}
private DataSet RetrieveProductsDowload()
{
dt2 = new DataTable();
ds2 = new DataSet();
sql = @String.Format(" SELECT * FROM tbl_2 WHERE sFolder IN ('control/Imp/foo'); ");
using (OdbcConnection cn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
using (OdbcCommand cmd =
new OdbcCommand(sql, cn))
{
OdbcDataAdapter adapter =
new OdbcDataAdapter(cmd);
adapter.Fill(ds2);
if (ds2.Tables.Count > 0)
{
dt2 = ds2.Tables[0];
FilePath = Server.MapPath("/myFolder/" + ds2.Tables[0].Rows[0]["sFolder"].ToString().Replace('/', '\\'));
Response.Write(FilePath);
// the response write FilePath is C:\inetpub\wwwroot\aspnet\myFolder\control\Imp\foo //
string[] filesLoc = Directory.GetFiles(FilePath);
List<ListItem> files = new List<ListItem>();
foreach (string file in filesLoc)
{
files.Add(new ListItem(Path.GetFileName(file)));
}
gvDownload.DataSource = files;
gvDownload.DataBind();
}
}
}
return ds2;
}
Upvotes: 0
Views: 1760
Reputation: 2115
Please, try this:
string[] allfiles = Directory.GetFiles(FilePath, "*", SearchOption.AllDirectories);
gvDownload.DataSource = allfiles;
gvDownload.DataBind();
Upvotes: 2