<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Download PDF</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>Download PDF</h2>
<asp:Label ID="lblDocumentId" runat="server" Text="Document ID:"></asp:Label>
<asp:TextBox ID="txtDocumentId" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnDownload" runat="server" Text="Download" OnClick="btnDownload_Click" />
<br />
<asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnDownload_Click(object sender, EventArgs e)
{
int documentId;
if (int.TryParse(txtDocumentId.Text, out documentId))
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connStr))
{
string query = "SELECT EmployeeName, Document, DocumentName, ContentType FROM PDF_EmployeeDocuments WHERE Id = @Id";
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.Parameters.AddWithValue("@Id", documentId);
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
string employeeName = reader["EmployeeName"].ToString();
byte[] fileData = (byte[])reader["Document"];
string fileName = reader["DocumentName"].ToString();
string contentType = reader["ContentType"].ToString();
Response.Clear();
Response.ContentType = contentType;
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(fileData);
Response.End();
}
else
{
lblMessage.Text = "Document not found.";
}
}
conn.Close();
}
}
}
else
{
lblMessage.Text = "Please enter a valid Document ID.";
}
}
-------------------
<asp:DropDownList ID="ddlYear" runat="server"></asp:DropDownList>
<asp:DropDownList ID="ddlMonth" runat="server"></asp:DropDownList>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateYearDropdown();
PopulateMonthDropdown();
}
}
private void PopulateYearDropdown()
{
int currentYear = DateTime.Now.Year;
for (int year = currentYear; year >= currentYear - 10; year--)
{
ddlYear.Items.Add(new ListItem(year.ToString(), year.ToString()));
}
}
private void PopulateMonthDropdown()
{
// Get current month
int currentMonth = DateTime.Now.Month;
for (int month = 1; month <= 12; month++)
{
string monthName = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(month);
ddlMonth.Items.Add(new ListItem(monthName, month.ToString()));
}
// Set the selected month to the current month
ddlMonth.SelectedValue = currentMonth.ToString();
}
0 Comments