Hi Soni,
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.
For file size and extension refer
HTML
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>Select Item</asp:ListItem>
<asp:ListItem>Passport</asp:ListItem>
<asp:ListItem>HighSchool Certificate</asp:ListItem>
<asp:ListItem>HighSchool Equivalency</asp:ListItem>
<asp:ListItem>Discount</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="lblMessage" runat="server" />
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<ajaxtoolkit:AjaxFileUpload ID="AjaxFileUpload1" runat="server" Width="500px" AllowedFileTypes="png,bmp,tiff,jpg,jpeg,pdf,doc,docx"
MaximumNumberOfFiles="4" OnUploadComplete="AjaxFileUpload1_UploadComplete" maxfilesize="2048" />
<asp:GridView ID="GridView1" DataKeyNames="Stud_Id" runat="server" AutoGenerateColumns="False"
OnRowCommand="grdAttachment_RowCommand" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="Stud_Id" HeaderText="Student ID" />
<asp:BoundField DataField="FileName" HeaderText="File Name" />
<asp:BoundField DataField="FileType" HeaderText="File Type" />
<asp:BoundField DataField="FileDoc" HeaderText="Byte Data" />
<asp:TemplateField HeaderText="Open File">
<ItemTemplate>
<asp:LinkButton ID="btnOpen" Text="View" CommandArgument='<%#Bind("FileType")%>'
Font-Bold="true" runat="server" CommandName="ViewFile"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</div>
Namespaces
C#
using System.Diagnostics;
using System.IO;
using System.Data.SqlClient;
using System.Data;
using System.Drawing;
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string constr = "Data Source=******;Initial Catalog=TEST;User ID=sa;Password=******";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT * FROM FileUploadTest";
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("~/Uploads/" + fileName));
string filePath = Server.MapPath("~/Uploads/" + fileName);
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
String strConnString = "Data Source=*.*.*;Initial Catalog=TEST;User ID=sa;Password=*******";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd;
cmd = new SqlCommand("SELECT FileName FROM FileUploadTest WHERE FileName = @FileName",con);
cmd.Parameters.AddWithValue("@FileName", fileName);
con.Open();
string file = Convert.ToString(cmd.ExecuteScalar());
con.Close();
for (int i = 0; i < 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("INSERT INTO FileUploadTest(FileName, FileType, FileDoc) values (@FileName, @FileType, @FileDoc)");
cmd.Parameters.AddWithValue("@FileName", fileName);
string contentType = "";
switch (contentTypes)
{
case ".png":
contentType = "png";
break;
case ".jpg":
contentType = "application/jpg";
break;
case ".jpeg":
contentType = "application/jpeg";
break;
case ".bmp":
contentType = "Application/bmp";
break;
case ".tiff":
contentType = "Application/tiff";
break;
case ".gif":
contentType = "Application/gif";
break;
case ".pdf":
contentType = "Application/pdf";
break;
case ".doc":
contentType = "Application/doc";
break;
case ".docx":
contentType = "Application/docx";
break;
default:
break;
}
cmd.Parameters.AddWithValue("@FileType", contentType);
cmd.Parameters.AddWithValue("@FileDoc", 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 < 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("Values not matched");
}
else
{
string constr = "Data Source=****;Initial Catalog=TEST;User ID=sa;Password=*******";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT * FROM FileUploadTest";
cmd.Connection = con;
con.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
con.Close();
}
}
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Record Saved Sucessfully');", true);
}
}
}
protected void grdAttachment_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ViewFile")
{
string fileName = Server.MapPath(@"~/Uploads/" + e.CommandArgument.ToString());
Process process = new Process();
process.StartInfo.UseShellExecute = true;
if (File.Exists(@"Folder\example.pdf"))
{
ProcessStartInfo psStartInfo = new ProcessStartInfo();
psStartInfo.FileName = @"Folder\example.pdf";
Process ps = Process.Start(psStartInfo);
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('File Not found');", true);
}
}
}