Hi kid.live,
Please refer below sample.
HTML
<div class="layout-px-spacing">
<div class="page-header">
</div>
<div class="row layout-top-spacing">
<div class="col-xl-12 col-lg-12 col-md-12 col-12 layout-spacing">
<div class="widget widget-content-area br-4">
<div class="widget-one">
<div class="row">
<div class="col-md-2">
<asp:Label ID="lbl_SearchByBranch" runat="server" Text="بحث بالوحدة"></asp:Label>
</div>
<div class="col-md-2">
<asp:DropDownList ID="ddlBranches" CssClass="form-control form-control-sm" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="OnSelectedIndexChanged">
<asp:ListItem Value="">All</asp:ListItem>
<asp:ListItem Value="001">Branch 1</asp:ListItem>
<asp:ListItem Value="005">Branch 5</asp:ListItem>
<asp:ListItem Value="006">Branch 6</asp:ListItem>
</asp:DropDownList>
</div>
<div class="col-md-2 text-right">
<asp:Label ID="lbl_SearchByDate" runat="server" Text="بحث بتاريخ"></asp:Label>
</div>
<div class="col-md-2">
<div class="input-group mb-4">
<asp:TextBox ID="txt_SearchByDate" OnTextChanged="txt_SearchByDate_TextChanged" CssClass="form-control" runat="server"></asp:TextBox>
<div class="input-group-append">
<asp:LinkButton ID="btn_Calendar" OnClick="btn_Calendar_Click" runat="server" CssClass="btn btn-dark">
<i class="bi bi-calendar3"></i>Open
</asp:LinkButton>
</div>
</div>
<asp:Calendar ID="datepicker" OnSelectionChanged="datepicker_SelectionChanged" Visible="false" runat="server"></asp:Calendar>
</div>
<div class="col-md-2 offset-2">
<asp:Button ID="btn_ExportExcel" OnClick="btn_ExportExcel_Click" CssClass="btn btn-info btn-block mb-2" runat="server" Text="Export To Excel" />
</div>
</div>
<hr />
<div class="row">
<div class="col-12">
<asp:GridView runat="server" CssClass="table table-striped table-bordered"
ID="gvDetails" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="amount" HeaderText="Amount" />
<asp:BoundField DataField="branch_num" HeaderText="Branch No" />
<asp:BoundField DataField="machineID" HeaderText="Machine" />
<asp:BoundField DataField="shift" HeaderText="Shift" />
<asp:BoundField DataField="userID" HeaderText="User" />
<asp:BoundField DataField="voucher_date" HeaderText="Voucher Date" />
<asp:BoundField DataField="voucher_number" HeaderText="Voucher Number" />
<asp:TemplateField>
<ItemTemplate>
<button class="btn btn-outline-dark rounded-circle">
<i class="bi bi-printer"></i>
</button>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle HorizontalAlign="Center" CssClass="GridPager" />
</asp:GridView>
</div>
</div>
<div class="row">
<div class="col-md-12 text-right">
<asp:Label ID="lblTotal" Text="Total No Of Records" runat="server" />
<asp:Label ID="lblTotalRecords" CssClass="badge-chip badge-danger mt-1 mb-1 ml-1" runat="server" />
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Namespaces
C#
using System.Net;
using Newtonsoft.Json;
VB.Net
Imports System.Net
Imports Newtonsoft.Json
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.GetJsonData(string.Empty, string.Empty);
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
this.GetJsonData(ddlBranches.SelectedValue, datepicker.SelectedDate.ToLongDateString());
}
public void GetJsonData(string branch, string searchbydate)
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
string json = (new WebClient()).DownloadString("https://receipt-voucher-default-rtdb.firebaseio.com/receipts-prod.json");
Dictionary<string, object> values = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
int noofrecords = values.Count;
List<Receipt> receipts = new List<Receipt>();
foreach (var entry in values)
{
receipts.Add(JsonConvert.DeserializeObject<Receipt>(entry.Value.ToString()));
}
if (!string.IsNullOrEmpty(branch) && !string.IsNullOrEmpty(searchbydate) && searchbydate != "Monday, January 1, 0001")
{
receipts = receipts.Where(x => x.branch_num == branch && Convert.ToDateTime(x.voucher_date).ToString("dd/MM/yyyy") == Convert.ToDateTime(searchbydate).ToString("dd/MM/yyyy")).ToList();
}
else if (!string.IsNullOrEmpty(branch))
{
receipts = receipts.Where(x => x.branch_num == branch).ToList();
if (!string.IsNullOrEmpty(searchbydate) && searchbydate != "Monday, January 1, 0001")
{
receipts = receipts.Where(x => Convert.ToDateTime(x.voucher_date).ToString("dd/MM/yyyy") == Convert.ToDateTime(searchbydate).ToString("dd/MM/yyyy")).ToList();
}
}
else if (!string.IsNullOrEmpty(searchbydate) && searchbydate != "Monday, January 1, 0001")
{
if (!string.IsNullOrEmpty(branch))
{
receipts = receipts.Where(x => x.branch_num == branch).ToList();
}
receipts = receipts.Where(x => Convert.ToDateTime(x.voucher_date).ToString("dd/MM/yyyy") == Convert.ToDateTime(searchbydate).ToString("dd/MM/yyyy")).ToList();
}
gvDetails.DataSource = receipts;
gvDetails.DataBind();
lblTotalRecords.Text = receipts.Count.ToString();
}
public class Receipt
{
public string amount { get; set; }
public string branch_num { get; set; }
public string machineID { get; set; }
public string shift { get; set; }
public string userID { get; set; }
public DateTime voucher_date { get; set; }
public string voucher_number { get; set; }
}
public void ExportExcel()
{
try
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "5M_MaqbozatDetails.xls"));
Response.ContentType = "application/ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter html = new System.Web.UI.HtmlTextWriter(writer);
GetJsonData(string.Empty, string.Empty);
//dl_JsonDetails.DataBind();
gvDetails.RenderControl(html);
Response.Write(writer.ToString());
Response.Flush();
Response.End();
}
catch (Exception ex)
{
}
}
protected void btn_ExportExcel_Click(object sender, EventArgs e)
{
ExportExcel();
}
protected void btn_Calendar_Click(object sender, EventArgs e)
{
datepicker.Visible = true;
}
protected void datepicker_SelectionChanged(object sender, EventArgs e)
{
txt_SearchByDate.Text = datepicker.SelectedDate.ToLongDateString();
datepicker.Visible = false;
this.GetJsonData(ddlBranches.SelectedValue, datepicker.SelectedDate.ToLongDateString());
}
protected void txt_SearchByDate_TextChanged(object sender, EventArgs e)
{
this.GetJsonData(ddlBranches.SelectedValue, datepicker.SelectedDate.ToLongDateString());
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.GetJsonData(String.Empty, String.Empty)
End If
End Sub
Protected Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Me.GetJsonData(ddlBranches.SelectedValue, datepicker.SelectedDate.ToLongDateString())
End Sub
Public Sub GetJsonData(ByVal branch As String, ByVal searchbydate As String)
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)
Dim json As String = (New WebClient()).DownloadString("https://receipt-voucher-default-rtdb.firebaseio.com/receipts-prod.json")
Dim values As Dictionary(Of String, Object) = JsonConvert.DeserializeObject(Of Dictionary(Of String, Object))(json)
Dim noofrecords As Integer = values.Count
Dim receipts As List(Of Receipt) = New List(Of Receipt)()
For Each entry In values
receipts.Add(JsonConvert.DeserializeObject(Of Receipt)(entry.Value.ToString()))
Next
If Not String.IsNullOrEmpty(branch) AndAlso Not String.IsNullOrEmpty(searchbydate) AndAlso searchbydate <> "Monday, January 1, 0001" Then
receipts = receipts.Where(Function(x) x.branch_num = branch AndAlso Convert.ToDateTime(x.voucher_date).ToString("dd/MM/yyyy") = Convert.ToDateTime(searchbydate).ToString("dd/MM/yyyy")).ToList()
ElseIf Not String.IsNullOrEmpty(branch) Then
receipts = receipts.Where(Function(x) x.branch_num = branch).ToList()
If Not String.IsNullOrEmpty(searchbydate) AndAlso searchbydate <> "Monday, January 1, 0001" Then
receipts = receipts.Where(Function(x) Convert.ToDateTime(x.voucher_date).ToString("dd/MM/yyyy") = Convert.ToDateTime(searchbydate).ToString("dd/MM/yyyy")).ToList()
End If
ElseIf Not String.IsNullOrEmpty(searchbydate) AndAlso searchbydate <> "Monday, January 1, 0001" Then
If Not String.IsNullOrEmpty(branch) Then
receipts = receipts.Where(Function(x) x.branch_num = branch).ToList()
End If
receipts = receipts.Where(Function(x) Convert.ToDateTime(x.voucher_date).ToString("dd/MM/yyyy") = Convert.ToDateTime(searchbydate).ToString("dd/MM/yyyy")).ToList()
End If
gvDetails.DataSource = receipts
gvDetails.DataBind()
lblTotalRecords.Text = receipts.Count.ToString()
End Sub
Public Class Receipt
Public Property amount As String
Public Property branch_num As String
Public Property machineID As String
Public Property shift As String
Public Property userID As String
Public Property voucher_date As DateTime
Public Property voucher_number As String
End Class
Public Sub ExportExcel()
Try
Response.Clear()
Response.Buffer = True
Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", "5M_MaqbozatDetails.xls"))
Response.ContentType = "application/ms-excel"
Response.Charset = ""
Me.EnableViewState = False
Dim writer As System.IO.StringWriter = New System.IO.StringWriter()
Dim html As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(writer)
GetJsonData(String.Empty, String.Empty)
gvDetails.RenderControl(html)
Response.Write(writer.ToString())
Response.Flush()
Response.End()
Catch ex As Exception
End Try
End Sub
Protected Sub btn_ExportExcel_Click(ByVal sender As Object, ByVal e As EventArgs)
ExportExcel()
End Sub
Protected Sub btn_Calendar_Click(ByVal sender As Object, ByVal e As EventArgs)
datepicker.Visible = True
End Sub
Protected Sub datepicker_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs)
txt_SearchByDate.Text = datepicker.SelectedDate.ToLongDateString()
datepicker.Visible = False
Me.GetJsonData(ddlBranches.SelectedValue, datepicker.SelectedDate.ToLongDateString())
End Sub
Protected Sub txt_SearchByDate_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
Me.GetJsonData(ddlBranches.SelectedValue, datepicker.SelectedDate.ToLongDateString())
End Sub
Screenshot