Upload files in gridview
<p>I have tried the code below for uploading files in a <strong>GridView</strong> but I'm currently having 2 problems:</p>

<ol>
<li><p>I want to prevent insertion of duplicate files, but so far my code does not work.</p>

<p>code :</p>

<pre><code>for (int i = 0; i &lt; count; i++)
 {
   if (GridViewEfile.Rows[i].Cells[1].Text == FileName)
   {
     Label2.Text = "File already in the list";
     break;
   }
 }
</code></pre></li>
<li><p>I would like to make the code more compact</p></li>
</ol>

<p>Code:</p>

<pre><code>protected void btnUpload_Click(object sender, EventArgs e)
{
    DataTable dt;
    DataRow dr = null;
    FileInfo fileObj = new FileInfo(FileUpload1.PostedFile.FileName);
    string FileName = FileUpload1.PostedFile.FileName;


    long size = FileUpload1.PostedFile.ContentLength;

    if (FileUpload1.HasFile)
    {
        if (FileUpload1.PostedFile.ContentLength &gt; 0)
        {


            if (GridViewEfile.Rows.Count &gt; 0)
            {

                int count = GridViewEfile.Rows.Count;

                dt = new DataTable();

                DataColumn dc1 = new DataColumn("File Name", typeof(string));

                DataColumn dc2 = new DataColumn("File Size", typeof(string));

                dt.Columns.Add(dc1);

                dt.Columns.Add(dc2);



                for (int i = 0; i &lt; count; i++)
                {
                    if (GridViewEfile.Rows[i].Cells[1].Text == FileName)
                    {
                        Label2.Text = "File already in the list";
                        break;

                    }}


                        for (int j = 0; j &lt; count; j++)
                        {

                            dr = dt.NewRow();
                            dr["File Name"] = GridViewEfile.Rows[j].Cells[1].Text;
                            dr["File Size"] = GridViewEfile.Rows[j].Cells[2].Text;
                            dt.Rows.Add(dr);
                        }

                        dr = dt.NewRow();

                        dr["File Name"] = FileName;
                        if (size &gt; 0)
                            dr["File Size"] = size.ToString() + " KB";
                        else
                            Label2.Text = "File size cannot be 0";


                        dt.Rows.Add(dr);

                        GridViewEfile.DataSource = dt;

                        GridViewEfile.DataBind();
                    }
            else
            {
                dt = new DataTable();

                DataColumn dc1 = new DataColumn("File Name", typeof(string));

                DataColumn dc2 = new DataColumn("File Size", typeof(string));

                dt.Columns.Add(dc1);

                dt.Columns.Add(dc2);

                dr = dt.NewRow();

                dr["File Name"] = FileName;
                if (size &gt; 0)
                    dr["File Size"] = size.ToString() + " KB";
                else
                    dr["File Size"] = fileObj.Length.ToString() + " Bytes";


                dt.Rows.Add(dr);
                GridViewEfile.DataSource = dt;
                GridViewEfile.DataBind();
            }

        }


    }
    else
    {
        Label2.Text = "Please select a file to add";
    }
}
</code></pre>

<p>Markup:</p>

<pre><code>&lt;asp:GridView ID="GridViewEfile" runat="server" AutoGenerateDeleteButton="True" Height="122px" ShowFooter="True" Style="left: 173px;  top: 84px" Width="532px" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4"
         OnRowCommand="GridViewEfile_RowCommand"   OnRowDeleting="GridViewEfile_RowDeleting"&gt;
        &lt;AlternatingRowStyle BorderColor="#FFC0C0" /&gt;
        &lt;FooterStyle BackColor="White" ForeColor="#333333" /&gt;
        &lt;RowStyle BackColor="White" BorderStyle="Dotted" ForeColor="#333333" /&gt;
        &lt;SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" /&gt;
        &lt;PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" /&gt;
        &lt;HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" /&gt;
        &lt;emptydatatemplate&gt;
               &lt;center&gt; No Files have been selected for upload.&lt;/center&gt; 
        &lt;/emptydatatemplate&gt;
    &lt;/asp:GridView&gt;
    &lt;asp:Label ID="Label2" runat="server" Text="Label"&gt;&lt;/asp:Label&gt;
 &lt;asp:FileUpload ID="FileUpload1" runat="server" /&gt;
&lt;asp:Button ID="btnUpload" runat="server" Text="Upload" 
    onclick="btnUpload_Click" /&gt;
</code></pre>
