title: Get Filenames From Folder in Gridview Control in ASP.Net c#
body: <p>I need shows the file names from a folder into a GridView control.</p>
  
  <p>I thinked use the <strong>Directory class</strong>.</p>
  
  <p>In my database I have the column <strong>sFolder</strong> with this value for each row:</p>
  
  <pre><code>control/Imp/foo
  </code></pre>
  
  <p>I have tried this <a href="http://www.c-sharpcorner.com/UploadFile/f50501/get-filenames-from-folder-in-gridview-control-in-Asp-Net/" rel="nofollow noreferrer">tutorial</a> on the web but I can't get the file names from a folder into a GridView control.</p>
  
  <p>I don't have error but the GridView is empty even if the path to the folder is correct.</p>
  
  <p>My code below.</p>
  
  <p>Can you help me?</p>
  
  <p>Thank you in advance for any help, really appreciated</p>
  
  <p><strong>.cs</strong></p>
  
  <pre><code>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 &gt; 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&lt;ListItem&gt; files = new List&lt;ListItem&gt;();
              foreach (string file in filesLoc)
              {
                  files.Add(new ListItem(Path.GetFileName(file)));
              }
              gvDownload.DataSource = files;
              gvDownload.DataBind();
  
          }
      }
  }
  
  return ds2;
  </code></pre>
  
  <p><strong>.aspx</strong></p>
  
  <pre><code>&lt;asp:GridView ID="gvDownload" EmptyDataText="Data empty"
      runat="server" AutoGenerateColumns="False"
      BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" GridLines="Vertical"&gt;
      &lt;AlternatingRowStyle BackColor="#DCDCDC" /&gt;
      &lt;Columns&gt;
          &lt;asp:BoundField DataField="Text" HeaderText="FileName" /&gt;
      &lt;/Columns&gt;
  &lt;/asp:GridView&gt;
  </code></pre>
  
  <p><strong>#Edit 01</strong></p>
  
  <pre><code>    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 &gt; 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&lt;ListItem&gt; files = new List&lt;ListItem&gt;();
              foreach (string file in filesLoc)
              {
                  files.Add(new ListItem(Path.GetFileName(file)));
              }
              gvDownload.DataSource = files;
              gvDownload.DataBind();
  
          }
      }
  }
  
  return ds2;
  
  }
  </code></pre>