Hi Kid.Live,
Kindly refer below sample.
HTML
<asp:DropDownList ID="ddlBranchNumbers" runat="server" CssClass="form-control form-control-sm"
OnSelectedIndexChanged="OnSelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
<asp:DropDownList ID="ddlSearchByMonths" runat="server" CssClass="form-control form-control-sm"
OnSelectedIndexChanged="OnSelectedIndexChanged" AutoPostBack="true" Visible="true">
<asp:ListItem Value="0">الكل</asp:ListItem>
<asp:ListItem Value="1">يناير / January</asp:ListItem>
<asp:ListItem Value="2">فبراير / February</asp:ListItem>
<asp:ListItem Value="3">مارس / March</asp:ListItem>
<asp:ListItem Value="4">أبريل / April</asp:ListItem>
<asp:ListItem Value="5">مايو / May</asp:ListItem>
<asp:ListItem Value="6">يونيو / June</asp:ListItem>
<asp:ListItem Value="7">يوليو / July</asp:ListItem>
<asp:ListItem Value="8">أغسطس / August</asp:ListItem>
<asp:ListItem Value="9">سبتمبر / September</asp:ListItem>
<asp:ListItem Value="10">أكتوبر / October</asp:ListItem>
<asp:ListItem Value="11">نوفمبر / November</asp:ListItem>
<asp:ListItem Value="12">ديسمبر / December</asp:ListItem>
</asp:DropDownList>
<asp:GridView runat="server" CssClass="table table-striped table-bordered" AutoGenerateColumns="false"
Width="100%" ID="gvInvoices">
<Columns>
<asp:BoundField DataField="branch_num" HtmlEncode="false" HeaderText="رقم المحطة<br/>Branch No" />
<asp:BoundField DataField="_date" HtmlEncode="false" DataFormatString="{0:dd/MM/yyyy}" HeaderText="التاريخ<br/>Date" />
<asp:BoundField DataField="invoice_number" HtmlEncode="false" HeaderText="رقم الفاتورة<br/>Invoice No" />
<asp:BoundField DataField="item" HtmlEncode="false" HeaderText="الصنف<br/>Gasoline Type" />
<asp:BoundField DataField="machineID" Visible="false" HtmlEncode="false" HeaderText="رقم المكينة<br/>Machine ID" />
<asp:BoundField DataField="payment_type" HtmlEncode="false" HeaderText="طريقة الدفع<br/>Payment Type" />
<asp:BoundField DataField="price" HtmlEncode="false" HeaderText="السعر<br/>Price" />
<asp:BoundField DataField="qty" HtmlEncode="false" HeaderText="الكمية<br/>Qty" />
<asp:BoundField DataField="total" HtmlEncode="false" HeaderText="الإجمالي<br/>Total Amount" />
<asp:BoundField DataField="vat_amount" HtmlEncode="false" HeaderText="الضريبة<br/>VAT Amount" />
<asp:BoundField DataField="userID" HtmlEncode="false" HeaderText="المستخدم<br/>User" />
<asp:BoundField DataField="buyer_nameAR" HtmlEncode="false" HeaderText="اسم العميل<br/>Customer Name" />
<asp:BoundField DataField="coupon" HtmlEncode="false" HeaderText="رقم الكوبون<br/>Coupon No" />
</Columns>
<PagerStyle HorizontalAlign="Center" CssClass="GridPager" />
<HeaderStyle HorizontalAlign="Center" />
</asp:GridView>
<br />
<asp:Label ID="lblTotalRecords" Text="text" runat="server" />
Namespaces
C#
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
VB.Net
Imports System.Net
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindDropDownList();
this.BindData();
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
if (ddlBranchNumbers.SelectedIndex > 0)
{
this.BindData();
}
if (ddlBranchNumbers.SelectedIndex > 0)
{
this.BindData();
}
}
private void BindData()
{
List<Root> roots = GetDataFromJSON();
if (ddlBranchNumbers.SelectedIndex > 0)
{
roots = roots.Where(x => x.branch_num.EndsWith(ddlBranchNumbers.SelectedValue.Trim())).ToList();
}
if (ddlSearchByMonths.SelectedIndex > 0)
{
roots = roots.Where(x => x._date.Month.ToString().Equals(ddlSearchByMonths.SelectedValue.Trim())).ToList();
}
gvInvoices.DataSource = roots;
gvInvoices.DataBind();
lblTotalRecords.Text = roots.Count.ToString();
}
private void BindDropDownList()
{
List<Root> roots = GetDataFromJSON();
ddlBranchNumbers.DataSource = roots.Select(x => new ListItem
{
Text = "رقم المحطة - " + x.branch_num,
Value = x.branch_num
}).Distinct();
ddlBranchNumbers.DataTextField = "Text";
ddlBranchNumbers.DataValueField = "Value";
ddlBranchNumbers.DataBind();
ddlBranchNumbers.Items.Insert(0, new ListItem { Text = "الكل", Value = "0" });
//ddlMachineNumbers.DataSource = roots.Select(x => new ListItem
//{
// Text = x.machineID.Substring(x.machineID.Length - 5, 5),
// Value = x.machineID.Substring(x.machineID.Length - 5, 5)
//}).Distinct();
//ddlMachineNumbers.DataTextField = "Text";
//ddlMachineNumbers.DataValueField = "Value";
//ddlMachineNumbers.DataBind();
//ddlMachineNumbers.Items.Insert(0, new ListItem { Text = "الكل", Value = "0" });
}
private List<Root> GetDataFromJSON()
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
string json = (new WebClient() { Encoding = System.Text.Encoding.UTF8 }).DownloadString("https://fuel-station-d8853-default-rtdb.firebaseio.com/invoices-prod.json");
JToken result = JsonConvert.DeserializeObject("[" + json + "]") as JToken;
List<Root> roots = new List<Root>();
foreach (var item in result)
{
JObject obj = JObject.Parse(item.ToString());
foreach (var keypair in obj)
{
foreach (var keypair1 in JObject.Parse(keypair.Value.ToString()))
{
foreach (var item1 in JObject.Parse(keypair1.Value.ToString()))
{
roots.Add(JsonConvert.DeserializeObject<Root>(item1.Value.ToString()));
}
}
}
}
return roots;
}
public class Root
{
public string branch_num { get; set; }
[JsonProperty("date")]
public DateTime _date { get; set; }
public string invoice_number { get; set; }
public string item { get; set; }
public string machineID { get; set; }
public string payment_type { get; set; }
public double price { get; set; }
public double qty { get; set; }
public double total { get; set; }
public string userID { get; set; }
public string buyer_nameAR { get; set; }
public string coupon { get; set; }
public double vat_amount { get; set; }
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindDropDownList()
Me.BindData()
End If
End Sub
Protected Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
If ddlBranchNumbers.SelectedIndex > 0 Then
Me.BindData()
End If
If ddlBranchNumbers.SelectedIndex > 0 Then
Me.BindData()
End If
End Sub
Private Sub BindData()
Dim roots As List(Of Root) = GetDataFromJSON()
If ddlBranchNumbers.SelectedIndex > 0 Then
roots = roots.Where(Function(x) x.branch_num.EndsWith(ddlBranchNumbers.SelectedValue.Trim())).ToList()
End If
If ddlSearchByMonths.SelectedIndex > 0 Then
roots = roots.Where(Function(x) x._date.Month.ToString().Equals(ddlSearchByMonths.SelectedValue.Trim())).ToList()
End If
gvInvoices.DataSource = roots
gvInvoices.DataBind()
lblTotalRecords.Text = roots.Count.ToString()
End Sub
Private Sub BindDropDownList()
Dim roots As List(Of Root) = GetDataFromJSON()
ddlBranchNumbers.DataSource = roots.[Select](Function(x) New ListItem With {
.Text = "رقم المحطة - " & x.branch_num,
.Value = x.branch_num
}).Distinct()
ddlBranchNumbers.DataTextField = "Text"
ddlBranchNumbers.DataValueField = "Value"
ddlBranchNumbers.DataBind()
ddlBranchNumbers.Items.Insert(0, New ListItem With {
.Text = "الكل",
.Value = "0"
})
End Sub
Private Function GetDataFromJSON() As List(Of Root)
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)
Dim json As String = (New WebClient() With {
.Encoding = System.Text.Encoding.UTF8
}).DownloadString("https://fuel-station-d8853-default-rtdb.firebaseio.com/invoices-prod.json")
Dim result As JToken = TryCast(JsonConvert.DeserializeObject("[" & json & "]"), JToken)
Dim roots As List(Of Root) = New List(Of Root)()
For Each item In result
Dim obj As JObject = JObject.Parse(item.ToString())
For Each keypair In obj
For Each keypair1 In JObject.Parse(keypair.Value.ToString())
For Each item1 In JObject.Parse(keypair1.Value.ToString())
roots.Add(JsonConvert.DeserializeObject(Of Root)(item1.Value.ToString()))
Next
Next
Next
Next
Return roots
End Function
Public Class Root
Public Property branch_num As String
<JsonProperty("date")>
Public Property _date As DateTime
Public Property invoice_number As String
Public Property item As String
Public Property machineID As String
Public Property payment_type As String
Public Property price As Double
Public Property qty As Double
Public Property total As Double
Public Property userID As String
Public Property buyer_nameAR As String
Public Property coupon As String
Public Property vat_amount As Double
End Class
Screenshot