ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Upload and display multiple files in GridView using AjaxFileUpload in ASP.Net<p>Hi <a class="username" rel="Soni">Soni</a>,</p> <p>If you are looking for check wether file exist or not in database and accordingly to change back color of gridview row and else insert file so refer below sample code.</p> <p>For file size and extension refer</p> <h1 class="header"><a class="title f16" href="https://www.aspforums.net:443/Threads/196936/How-to-check-file-size-before-uploading-in-AjaxFileUpload-control-using-C-and-JavaScript-in-ASPNet/">How to check file size before uploading in AjaxFileUpload control using C# and JavaScript in ASP.Net</a></h1> <h1 class="header"><a href="https://www.aspsnippets.com/Articles/Validate-Check-File-Size-in-KB-Dimensions-Height-and-Width-of-an-Image-using-CustomValidator-in-ASPNet-using-C-and-VBNet.aspx">Validate (Check) File Size in KB, Dimensions (Height and Width) of an Image using CustomValidator in ASP.Net using C# and VB.Net</a></h1> <h1 class="header"><a class="title f16" href="https://www.aspforums.net:443/Threads/135387/Validate-uploaded-file-extension-using-AjaxFileUpload-and-change-file-name-in-ASPNet/">Validate uploaded file extension using AjaxFileUpload and change file name in ASP.Net</a></h1> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;div&gt; &lt;asp:DropDownList ID=&#34;DropDownList1&#34; runat=&#34;server&#34; AutoPostBack=&#34;True&#34; OnSelectedIndexChanged=&#34;DropDownList1_SelectedIndexChanged&#34;&gt; &lt;asp:ListItem&gt;Select Item&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;Passport&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;HighSchool Certificate&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;HighSchool Equivalency&lt;/asp:ListItem&gt; &lt;asp:ListItem&gt;Discount&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;asp:Label ID=&#34;lblMessage&#34; runat=&#34;server&#34; /&gt; &lt;asp:ScriptManager ID=&#34;ScriptManager1&#34; runat=&#34;server&#34;&gt; &lt;/asp:ScriptManager&gt; &lt;ajaxtoolkit:AjaxFileUpload ID=&#34;AjaxFileUpload1&#34; runat=&#34;server&#34; Width=&#34;500px&#34; AllowedFileTypes=&#34;png,bmp,tiff,jpg,jpeg,pdf,doc,docx&#34; MaximumNumberOfFiles=&#34;4&#34; OnUploadComplete=&#34;AjaxFileUpload1_UploadComplete&#34; maxfilesize=&#34;2048&#34; /&gt; &lt;asp:GridView ID=&#34;GridView1&#34; DataKeyNames=&#34;Stud_Id&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;False&#34; OnRowCommand=&#34;grdAttachment_RowCommand&#34; CellPadding=&#34;4&#34; ForeColor=&#34;#333333&#34; GridLines=&#34;None&#34;&gt; &lt;AlternatingRowStyle BackColor=&#34;White&#34; ForeColor=&#34;#284775&#34; /&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;Stud_Id&#34; HeaderText=&#34;Student ID&#34; /&gt; &lt;asp:BoundField DataField=&#34;FileName&#34; HeaderText=&#34;File Name&#34; /&gt; &lt;asp:BoundField DataField=&#34;FileType&#34; HeaderText=&#34;File Type&#34; /&gt; &lt;asp:BoundField DataField=&#34;FileDoc&#34; HeaderText=&#34;Byte Data&#34; /&gt; &lt;asp:TemplateField HeaderText=&#34;Open File&#34;&gt; &lt;ItemTemplate&gt; &lt;asp:LinkButton ID=&#34;btnOpen&#34; Text=&#34;View&#34; CommandArgument=&#39;&lt;%#Bind(&#34;FileType&#34;)%&gt;&#39; Font-Bold=&#34;true&#34; runat=&#34;server&#34; CommandName=&#34;ViewFile&#34;&gt;&lt;/asp:LinkButton&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;EditRowStyle BackColor=&#34;#999999&#34; /&gt; &lt;FooterStyle BackColor=&#34;#5D7B9D&#34; Font-Bold=&#34;True&#34; ForeColor=&#34;White&#34; /&gt; &lt;HeaderStyle BackColor=&#34;#5D7B9D&#34; Font-Bold=&#34;True&#34; ForeColor=&#34;White&#34; /&gt; &lt;PagerStyle BackColor=&#34;#284775&#34; ForeColor=&#34;White&#34; HorizontalAlign=&#34;Center&#34; /&gt; &lt;RowStyle BackColor=&#34;#F7F6F3&#34; ForeColor=&#34;#333333&#34; /&gt; &lt;SelectedRowStyle BackColor=&#34;#E2DED6&#34; Font-Bold=&#34;True&#34; ForeColor=&#34;#333333&#34; /&gt; &lt;SortedAscendingCellStyle BackColor=&#34;#E9E7E2&#34; /&gt; &lt;SortedAscendingHeaderStyle BackColor=&#34;#506C8C&#34; /&gt; &lt;SortedDescendingCellStyle BackColor=&#34;#FFFDF8&#34; /&gt; &lt;SortedDescendingHeaderStyle BackColor=&#34;#6F8DAE&#34; /&gt; &lt;/asp:GridView&gt; &lt;/div&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Diagnostics; using System.IO; using System.Data.SqlClient; using System.Data; using System.Drawing; </pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.BindGrid(); } } private void BindGrid() { string constr = &#34;Data Source=******;Initial Catalog=TEST;User ID=sa;Password=******&#34;; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = &#34;SELECT * FROM FileUploadTest&#34;; cmd.Connection = con; con.Open(); GridView1.DataSource = cmd.ExecuteReader(); GridView1.DataBind(); con.Close(); } } } protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e) { string contentTypes = e.ContentType.ToLower(); string fileName = Path.GetFileName(e.FileName); AjaxFileUpload1.SaveAs(Server.MapPath(&#34;~/Uploads/&#34; + fileName)); string filePath = Server.MapPath(&#34;~/Uploads/&#34; + fileName); FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); Byte[] bytes = br.ReadBytes((Int32)fs.Length); String strConnString = &#34;Data Source=*.*.*;Initial Catalog=TEST;User ID=sa;Password=*******&#34;; SqlConnection con = new SqlConnection(strConnString); SqlCommand cmd; cmd = new SqlCommand(&#34;SELECT FileName FROM FileUploadTest WHERE FileName = @FileName&#34;,con); cmd.Parameters.AddWithValue(&#34;@FileName&#34;, fileName); con.Open(); string file = Convert.ToString(cmd.ExecuteScalar()); con.Close(); for (int i = 0; i &lt; GridView1.Rows.Count; i++) { if (file == GridView1.Rows[i].Cells[1].Text) { GridView1.Rows[i].BackColor = Color.Green; } else { GridView1.Rows[i].BackColor = Color.Red; cmd = new SqlCommand(&#34;INSERT INTO FileUploadTest(FileName, FileType, FileDoc) values (@FileName, @FileType, @FileDoc)&#34;); cmd.Parameters.AddWithValue(&#34;@FileName&#34;, fileName); string contentType = &#34;&#34;; switch (contentTypes) { case &#34;.png&#34;: contentType = &#34;png&#34;; break; case &#34;.jpg&#34;: contentType = &#34;application/jpg&#34;; break; case &#34;.jpeg&#34;: contentType = &#34;application/jpeg&#34;; break; case &#34;.bmp&#34;: contentType = &#34;Application/bmp&#34;; break; case &#34;.tiff&#34;: contentType = &#34;Application/tiff&#34;; break; case &#34;.gif&#34;: contentType = &#34;Application/gif&#34;; break; case &#34;.pdf&#34;: contentType = &#34;Application/pdf&#34;; break; case &#34;.doc&#34;: contentType = &#34;Application/doc&#34;; break; case &#34;.docx&#34;: contentType = &#34;Application/docx&#34;; break; default: break; } cmd.Parameters.AddWithValue(&#34;@FileType&#34;, contentType); cmd.Parameters.AddWithValue(&#34;@FileDoc&#34;, bytes); cmd.CommandType = CommandType.Text; cmd.Connection = con; try { con.Open(); cmd.ExecuteNonQuery(); } catch (Exception ex) { Response.Write(ex.Message); } finally { con.Close(); con.Dispose(); } } } } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { var text = DropDownList1.SelectedItem.Value; lblMessage.Text = text; for (int i = 0; i &lt; this.GridView1.Rows.Count; i++) { this.GridView1.Rows[i].Visible = true; string cellvalue = GridView1.Rows[i].Cells[1].Text; if (cellvalue.TrimEnd() != text.TrimEnd()) { GridView1.Rows[i].BackColor = Color.Yellow; GridView1.Rows[i].BackColor = System.Drawing.Color.Red; Response.Write(&#34;Values not matched&#34;); } else { string constr = &#34;Data Source=****;Initial Catalog=TEST;User ID=sa;Password=*******&#34;; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = &#34;SELECT * FROM FileUploadTest&#34;; cmd.Connection = con; con.Open(); GridView1.DataSource = cmd.ExecuteReader(); GridView1.DataBind(); con.Close(); } } ScriptManager.RegisterStartupScript(this, this.GetType(), &#34;script&#34;, &#34;alert(&#39;Record Saved Sucessfully&#39;);&#34;, true); } } } protected void grdAttachment_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == &#34;ViewFile&#34;) { string fileName = Server.MapPath(@&#34;~/Uploads/&#34; + e.CommandArgument.ToString()); Process process = new Process(); process.StartInfo.UseShellExecute = true; if (File.Exists(@&#34;Folder\example.pdf&#34;)) { ProcessStartInfo psStartInfo = new ProcessStartInfo(); psStartInfo.FileName = @&#34;Folder\example.pdf&#34;; Process ps = Process.Start(psStartInfo); } else { ScriptManager.RegisterStartupScript(this, this.GetType(), &#34;script&#34;, &#34;alert(&#39;File Not found&#39;);&#34;, true); } } }</pre>https://www.aspforums.net:443/Threads/185496/Upload-and-display-multiple-files-in-GridView-using-AjaxFileUpload-in-ASPNet/https://www.aspforums.net:443/Threads/185496/Upload-and-display-multiple-files-in-GridView-using-AjaxFileUpload-in-ASPNet/Wed, 14 Aug 2019 03:34:26 GMT