Hi kid.live,
Refer below sample.
HTML
<table>
<tr>
<td>Branch Number:</td>
<td>
<asp:DropDownList runat="server" ID="ddlBranchNumbers">
</asp:DropDownList></td>
</tr>
<tr>
<td>Machine Number:</td>
<td>
<asp:DropDownList runat="server" ID="ddlMachineNumbers">
</asp:DropDownList></td>
</tr>
<tr>
<td>
<asp:Button Text="Search" runat="server" OnClick="Search" /></td>
</tr>
</table>
<hr />
<asp:GridView runat="server" ID="gvDetails">
</asp:GridView>
Namespaces
C#
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
VB.Net
Imports System.Collections.Generic
Imports System.Linq
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 Search(object sender, EventArgs e)
{
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 (ddlMachineNumbers.SelectedIndex > 0)
{
roots = roots.Where(x => x.machineID.EndsWith(ddlMachineNumbers.SelectedValue.Trim())).ToList();
}
gvDetails.DataSource = roots;
gvDetails.DataBind();
}
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 = "Select", 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 = "Select", Value = "0" });
}
private List<Root> GetDataFromJSON()
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
string json = (new WebClient()).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 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 Search(ByVal sender As Object, ByVal e As EventArgs)
Me.BindData()
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 ddlMachineNumbers.SelectedIndex > 0 Then
roots = roots.Where(Function(x) x.branch_num.EndsWith(ddlMachineNumbers.SelectedValue.Trim())).ToList()
End If
gvDetails.DataSource = roots
gvDetails.DataBind()
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 = "Select",
.Value = "0"
})
ddlMachineNumbers.DataSource = roots.[Select](Function(x) New ListItem With {
.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 With {
.Text = "Select",
.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()).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 vat_amount As Double
End Class
Screenshot